Книга: Metaprogramming Ruby 2
Назад: The Fall of alias_method_chain
Дальше: Chapter 12: The Evolution of Attribute Methods

A Lesson Learned

Throughout this book I showed you how convenient, elegant, and cool metaprogramming can be. The story of alias_method_chain, however, is a cautionary tale: metaprogramming code can sometimes get complicated, and it can even cause you to overlook more traditional, simpler techniques. In particular, sometimes you can avoid metaprogramming and use plain, old-fashioned object-oriented programming instead.

The lesson I personally learned from this story is: resist the temptation to be too clever in your code. Ask yourself whether there is a simpler way to reach your goal than metaprogramming. If the answer is no, then go forth and metaprogram the heck out of your problem. In many cases, however, you’ll find that a more straightforward OOP approach does the job just as well.

In this chapter, I showed you that metaprogramming can be overused and sometimes replaced with simpler techniques. To be fair, however, metaprogramming is still one of the tastier ingredients in the Rails pie. In the next chapter, I’ll show you how one of Rails’ defining features owes its very existence to a clever mix of metaprogramming tricks.

Назад: The Fall of alias_method_chain
Дальше: Chapter 12: The Evolution of Attribute Methods