Don’t Repeat Yourself

Definition

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

Introduction

DRY on wikipedia makes for good light introduction. Go ahead, read it.

While the DRY on c2 provides reasoning and qualification on where duplication is not a DRY violation. Go ahead, read that too.

Application

When in doubt, I like to apply “two is a coincidence, three is a pattern”. Especially when the code has not been proven correct yet.

Avoid redundant naming / qualifications.

While the concept is simple, it takes some experience to see how ruthlessly it can be applied.

For example, in combination with higher-order functions you can take WET

while True:
  foo()

and DRY it as

def forever(fn):
  while True:
    fn()

forever(foo)