A tiffin service in Pune cooks lunch every morning for a few hundred office workers. Its app shows the menu, takes payments, sends a monthly invoice and texts each customer when the rider leaves. The code is small, and three short rules help keep it that way.
DRY (don't repeat yourself) says each fact the business knows lives in one place. KISS (keep it simple, stupid) says we write the plainest code that does the job. YAGNI (you aren't gonna need it) says we build only what is needed now. Each rule has a common misreading, and DRY and KISS can pull against each other.
DRY: one fact, one place
The goods and services tax (GST) rate on a meal is one piece of knowledge. Prices are in cents, in a long, so a $10 meal is 1000. In the first version, the rate lives in three classes:
public class MenuCard { public long displayPriceCents(Meal meal) { return meal.priceCents() * 105 / 100; }} public class Checkout { public long gstCents(long subtotalCents) { return subtotalCents * 5 / 100; }} public class MonthlyInvoice { public long gstCents(long mealsTotalCents) { return mealsTotalCents * 5 / 100; }}Suppose the rate goes from 5% to 12%. A developer searches for 5 / 100, finds Checkout and MonthlyInvoice, and changes both. The search does not find 105 / 100 in MenuCard. For a $10 meal, checkout now charges $11.20, while the stale menu still shows $10.50.
The fix gives the rate one home, and every caller asks it:
public final class Gst { private static final int RATE_PERCENT = 5; private Gst() { } public static long on(long amountCents) { return amountCents * RATE_PERCENT / 100; }}MenuCard now returns meal.priceCents() + Gst.on(meal.priceCents()), and Checkout and MonthlyInvoice call Gst.on too. The rate change is one line in Gst, so the menu and the checkout cannot use different rates.
The misreading: DRY is not about lines that look the same. It is about knowledge. Two lines can match today and still belong to two different rules. We come back to this below.
KISS: the plainest code that works
Every evening, the kitchen needs tomorrow's count of veg and non-veg tiffins, leaving out customers who paused their plan. Here is a clever version:
public KitchenCount countFor(LocalDate day, List<Subscriber> subscribers) { int[] counts = subscribers.stream() .filter(s -> !s.isPausedOn(day)) .collect(() -> new int[2], (acc, s) -> acc[s.isVeg() ? 0 : 1]++, (left, right) -> { left[0] += right[0]; left[1] += right[1]; }); return new KitchenCount(counts[0], counts[1]);}It works, but the reader has to work out that index 0 is veg, index 1 is non-veg, and what the third lambda is for. A plain loop says the same thing in words:
public KitchenCount countFor(LocalDate day, List<Subscriber> subscribers) { int veg = 0; int nonVeg = 0; for (Subscriber subscriber : subscribers) { if (subscriber.isPausedOn(day)) { continue; } if (subscriber.isVeg()) { veg++; } else { nonVeg++; } } return new KitchenCount(veg, nonVeg);}If the kitchen adds Jain meals, the loop gets one more counter and one more branch. The clever version needs a new array index, and all three lambdas must agree on it.
The misreading: simple does not mean short, and it does not mean skipping the hard case. The loop is longer than the stream, and it still handles paused customers. Simple means a reader can see what the code does and change it safely.
YAGNI: build it when it is needed
The service sends one SMS per delivery, "your tiffin is on the way", through one vendor. A developer expects more vendors some day, so they build a plugin system before anyone needs it. It has an SmsVendor interface and a QuickSmsVendor that implements it, a registry, a loader that reads vendor class names from a config file, and a fallback rule.
All of that serves one vendor. Each part has to be read, tested and kept working, and none of it does anything the one class cannot do today. The class the service needs today is small:
public class DeliverySms { private final QuickSmsClient client; public DeliverySms(QuickSmsClient client) { this.client = client; } public void sendOnTheWay(String phone, String riderName) { if (phone == null || !phone.matches("[6-9]\\d{9}")) { throw new IllegalArgumentException("Not a mobile number: " + phone); } client.send(phone, "Your tiffin is on the way with " + riderName + "."); }}When a second vendor arrives, we put an interface in front of the client. Only DeliverySms calls QuickSmsClient, so that change is small. The Adapter pattern page makes this change with two SMS providers.
The misreading: YAGNI is about features, not about care. The phone number check is not a future feature. Bad numbers can arrive today, so DeliverySms checks them today, and that check needs tests today.
When DRY and KISS pull against each other
Riders get a bonus of 5% of each order, so the payout code also has subtotal * 5 / 100. It looks like the GST line. A developer reads DRY as "merge lines that look alike" and makes RiderPay call Gst.on. Then GST goes to 12%, and the accountant updates Gst. Riders now get 12% without anyone deciding that. The next fix adds a flag, Gst.on(amount, forRider), and every reader of Gst must now understand two rules to read one. That flag breaks KISS. DRY never asked for the merge, because the GST rate and the rider bonus are two pieces of knowledge with different owners. Two lines that look alike are simpler than one method that serves two owners.
In an interview
In a low-level design (LLD) round, give these rules as your reasons, out loud. Say which fact you keep in one place, what you are not building yet, and what would make you build it: "one SMS vendor today, so one class; a second vendor gets an interface". The Open-Closed Principle page shows when an extension point has earned its place.
Common mistakes:
- Merging code because it looks the same. Two rules with different owners stay apart, even when their lines match today.
- Building for a vendor, format or region nobody asked for. Name the change that would make you build it, and stop there.
- Using YAGNI to skip validation or tests. They are part of the feature we are building now.
Summary
- DRY: each piece of knowledge has one home. Lines that only look alike are not the same knowledge.
- KISS: write the plainest code a reader can follow and change. Short is not the same as simple.
- YAGNI: build what is needed now, and build it properly. Add the extension point when the second case arrives.