Produced from book: The Pragmatic Programmer

Duplication of knowledge is bad design because forces the operator to modify several modules to change one single fact


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


The less modules needed to be changed when representation of knowledge is required to change, the easier is to maintain and understand the codebase. The tricky part is that DRY is not limited only to code duplication, and not all code duplication is knowledge duplication.

For example:

def validate_age(value):
	validate_type(value, :integer)
	validate_min_integer(value, 0)

def validate_quantity(value):
	validate_type(value, :integer)
	validate_min_integer(value, 0)

The functions have the same body, but the knowledge they represent is different.

The DRY principle can also be violated in documentation. Commentaries describing what the code does are duplicating knowledge effectively. The code already describes by itself what it does (unless it has obscure naming and structure), so adding commentaries describing this again in natural language adds an extra step to maintain.

We can also observe violations with regard data structures.

class Line {
	Point start;
	Point end;
	# length should be calculated, so it adapts automatically 
	double length; 
}

Finally, duplication can also be observed in data sources. Image your database has a users table:

users:
  id:     number
  name:   text
  email:  text

Then, if we write the same structure in source code, we are duplicating it:

class User {
  id: number;
  name: string;
  email: string;
}

If someone adds a new column to the database and forgets to update the User class, bug follows.