Don’t Repeat Yourself

The DRY principle - short for “don’t repeat yourself” - comes from The Pragmatic Programmer.

Notes

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

Following this principle is one of the best ways to prevent bugs and move faster. Every duplicated piece of knowledge is a bug waiting to happen. Many development techniques are really just ways to prevent and eliminate duplication, and many smells are just ways to detect existing duplication.

When knowledge is duplicated, changing it means making the same change in several places. Leaving duplication introduces a risk that the various duplicate implementations will slowly diverge, making them harder to merge and making it more likely that a bug remains in one or more incarnations after being fixed.

Duplication leads to frustration and paranoia. Rampant duplication is a common reason that developers reach for a Grand Rewrite.

Duplicated Knowledge vs Duplicated Text

It’s important to understand that this principle states that knowledge should not be repeated; it does not state that text should never be repeated.

For example, this sample does not violate the DRY principle, even though the word “save” is repeated several times:

def sign_up 
	@user.save 
	@account.save 
	@subscription.save
end

However, this code contains duplicated knowledge that could be extracted:

def sign_up_free 
  @user.save 
  @account.save 
  @trial.save
end
 
def sign_up_paid 
  @user.save 
  @account.save 
  @subscription.save
end

Notes

Chú ý tới duplicated knowledge hơn là duplicated text

Application

The following smells may point towards Duplicated Code and can be avoided by following the DRY principle:

Making behavior easy to reuse is essential to avoiding duplication. Developers won’t be tempted to copy and paste something that’s easy to reuse through a small, easy to understand class or method. You can use these solutions to make knowledge easier to reuse:

Applying these techniques before duplication occurs will make it less likely that duplication will occur. If you want to prevent duplication, make knowledge easier to reuse by keeping classes small and focused.