Codacoda
Back to Academy

coding principles

DRY - Don't Repeat Yourself

The DRY principle states that every piece of knowledge must have a single, unambiguous, authoritative representation within a system. Code duplication leads to inconsistencies — when a bug is fixed in one copy but not another, or when business rules drift between duplicated implementations. DRY is not just about identical lines of code; it is about duplicated knowledge. Extract shared logic into functions, constants, modules, or configuration so that changes need to be made in only one place.

Use Cases

  • Extracting repeated validation logic into a shared utility function
  • Using constants or enums instead of magic strings scattered throughout the code
  • Creating shared UI components instead of copying markup across pages
  • Centralizing business rules in a single module rather than duplicating them in multiple services

Visualization

BEFOREUserControllervalidateEmail()formatDate()OrderControllervalidateEmail()formatDate()ReportServicevalidateEmail()formatDate()
Speed:1x
Three classes each contain identical validateEmail() and formatDate() implementations. The same logic is duplicated in three places.Step 1 / 7

Implementation

Output

Click "Run Code" to see output...