Overview

The DRY principle states that 'every piece of knowledge must have a single, unambiguous, authoritative representation within a system.' When code is duplicated, any change requires updating multiple locations, which is error-prone.

How to Achieve DRY

  • Functions and Methods: Extract common logic into reusable functions.
  • Classes and Inheritance: Use OOP to share behavior.
  • Modules and Libraries: Package common code for use across projects.

Trade-offs

Over-applying DRY can lead to 'premature abstraction,' making the code harder to understand and maintain. Sometimes, a little duplication is better than a wrong abstraction.

Related Terms