Duplication

One of the "enemies" of clean code.

Cases of duplication

  • Copy-pasted parts

  • Duplicated logic - something implemented more times on different ways

  • Duplicated values or information - literals or constants

Avoid inline implementation

This is also a misplaced responsibility!

  • Not reusable - Leads to duplication

  • Different implementations

  • Not testable

  • Refactor it

DRY Principle

  • Don't repeat yourself (Kent Beck)

Example: Refactor inline implementation

Example: Bad: Duplication causes incorrect intention

Example: Good: No duplicates, correct intention

Example: Bad: Duplicated implementation with differences

Example: Bad: Duplicates differ only in values

Example: Good: Refactor value differences to input parameters

Last updated

Was this helpful?