Programming principles

Lesson 01 · 12 min

DRY, KISS, YAGNI

Three short rules that remove half of the extra code, and the case where DRY hurts.

12 minLesson 1 of 5Practice includedAsk AI on any point

Programming principles are not laws but compressed experience: what usually breaks a year later and how to avoid it. Let's start with the three shortest ones. They come up in every interview, and they are the ones most often applied wrong.

AI

You can ask about this point«Lesson summary»

DRY: don't repeat yourself

DRY (Don't Repeat Yourself) says that every piece of knowledge about the system should live in one place. It is about knowledge, meaning a business rule, a formula, a constant, not about identical lines of text.

Bad: the discount rule in three places
Good: the rule lives in one function

Now when marketing changes the discount threshold, you edit one line. In the first version you edit three places and are sure to forget the fourth.

AI

You can ask about this point«DRY: don't repeat yourself»

When DRY hurts: the wrong abstraction

Two pieces of code can look the same but mean different things. If you merge them, they start changing for different reasons, and the shared function grows flags.

Bad: a shared function with flags
AI

You can ask about this rule«Duplication is cheaper than the wrong abstraction»

AI

You can ask about this point«When DRY hurts: the wrong abstraction»

KISS: keep it simple

KISS (Keep It Simple, Stupid) asks you to pick the simplest solution that works. Simple code is easier to read, test and delete. Cleverness rarely pays off.

Bad: clever but unclear
Good: reads right the first time
AI

You can ask about this point«KISS: keep it simple»

YAGNI: you aren't gonna need it

YAGNI (You Aren't Gonna Need It) forbids writing code "for the future": a plugin system for one plugin, a setting nobody changes, a base class for a single child. You have to maintain that code right now, while the need it was written for often never shows up, or shows up in a different shape.

AI

You can ask about this rule«Write for today's task»

  • DRY is about knowledge, not identical lines.
  • KISS is about the next person understanding the code in a minute.
  • YAGNI is about not paying today for a feature that may never exist.
AI

You can ask about this point«YAGNI: you aren't gonna need it»

Check

Lock it in before you move on

  1. 1. What does DRY actually forbid?

  2. 2. A shared function has grown four boolean flags. What does that most likely mean?

  3. 3. Which option is closer to KISS?

  4. 4. You are asked to add CSV export. What violates YAGNI?

PracticeSolve the lesson tasksA few short tasks in the editor. Check your code right away and see where it goes wrong.Start practice