Another example pulled from an actual code base is the bubble method:
def bubble(*args)The bubble method is used in our code base to facilitate a Domain Specific Language that reads like natural language. The bubble method was extracted from duplicate methods in the code base such as:
args.each do |method_name|
define_method(method_name) do
self
end
end
end
def flagHowever, the above defined bubble method allows the
self
end
def as
self
end
flag
and as
methods to be defined simply by using:bubble :flag, :asThe current code base I'm working in has several of these class methods that do everything from simply returning self to creating entire object graphs.
The reduced code duplication is an obvious benefit to this style of programming. However, some developers dislike this style of programming because they feel that the class methods obscure intent and decrease readability. There is clearly a trade-off and in the end it may simply be a coding style choice.
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.