The DRY Principle

The DRY Principle#

Don’t Repeat Yourself

“Every piece of logic should have a single, unambiguous, authoritative representation within a system.”

Why DRY
  • Fix a bug in one place, not five

  • Changes propagate automatically

  • Reusable components get better testing

  • Encourages modular architecture

When DRY hurts
  • Over-abstraction obscures intent

  • Forcing two different use cases into one function creates “parameter soup”

  • Accidental duplication ≠ logical duplication

Keep in mind

“Duplication is far cheaper than the wrong abstraction.”


Example: Magic Strings#

WET ❌
print(df['Name'])
print(df['Age'])
# "Name" repeated everywhere

If the column is renamed, you hunt down every occurrence.

DRY ✔
cn = SimpleNamespace()
cn.name = 'Name'
cn.age = 'Age'

print(df[cn.name])
print(df[cn.age])

Rename once, everything follows. IDE autocomplete works. Typos crash immediately.