One page
Programming principles cheat sheet
What to keep at hand. If a line is not clear, go back to the matching lesson in the course.
Open the course: Programming principlesBasic rules
| What | How |
|---|---|
| DRY | One piece of knowledge, one place. Duplication is cheaper than the wrong abstraction. |
| KISS | The simplest solution that works. |
| YAGNI | Don't write code for requirements that don't exist yet. |
| Rule of three | Extract shared code on the third repetition, not the second. |
SOLID in one line each
| What | How |
|---|---|
| S: Single Responsibility | One reason to change per module. |
| O: Open/Closed | New behavior through new code, without editing the old. |
| L: Liskov Substitution | A subtype works wherever its parent works. |
| I: Interface Segregation | Narrow interfaces for a specific client. |
| D: Dependency Inversion | Business logic depends on interfaces, not on the database and HTTP. |
Code smells worth fixing
| What | How |
|---|---|
| Long switch on type | A strategy or a map of handlers (O). |
| Boolean flags in parameters | Split into several functions. |
| new Postgres() inside a service | Pass the dependency into the constructor (D). |
| A comment saying "what the code does" | Rename the variable or extract a function. |
| A method throws NotImplemented | Liskov is violated: revisit the hierarchy. |
| A 500 line class | Find the reasons for change and split it (S). |
Pattern map
| What | How |
|---|---|
| Strategy | Several ways to do one action: type Pricing = (base: number) => number |
| Factory | One place to choose the implementation: createNotifier(channel, config) |
| Observer | Reactions to an event without direct calls: emitter.subscribe(fn) |