Notes
The problem
Here is the pain. The delivery fee is forty rupees plus eight per kilometre, and it is free above five hundred. Checkout computes it. The receipt email computes it again, with a copy of the same lines. Months later the business lowers the free delivery limit to four hundred. Someone updates Checkout and forgets the mailer. Now customers pay zero but their receipt shows a fee. That is the bug DRY prevents.
// in Checkout long fee = 4000 + km * 800; if (total >= 50000) fee = 0; // in ReceiptMailer, copied long fee = 4000 + km * 800; if (total >= 50000) fee = 0; // later: free delivery now // starts at 400, Checkout only
DRY
DRY means every piece of knowledge has one clear home. Notice the word knowledge, not code. The fee rule is one business fact, so it lives in one class, DeliveryFee. Checkout and the receipt mailer both ask it. When the free delivery limit changes, you edit one line, and both places are correct at once. The goal is not fewer lines. The goal is one place to change when a fact changes.
DRY in code
In code it is small. A DeliveryFee class holds the base charge, the rate per kilometre and the free delivery limit as named constants, and one method that applies the rule. Money is kept in paise as a long, so there are no rounding surprises. Both callers pass the order total and the distance. Nobody else knows the formula, so nobody else can get it wrong.
final class DeliveryFee {
static final long BASE = 4000;
static final long PER_KM = 800;
static final long FREE_FROM = 40000;
long forOrder(long total, int km) {
if (total >= FREE_FROM) return 0;
return BASE + km * PER_KM;
}
} DRY gone wrong
Now the trap. The rider is also paid eight rupees per kilometre. Same number, same multiplication. A keen developer merges them into one shared method. But these are two different facts. The customer price is set by marketing. The rider rate is set by operations and labour rules. Next month one changes and the other must not. Code that only looks the same by coincidence is accidental duplication. Leave it duplicated.
The wrong abstraction
If you merge anyway, this is what happens. A night surcharge applies only to customers, so you add a flag. A rain bonus applies only to riders, another flag. Soon the shared helper has four booleans and a tangle of ifs, and every change risks breaking a caller you did not think about. This is the wrong abstraction. The fix is to inline it back into two simple methods and let each one evolve on its own.
long perKm(int km, boolean rider,
boolean night, boolean rain,
boolean customer) {
long r = rider ? 800 : 800;
if (night && !rider) r += 200;
if (rain && rider) r += 300;
...
}
every caller now depends on
every other caller A safe test
So how do you decide? Ask one question. If this rule changes, must the other copy change too, every single time? If yes, it is one fact, so merge it. If no, they are separate facts that happen to match today. If you are not sure, wait until you see the same thing a third time. Merging later is cheap. Pulling apart a shared abstraction that six classes depend on is painful.
- same business fact?
- yes: merge, one home
- no: keep them apart
- not sure yet?
- wait for the third copy
- (rule of three)
- merging later is cheap,
- un-merging is painful
KISS
Next, KISS. Keep it simple. Choose the plainest design that solves the real problem. Here someone replaced the three line fee method with a generic rule engine that reads conditions from a map of lambdas and evaluates them in priority order. It is clever and it can express anything. But a new teammate needs an hour to find out what the fee is, and a bug in the engine breaks every price at once.
KISS in practice
Simple does not mean the fewest characters. A dense one-line stream that nobody can debug is not simple. Simple means a reader understands it quickly and can change it safely. Warning signs: you need a diagram to explain a small rule, a generic framework has exactly one user, or reuse is done through deep inheritance. Plain names, plain if statements and small methods usually win.
- you need a diagram to explain
- a three-line rule
- generic engine, one user
- clever one-liners and
- streams nobody can debug
- deep inheritance for reuse
- simple is not "short":
- clear names and plain ifs
- usually win
YAGNI
Third, YAGNI. You are not going to need it. The app only sells in India, in rupees. A developer builds a currency plugin system with exchange rate providers, just in case we go global. Today that code earns nothing, yet it must be read, tested and kept working through every change. And when the company really expands, the real needs are usually different from the guess. Build it when the requirement arrives.
YAGNI is not careless
YAGNI is not an excuse for messy code. It says skip features built for a maybe: extra plugins, extra settings, extra flags. It does not say skip clean structure. Small classes, clear names, tests and one home per rule are what make the future change cheap when it comes. A small interface you already need for testing is fine. A whole framework for a guess is not.
- skip: features for a maybe
- (plugins, configs, flags)
- keep: things that make any
- change cheap
- clear names, small classes,
- tests, one home per rule
- cheap seams are fine:
- an interface you already
- need for testing
Run it
Watch the clean version run. An order of four hundred and fifty rupees, three kilometres away. Checkout asks DeliveryFee and gets zero, because the free limit is now four hundred. The receipt mailer asks the same class with the same inputs and also gets zero. The two can never disagree, because there is only one copy of the rule. The rider payout stays in its own class with its own rate.
Balancing the three
These rules pull against each other. DRY pushes you to extract shared code. KISS and YAGNI push you to keep things plain and skip extra layers. The tie breaker is the cost of the next change. Extracting a real shared business fact makes changes cheaper, so DRY wins. Extracting a coincidence or building for a guess makes changes more expensive, so simplicity wins.
- DRY pushes you to extract
- KISS and YAGNI push you to
- keep things plain
- extract a real shared fact:
- DRY wins
- extract a guess or a
- coincidence: KISS wins
- judge by cost of change
In interviews
In a machine coding round, use these rules while you design, not as a speech at the end. Say pricing lives in one class, so the fee rule is not copied into checkout and receipts. Say you are not adding multiple currencies now, but the pricing interface leaves room for it. Interviewers like candidates who scope down on purpose. The common mistake is abstracting every two similar lines and ending up with a maze.
- "Pricing lives in one
- PricingStrategy, so fee
- rules are not copied into
- checkout and receipts."
- "I am not adding multi-
- currency now; the interface
- leaves room for it later."
- mistake: abstracting every
- pair of similar lines