Site Archive (Complete)
Architecture Blog: "Don't Repeat Yourself" Principle
Architecture & Design
PATTERN LANGUAGE

Modeling, Managing, Making it Right.

by Jonathan Erickson
IF YOU BUILD IT

... Will they Come?

by Arnon Rotem-Gal-Oz
August 16, 2006

"Don't Repeat Yourself" Principle

Another OO principle worth discussing is "Don’t Repeat Yourself" (or "DRY" for short). I’ve seen it referenced as "Single Point Control".

The principle simply states that:

Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.

Actually saying that this is an "object-oriented principle" does a disservice to this principle because it is a fundamental principle for software engineering. For example, consider:

  • Database normalization rules. Redesigning tables so that data will only be defined once.
  • Refraining from magic numbers. Adding constants, variables.
  • Parameterizing algorithms. Instead of rewriting them for new sets of requests.
  • Writing functions instead of repetitive code.
  • Even go-to statements.
  • etc.

For object orientation DRY means things like using inheritance, template methods etc. DRY is also related to several refactorings like extract class, extract method, extract supereclass, etc.

The most obvious benefit of DRY is increasing maintainability and testability--you have one place to go when you need to change something and you only have to test once for all. if you fail to apply DRY a maintainer/tester will have to repeat their efforts as well (plus the overhead of understanding what is it exactly that happening and why the results are different).

The pitfall of DRY is trying to generalize too much which can result in over complication (also see YAGNI).

Posted by Arnon Rotem-Gal-Oz at 05:16 AM  Permalink




 
INFO-LINK


Related Sites: DotNetJunkies, SD Expo, SqlJunkies