Don??™t Repeat Yourself
At a high level, metaprogramming is useful in working toward the DRY principle
(Don??™t Repeat Yourself). Also referred to as ???Once and Only Once,??? the DRY principle
dictates that you should only need to express a particular piece of information
once in a system. Duplication is usually unnecessary, especially in dynamic
* http://www.zenspider.com/ZSS/Products/ParseTree/
?? http://rubyforge.org/projects/seattlerb
What Is Metaprogramming? | 3
languages like Ruby. Just as functional abstraction allows us to avoid duplicating
code that is the same or nearly the same, metaprogramming allows us to avoid duplicating
similar concepts when they recur throughout an application.
Metaprogramming is primarily about simplicity. One of the easiest ways to get a feel
for metaprogramming is to look for repeated code and factor it out. Redundant code
can be factored into functions; redundant functions or patterns can often be factored
out through the use of metaprogramming.
Design patterns cover overlapping territory here; patterns are designed
to minimize the number of times you have to solve the same problem.
In the Ruby community, design patterns have acquired something of a
negative reputation. To some developers, patterns are a common
vocabulary for describing solutions to recurring problems. To others,
they are overengineered.
To be sure, patterns can be overapplied.
Pages:
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27