- Separate data from behavior
- Recognize sequence, decisions, and repetition
- Design single-purpose functions
The smallest model
Every program receives information, transforms it, and produces a result. Write those three parts before choosing a language.
A variable names data; a type limits meaningful operations; a function encapsulates a transformation.
- Input: what the program needs.
- Process: the applied rules.
- Output: the observable result.
function calculateTotal(price, quantity) {
const subtotal = price * quantity;
return subtotal * 1.21;
}Control flow
A condition chooses a path. A loop repeats a rule. Use them when the domain needs them, not to show that you know the syntax.
function canEnter(age, hasTicket) {
if (age < 18) return false;
return hasTicket;
}Functions people can understand
A good function has a concrete name, explicit inputs, and a predictable output. If it needs a very long explanation, it probably does more than one thing.
Practice
Write a function that receives prices and returns only those above 10,000 after tax.
Show a hint
Separate the tax calculation from filtering. Two small functions are easier to test.