Loading…
Loading…
Charting your lesson…
Opening your lesson…
Design Principles: DRY, KISS, Coupling and Cohesion
Lesson 3 of 3
6 steps•about 14 min•11 flashcards
Step 1/ 61 min
Beyond making code work, good engineers make it maintainable. A handful of timeless principles — DRY, KISS, YAGNI, and the pairing of low coupling / high cohesion — capture what separates clean code from a mess, and they recur constantly in interviews.
Core idea: these principles guide code toward being simple, non-repetitive, and well-structured. DRY eliminates duplication, KISS favours simplicity, YAGNI avoids speculative features, and the twin goals of low coupling (modules minimally dependent) and high cohesion (each module does one focused thing) drive good modular design.
Step 2/ 62 min
// High coupling (bad): OrderService reaches into MySQL internals directly
// Low coupling (good): OrderService depends on a Repository interface
The mantra: "low coupling, high cohesion."
These principles reinforce each other but can also tension:
Step 3/ 61 min
A UserManager class validates emails, saves to the database, sends welcome emails, and formats reports — plus the email-validation regex is copy-pasted in three methods.
isValidEmail() method (single source of truth; fix it once).UserValidator, UserRepository, EmailService, ReportFormatter (each highly cohesive — one responsibility, echoing SRP).Step 4/ 61 min
Common asks: "what is DRY / KISS / YAGNI", "difference between coupling and cohesion and which you want high/low", "give an example of tight coupling and how to reduce it" (introduce an interface/dependency injection), and "what is a code smell". These often connect back to SOLID (SRP ≈ high cohesion; DIP enables low coupling).
Step 5/ 61 min
Getting coupling and cohesion backwards — aiming for "high coupling" or "low cohesion". You want low coupling (modules independent, so changes don't ripple) and high cohesion (each module focused on one job). Reversing them describes a fragile, tangled design, yet the symmetry of the terms makes this a frequent slip.
Step 6/ 6· Worked examples3 min
Problem
Critique this code against DRY, KISS, coupling, and cohesion, then refactor. Explain how each principle applies.
class Report:
def total_with_tax(self, items):
t = 0
for i in items: t += i.price
return t + t * 0.18 # tax logic duplicated below
def invoice_with_tax(self, items):
t = 0
for i in items: t += i.price # same summation duplicated
return t + t * 0.18 # same tax rate hard-coded again
def send_email(self, addr, body): # unrelated responsibility here
...
Try it on paper first.
Recall
Keep this
Clean-code principles — DRY, KISS, coupling, and cohesion — show design sense beyond making code work. "What is low coupling / high cohesion?" and "what does DRY mean?" are frequent interview questions that reveal how you think about maintainability.
The goal: low coupling + high cohesion — the most cited maintainability principle.
| Property | Good value | Meaning |
|---|---|---|
| Coupling | low | modules independent |
| Cohesion | high | module is focused |
"What is DRY/KISS/YAGNI?", "explain coupling and cohesion", "what's the ideal combination?", "why is high cohesion good?", and "what is separation of concerns?".
DRY = single source of truth (no duplication); KISS = simplest solution; YAGNI = don't build until needed. Coupling = inter-module dependence (want it low); cohesion = intra-module focus (want it high). The maintainability goal is low coupling + high cohesion, driven by separation of concerns.
You've read 0 of 6 steps
Now lock it in — questions straight after reading are what make it stick.