Don’t Repeat Yourself

Don’t Repeat Yourself


Duplication (inadvertent or purposeful duplication) can lead to maintenance nightmares, poor factoring, and logical contradictions.

Duplication, and the strong possibility of eventual contradiction, can arise anywhere: in architecture, requirements, code, or documentation. The effects can range from mis-implemented code and developer confusion to complete system failure.

The Mars Climate Orbiter was lost due to a semantic contradiction: part of the system was working in Imperial units, another in Metric. There was a duplication of knowledge (implicit units), and the duplicates were out of step.

One could argue that most of the difficulty in Y2K remediation is due to the lack of a single date abstraction within any given system; the knowledge of dates and date-handling is widely spread.

The Problem:

But what exactly counts as duplication? CloneAndModifyProgramming is generally cited as the chief culprit (see OnceAndOnlyOnce, etc.), but there is more to it than that. Whether in code, architecture, requirements documents, or user documentation, duplication of knowledge – not just text – is the real culprit.


The DRY (Don’t Repeat Yourself) Principle states:

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

It’s okay to have mechanical, textual duplication (the equivalent of caching values: a repeatable, automatic derivation of one source file from some meta-level description), as long as the authoritative source is well known.

For example, in a mixed-language CORBA environment you may choose to treat the IDL definitions as authoritative. These definitions will be used to automatically generate source code files which duplicate the knowledge in the IDL. But that’s okay: the IDL form of the knowledge meets the requirements of our definition.

Where different levels of abstraction are involved, a consistent conflict-resolution scheme must be used. This could be as simple as identifying one level as authoritative in all cases, or always deferring to the high level, or whatever, as long as it is consistently applied.

For example, in C++ the interface and implementation for a class are typically specified in separate files, duplicating knowledge. You may consider the header file to be authoritative for the contract of the class as viewed by its clients, and the source code to be authoritative regarding issues of implementation which are hidden by the implementation.


This principle is similar to OnceAndOnlyOnce, but with a different objective. With OnceAndOnlyOnce, you are encouraged to refactor to eliminate duplicated code and functionality. With DRY, you try to identify the single, definitive source of every piece of knowledge used in your system, and then use that source to generate applicable instances of that knowledge (code, documentation, tests, etc).

The principle also assumes that your projects have a high degree of automation, allowing the generation of the derivative knowledge artifacts whenever required.

4 comments to Don’t Repeat Yourself

  • 算是一我的个人博客,没有打算,做成网站的模式,只是想找一块儿地儿,分享表达一下。:)

  • I’m having a slight predicament right here. I would love to purchase myself a good phone and can’t decide on which phone to select. First of all, i am considering the Samsung B3310, because my friend has one. It appears pretty tough, and everything seemed good, but i began looking at others. Now my largest dilemma is to choose between the Nokia 5610Nokia 6500 and the Samsung i900 Omnia. Any advice?

  • I am not good at choosing phones.But maybe you can try a adive like the follows:
    list main strongpoints of the three phone:Nokia 5610,6500,Samsung i900
    like this:
    (5610) p0:…,p1:….,p2:…
    (6500) p0:….,p1:….,p2:…
    (i900) p0:….,p1:….,p2:…
    then erase (px(x=0,1,2)) which function you can accept to delete from the list, which phone remains more points what you choose to keep, maybe that’s your real choice from your heart.

    Hope this will help you! :)

  • Wonderful blog! I genuinely love how it’s easy on my eyes as well as the info are well written. I am wondering how I might be notified whenever a new post has been made. I have subscribed to your rss feed which must do the trick! Have a nice day!

Leave a Reply




You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

Protected by WP Anti Spam