Sunday, February 14, 2010

DRY

One key principle behind Ruby on Rails is the one call DRY, an acronym for Don't Repeat Yourself.  Expanded, it means that you should never write the same complex piece of code twice, but instead use function definitions and calls.  The main objective behind it is to ease maintenance, by restricting future changes to code to one place instead of multiple as things need to be modified or fixed.

The principle is by no means a new one to software development and has been touted without the fancy acronym outside of Ruby on Rails for many years.   Subroutines in the original assembly languages had this value in mind as did macro definitions C and function and method definitions in many languages.  Class definitions and even dynamic linking could arguably be considered to have some basis in the same idea.

The visible effects of this principle on Ruby code in Rails are multiple.  For a start, method bodies tend to be small and application structure granular.  Reasons for this include the fact that every method that doesn't implement the most primitive of tasks calls at least one other method.  And Ruby's expressiveness and dynamic nature mean that predefined logic can be reused in all sorts of grammatical contexts, such as overridden operators, enumerators, or as overridden basic classes.

Another effect of DRY on Ruby programs related to the above is that method definitions are numerous.  Following the "no repetition allowed" principle in coding means that nearly every non-trivial method's definition relies on an spawns several other method definitions.   A knock-on effect of this for development is that stack traces from Ruby code like that in Ruby on Rails are long and sometimes challenging to follow due to the deep nesting of method calls.

The DRY principle does overall benefit both the application and the programmer in a number of ways.  It forces the programmer to think in a modular fashion, breaking down large problems domain into key atomic ideas.  It results in logic fragments and concepts that are nicely packaged, easily reusable and usually easy to understand.  And it decreases the number of duplicate code changes that need to be made to modify code thereby reducing coding/copying errors.

Most modern languages employ the DRY principle in some form as code reuse mechanisms.  In coding views, fragments of HTML are instinctively packaged into includable fragments such as Rails "Partials".   Most libraries and languages including Javascript and jQuery rely heavily on function definitions.  CSS supports runtime merging of files (and, using SASS, as many Rails developers do, the reusability of styles through "mixins"). In fact, nearly every language or tool implements some mechanism for defining reusable code fragments, not only within an application, but amongst more that one.  The grammatical contexts and semantics for re-use in software development tend to follow a core set of known patterns, but are on these there are many variations.

In general, the DRY principle is every programmer's friend.  If you or someone else may need to design or do something more than once, turn it into a method, class, module, or even plugin/engine.  It might add future value to even portions of your present output, or at least save on headache medication later.

No comments:

Post a Comment

Followers