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 principles

Basic rules

WhatHow
DRYOne piece of knowledge, one place. Duplication is cheaper than the wrong abstraction.
KISSThe simplest solution that works.
YAGNIDon't write code for requirements that don't exist yet.
Rule of threeExtract shared code on the third repetition, not the second.

SOLID in one line each

WhatHow
S: Single ResponsibilityOne reason to change per module.
O: Open/ClosedNew behavior through new code, without editing the old.
L: Liskov SubstitutionA subtype works wherever its parent works.
I: Interface SegregationNarrow interfaces for a specific client.
D: Dependency InversionBusiness logic depends on interfaces, not on the database and HTTP.

Code smells worth fixing

WhatHow
Long switch on typeA strategy or a map of handlers (O).
Boolean flags in parametersSplit into several functions.
new Postgres() inside a servicePass the dependency into the constructor (D).
A comment saying "what the code does"Rename the variable or extract a function.
A method throws NotImplementedLiskov is violated: revisit the hierarchy.
A 500 line classFind the reasons for change and split it (S).

Pattern map

WhatHow
StrategySeveral ways to do one action: type Pricing = (base: number) => number
FactoryOne place to choose the implementation: createNotifier(channel, config)
ObserverReactions to an event without direct calls: emitter.subscribe(fn)

Other cheat sheets