Monday, January 12, 2009

Essential Software Principles: Don't Repeat Yourself (DRY)

Don't Repeat Yourself, also called the Single Point of Truth, is about representing any and every piece of information in a single representation.

It is usually used only for code, but was meant to have a broader meaning. According to the authors of The Pragmatic Programmer it should include "database schemas, test plans, the build system, even documentation". They further refer to various techniques for achieving this, like code generation, automatic build systems and scripting languages.

It is an important principle to focus on. Whatever it is you do, if it's described in two places one is going to get out of sync from the other. Other issues come as well. Duplication will make it harder to do changes, it reduces clarity and is perfect for creating inconsistencies in logic.

The Pragmatic Programmer defines for reasons for duplication:

  • Imposed duplication: You feel you have no other choice
  • Inadvertent duplication: You don't realize you are duplicating
  • Impatient duplication: Duplication seems like the simplest option
  • Interdeveloper duplication: Multiple people duplicate

All are common cases for duplication. Being aware of each and working to avoid them are essential.

The prime example of imposed duplication is documentation vs code. Often this is something you cannot avoid. It can also bring value. For instance can different views of a system be terribly helpful in simplifying and understanding issues. But as we all know, these things quickly get out of sync. And the effort of keeping documentation up to date is seldom a funny effort.

Often when you have an example of Imposed Duplication, other ways exist to solve it. Code is often duplicated into documentation because it is not available externally in a concise form. Perhaps you could get away with the BDD form of doing it, having running pieces of business rules. That would in many ways remove duplication.

I don't have much to concrete to say about it concerning our prime artifact: the code. Except one thing. You don't duplicate unless you have a really, really good reason for it. Duplication leads to anger, anger leads to hate...hate leads to suffering.

No comments: