beginner · 24 minute study
Turn a rule into a small, testable contract
Move from copying blocks of code to naming a useful operation with clear inputs, outputs and failure behavior.
Useful preparation · 1 prerequisite
Use this as a map, not a lock. If these ideas are familiar, continue.
- Names, values and shared objects — Before learning many keywords, learn to track what a name refers to and which changes are visible elsewhere. This prevents confusion in every later language and UI state lesson.
First, build a mental model
A function boundary is an agreement. The caller supplies valid inputs; the function supplies a result or an explicit failure. State which effects it performs so callers do not have to guess.
- Parameters make inputs visible; a returned value makes the result visible.
- A pure calculation returns the same result for the same inputs and avoids observable external side effects.
- A guard makes a precondition executable. A useful contract also states what zero, missing or invalid input means.
- Local variables belong to a call. A closure can retain access to surrounding bindings, which introduces an ownership and lifetime question.
- The exact signature syntax differs across languages; the responsibility boundary and examples of valid/invalid behavior transfer.
function lineTotal(unitPrice, quantity) {
if (!Number.isFinite(unitPrice) || unitPrice < 0) return null;
if (!Number.isInteger(quantity) || quantity < 0) return null;
return unitPrice * quantity;
}
console.log(lineTotal(4, 3));
console.log(lineTotal(4, -1));- Valid request
A unit price of 4 and quantity 3 pass the guards and produce 12.
- Invalid request
A negative quantity is rejected before multiplication.
- Caller obligation
The caller must handle null instead of pretending every call produces a valid total.
Production thinking
Take this beyond the example.
Use small contracts to make AI-generated code reviewable: list valid examples, rejection examples and side effects, then verify those claims. A function name is an intention, not evidence that its implementation is correct.
Sources and further reading
Your evidence
unseen is a local learning signal, not a credential or proof of production competence.
Explain the mental model, then explicitly acknowledge it. Opening a page is not evidence.
Supported retries can complete this lesson. Independent evidence and delayed review determine the skill stage.
Next lesson · Choose a data shape and follow the work it creates →