DRY – Don’t Repeat Yourself

“Don’t Repeat Yourself” is a principle of programming.  I like this quote by Uncle Bob (Robert C. Martin) in his book Clean Code (italics mine):

Duplication may be the root of all evil in software.  Many principles and practices have been created for the purpose of controlling or eliminating it.  Consider, for example, that all of Codd’s database normal forms serve to eliminate duplication in data.  Consider also how object-oriented programming serves to concentrate code into base classes that would otherwise be redundant.  Structured programming, Aspect Oriented Programming, Component Oriented Programming, are all, in part, strategies for eliminating duplication.  It would appear that since the invention of the subroutine, innovations in software development have been an ongoing attempt to eliminate duplication from our source code. (pg 48)

This relates to hints I’ve been dropping here and there and everywhere (here too) about condensing the expression of knowledge.  I like Uncle Bob’s conclusion to the chapter:

Every system is built from a domain-specific language designed by the programmers to describe the system.  Functions are the verbs of that language, and classes are the nouns.  This is not some throwback to the hideous old notion that the nouns and verbs in a requirements document are the first guess of the classes and functions of a system.  Rather, this is a much older truth.  The art of programming is, and has always been, the art of language design.

Master programmers think of systems as stories to be told rather than programs to be written.  They use the facilities of their chosen programming language to construct a much richer and more expressive language that can be used to tell that story.  Part of that domain-specific language is the hierarchy of functions that describe all the actions that take place within that system.  In an artful act of recursion those actions are written to use the very domain-specific language they define to tell their own small part of the story. (pg 49)

One thought on “DRY – Don’t Repeat Yourself

  1. […] The following code is ripe for some kind of alleviation of excessive typing (it’s a language-mandated violation of the DRY principle). […]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s