DRY, KISS, YAGNI, cohesion and coupling
- DRY
- Don't Repeat Yourself
- KISS
- Keep It Simple, Stupid
- YAGNI
- You Aren't Gonna Need It
Mid-level Frequently asked 4 min
On this page
The question
What do high cohesion and low coupling mean, and why do they matter?
30-second answer
Cohesion is how closely the parts of one class belong together; coupling is how much a class depends on others. Aim for classes whose methods all serve one purpose, connected through small, stable interfaces. Then a change stays inside one class instead of rippling through every class that depends on it.
Understand it
How far does a change to Pricing reach?
Explore3 classes call Pricing directly, so a change to Pricing can break all 3.
View as text
Up to five classes, Cart, Order, Invoice, Quote and Report, can depend on the Pricing class. A slider sets how many do, and a toggle puts a PriceRules interface between them and Pricing. Classes a change to Pricing can break are marked at risk.
1 class calls Pricing directly, so a change to Pricing can break it. One class is marked at risk.
5 classes call Pricing directly, so a change to Pricing can break all 5. All five are marked at risk.
5 classes depend only on the PriceRules interface, so a change inside Pricing breaks none of them. The arrows now end at the interface, Pricing implements it below, and every class is marked safe.
Up to five classes, Cart, Order, Invoice, Quote and Report, can depend on the Pricing class. A slider sets how many do, and a toggle puts a PriceRules interface between them and Pricing. Classes a change to Pricing can break are marked at risk.
1 class calls Pricing directly, so a change to Pricing can break it. One class is marked at risk.
5 classes call Pricing directly, so a change to Pricing can break all 5. All five are marked at risk.
5 classes depend only on the PriceRules interface, so a change inside Pricing breaks none of them. The arrows now end at the interface, Pricing implements it below, and every class is marked safe.
Takeaway: Every class that depends on a concrete class can break when it changes. A narrow interface in between keeps the change inside.
In code
interface PriceRules { Money priceOf(Item item); }
class Pricing implements PriceRules { // free to change inside
@Override public Money priceOf(Item item) { return item.basePrice(); }
}
class Cart {
private final PriceRules prices; // knows only the interface
Cart(PriceRules prices) { this.prices = prices; }
Money total(List<Item> items) {
Money sum = Money.ZERO;
for (Item item : items) sum = sum.plus(prices.priceOf(item));
return sum;
}
} Wrong String chained = order.getCustomer().getAddress().getCity(); // couples the caller to 3 classes
Right String asked = order.shippingCity(); // couples it to Order only// before: the same rule copied into two classes, and the copies drift apart
Wrong boolean signupOk = email != null && email.contains("@"); // SignupController
Wrong boolean inviteOk = email != null && email.contains("@"); // InviteService
// after: a value type owns the rule; changing it edits one line
Right record Email(String value) {
Right Email { if (value == null || !value.contains("@")) throw new IllegalArgumentException("invalid email"); }
}Solve it
Split an OrderUtils class that every team edits
OrderUtils has 14 static methods: tax rules, PDF invoice rendering, email text and date parsing. Every release has merge
conflicts in it, and a tax change once broke invoices. Improve cohesion and coupling without over-engineering.
- Group methods by the data they use. Tax methods read order lines and regions; rendering reads an invoice; date parsing reads only strings. Groups that share no data show low cohesion.
- Move each group to its owner. Tax goes to a
TaxPolicy, rendering to anInvoiceRenderer, and date parsing is replaced byjava.time. Each class now changes for one reason. - Narrow what callers know. A caller that needed tax depended on all of
OrderUtils; now it depends onTaxPolicyonly, so coupling drops. - Keep it simple (KISS, YAGNI). Move code; do not add an interface or a factory per method until a second implementation exists.
- Check. A tax change now touches one class and its tests. Count the files a typical change edits, before and after.
// before: one class, four unrelated reasons to change
final class OrderUtils {
static Money tax(Order o) { /* region rules */ return Money.ZERO; }
static byte[] invoicePdf(Order o) { /* layout */ return new byte[0]; }
static LocalDate parse(String s) { /* hand-written parser */ return null; }
}
// after: cohesive classes, and callers depend only on what they use
class TaxPolicy {
Money taxFor(Order order) { /* region rules */ return Money.ZERO; }
}
class InvoiceRenderer {
byte[] render(Invoice invoice) { /* layout */ return new byte[0]; }
}
LocalDate due = LocalDate.parse("2026-11-08"); // java.time already parses ISO datesHow to answer
- High cohesion: everything in a class serves one purpose.
- Low coupling: classes depend on few, stable abstractions.
- A change to a class with many dependents can break all of them.
- Interfaces, events and narrow method signatures lower coupling.
Common mistakes
- Saying low coupling means no dependencies; classes must collaborate, through narrow and stable contracts.
- Treating DRY as "never repeat a line"; merging code that changes for different reasons couples it.
Follow-up questions
What is the Law of Demeter?
A method should talk only to its close friends: its own fields, its parameters and objects it creates, not to objects returned by them. order.getCustomer().getAddress().getCity() couples the caller to three classes; asking order.shippingCity() couples it to one.
- Talk to fields, parameters and objects you create.
- Chained getters expose another class's internal structure.
- Chains on one fluent API, such as a builder or a stream, are fine.
String before = order.getCustomer().getAddress().getCity(); // knows 3 classes
String after = order.shippingCity(); // knows 1 See it in the infographic: How far does a change to Pricing reach?
What do DRY, KISS and YAGNI mean?
DRY, Don't Repeat Yourself: each piece of knowledge has one place in the code. KISS, Keep It Simple, Stupid: choose the simplest design that works. YAGNI, You Aren't Gonna Need It: do not build features or extension points before they are needed. Together they push toward small, direct code that is easy to change.
- DRY is about knowledge, not about lines that look the same.
- KISS favors plain code over clever code.
- YAGNI avoids paying now for needs that may never come.
A small change to the Pricing class broke five other classes. What would you change?
Find what those classes actually use from Pricing and put it behind a small interface that they depend on. Pricing implements it, so its internals can change freely. Then ask why five classes call pricing directly; perhaps one service should own that call.
- Depend on a narrow interface, not on the concrete class.
- Keep the internals behind that interface.
- Reduce the number of direct callers.
See it in the infographic: How far does a change to Pricing reach?
Is duplication always worse than a shared abstraction?
No. Merging two pieces of code that only look alike couples them, and the next change to one forces flags into the shared code. Keep the duplicates until a shared reason to change is clear.
How can you measure coupling?
Count dependencies: the classes a class uses and the classes that use it. A class with many dependents must stay stable. Tools such as JDepend report these counts, and ArchUnit tests can forbid unwanted dependencies.
Check yourself
Show answers
- 0: Correct. They see only the interface, so the implementation can change freely.
- 5: Incorrect. They no longer depend on Pricing itself, only on the interface.
- 1: Incorrect. No class depends on the implementation directly.
Show answers
- order.shippingCity(): Correct. The caller talks only to the order it already has.
- order.getCustomer().getAddress().getCity(): Incorrect. It reaches through two objects returned by other calls.
- order.getCustomer().getName(): Incorrect. It still talks to an object returned by another call.