KISS
KISS — Keep It Simple, Stupid
Keep it simple, stupid. A slogan traditionally attributed to Kelly Johnson (Lockheed Skunk Works, 1960s), although the exact form and authorship are disputed. A modern engineering version: "complexity must be justified; simplicity is the default."
The core idea
The simplest solution that works is the best one.
Complexity is debt. Everyone who reads the code in the future pays for it (including you in 3 months).
Why it matters
| What complexity takes from you | Why |
|---|---|
| Reading time | More constructs mean more time to parse the code in your head |
| Editing time | Harder to predict what will break |
| Number of bugs | Higher complexity, more execution paths, higher chance of missing a case |
| Ability to adapt | A clever solution for the current task often does not fit the next one |
Donald Knuth: "Premature optimization is the root of all evil." The same can be said about premature cleverness.
Example 1: "clever" brevity vs readability
❌ Bad
is_even = lambda n: not bool(n & 1)
- A bitwise operation instead of an arithmetic one.
- The
bool()aroundn & 1is redundant. - A
lambdawithout a name assigned to a variable is an anti-pattern (PEP 8 explicitly recommendsdef). - No types.
✅ Good
def is_even(n: int) -> bool:
return n % 2 == 0
Reads out loud: "n is even if n modulo 2 equals zero." That's it.
Example 2: nested ternaries and combined expressions
❌ Bad
status = "active" if user.is_logged_in and not user.is_blocked else ("blocked" if user.is_blocked else "guest")
Try reading this on the first try.
✅ Good
if user.is_blocked:
status = "blocked"
elif user.is_logged_in:
status = "active"
else:
status = "guest"
More lines, but it reads top to bottom, without your eyes going back.
Example 3: magic numbers
❌ Bad
if response.status == 429:
time.sleep(60)
What is 429? What is 60? The reader has to google HTTP codes or read the API docs.
✅ Good
HTTP_TOO_MANY_REQUESTS = 429
RATE_LIMIT_COOLDOWN_SEC = 60
if response.status == HTTP_TOO_MANY_REQUESTS:
time.sleep(RATE_LIMIT_COOLDOWN_SEC)
Warning: constant names are documentation. If you name it COEF = 1.20, that is worse than a magic number, because it misleads.
Example 4: over-generalisation
❌ Bad
class GenericProcessor:
def __init__(self, strategy, validators=None, hooks=None, observers=None):
self.strategy = strategy
self.validators = validators or []
self.hooks = hooks or {}
self.observers = observers or []
def process(self, item, context=None, **kwargs):
...
All "for the future", when you just needed:
✅ Good
def process_order(order: Order) -> Receipt:
...
(This also violates YAGNI. KISS and YAGNI often go together.)
Example 5: one long function vs one long function plus many helpers
KISS does not mean "split everything into tiny functions". Over-splitting also hurts.
❌ Bad (over-splitting)
def calculate_total(qty, price):
return _multiply(_get_qty(qty), _get_price(price))
def _get_qty(q): return q
def _get_price(p): return p
def _multiply(a, b): return a * b
This is not "cleaner", this is seven eye jumps instead of one multiplication.
✅ Good
def calculate_total(qty: int, price: float) -> float:
return qty * price
Rule: extract a function when the block has a meaningful name (the level of abstraction goes up). If the name just retells what the body does, do not extract it.
The trap: KISS is NOT "less code"
Sometimes longer code is simpler. The classic example: guard clauses vs nested if.
❌ Shorter but harder
def discount(c):
if c is not None:
if c.tier == "gold":
if c.years > 5:
return 0.20
else:
return 0.15
return 0
✅ Longer but easier to read
def discount(c):
if c is None:
return 0
if c.tier != "gold":
return 0
if c.years > 5:
return 0.20
return 0.15
This is called early return or guard clauses. Less nesting means less cognitive load.
Rules of thumb
- If a colleague has to stop and think about one line, the line is too clever.
- The name explains the meaning. If you need a comment saying "what this does", rewrite the code instead of adding the comment.
- Cyclomatic complexity should stay around 10 per function (McCabe, 1976). Count all decision points:
if,elif,for,while,case,except, ternaries, andand/orinside boolean expressions. - The shortest expression is not always the simplest. Simplicity is about ease of reading, not brevity.
- No language feature is worth using just for the "wow" effect. Metaclasses, monkey-patching, heavy metaprogramming: only when you actually need them.
One-line summary
Defend simplicity, justify complexity. The reader of your code is your future client.