However, this need not be the
case if they are used judiciously. Design patterns are only useful insofar
as they reduce cognitive complexity. In Ruby, some of the finegrained
patterns are so transparent that it would be counterintuitive to
call them ???patterns???; they are really idioms, and most programmers
who ???think in Ruby??? use them without thinking. Patterns should be
thought of as a vocabulary for describing architecture, not as a library
of prepackaged implementation solutions. Good Ruby design patterns
are vastly different from good C++ design patterns in this regard.
In general, metaprogramming should not be used simply to repeat code. You should
always evaluate the options to see if another technique, such as functional abstraction,
would better suit the problem. However, in a few cases, repeating code via
metaprogramming is the best way to solve a problem. For example, when several
very similar methods must be defined on an object, as in ActiveRecord helper methods,
metaprogramming can be used.
Caveats
Code that rewrites itself can be very hard to write and maintain. The programming
devices you choose should always serve your needs??”they should make your life easier,
not more difficult. The techniques illustrated here should be more tools in your
toolbox, not the only tools.
Bottom-Up Programming
Bottom-up programming is a concept borrowed from the Lisp world.
Pages:
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28