Software Craftsmanship & Practice
Pragmatic Mindset: DRY, Orthogonality, Reversibility
Three lenses from The Pragmatic Programmer for keeping a codebase changeable over years, not just working on day one: don't duplicate knowledge, keep components independent, and avoid decisions that can't be undone.
- DRY — "every piece of knowledge must have a single, unambiguous, authoritative representation" — broader than "don't copy-paste code": it covers docs, schemas, config, and business rules stated twice in different forms
- Orthogonality — two components are orthogonal if changing one has no effect on the other; the practical test is "can I change X without understanding Y?"
- Reversibility — requirements will change, so prefer decisions that are cheap to undo over ones that lock in a direction
- "No broken windows" — small, visible quality lapses left unfixed normalize further decay quickly
- DRY is about knowledge, not literal text — two pieces of code that happen to look similar today but change for unrelated reasons are not a DRY violation
DRY is the most quoted and most misapplied of the three. It does not say "delete duplicate-looking code" — it says a given piece of knowledge (a business rule, a constant, a format) should live in exactly one authoritative place. A tax rate hard-coded in three files is a DRY violation even though the three occurrences don't look like "duplicated code" in an IDE's sense; two unrelated methods that happen to contain a similar four-line loop, for different reasons that will diverge over time, are not a DRY violation even though they look identical today.
// DRY violation: the discount rule is knowledge, stated in two places.
class Checkout {
double total(Cart cart) {
double t = cart.subtotal();
if (cart.itemCount() > 10) t *= 0.9; // bulk discount, stated here...
return t;
}
}
class ReceiptEmail {
String preview(Cart cart) {
double est = cart.subtotal();
if (cart.itemCount() > 10) est *= 0.9; // ...and again here. One will drift.
return "Estimated: quot; + est;
}
}
// Fixed: one authoritative place for the rule.
class DiscountPolicy {
static double apply(Cart cart, double amount) {
return cart.itemCount() > 10 ? amount * 0.9 : amount;
}
}