Terminology (4)Terms used in this lesson
Term on the front, definition on the back. Paste with tabs between fields and a new line between cards, or import the download in Anki as Basic.
- Side effect
- Anything a function does besides computing its result: changing outside state or arguments, printing, reading input.
- Pure function
- A function whose result depends only on its arguments and which has no side effects.
- Hidden input
- Outside state a function reads that is not one of its arguments.
- Short-circuit evaluation
- && and || skip their right side when the left side already decides the result.
Results, inputs and outward effects
A pure function computes its result from its arguments without side effects. Reading changing outside state introduces a hidden input; changing caller-visible data or performing I/O adds an effect. Local calculation state can change while the function remains pure, provided those changes do not escape.
To assess a call, inspect both its body and the path that reaches it. A signature can permit particular access without describing every effect, and conditional evaluation can skip a call entirely. Reusing a calculated answer and repeating an outward action require different decisions.
02Changed arguments
Changes to the cart persist into its next preview
The till can preview a cart repeatedly before checkout. totalWithTax adds 10% and sums two prices of 50, then the caller requests the same preview again on that cart. Predict both lines and account for what the second call receives.
totalWithTax writes prices[i] = prices[i] * 1.1.
function totalWithTax(prices: number[]): number {
let total = 0;
for (let i = 0; i < prices.length; i++) {
prices[i] = prices[i]! * 1.1;
total += prices[i]!;
}
return Math.round(total);
}
const cart = [50, 50];
console.log(totalWithTax(cart));
console.log(totalWithTax(cart));
03Effects in conditions
An effect on the right of && or || may never run
Before wiring checkout, the engineer reduces a conditional posting path to a charge counter. charge increments the counter and returns true. With items set to 0, predict the final count and any message from the condition.
charge changes a module-level counter.
let charges = 0;
function charge(): boolean {
charges += 1;
return true;
}
const items = 0;
if (items > 0 && charge()) {
console.log("charged");
}
console.log(`charges ${charges}`);
04Repair the café till
Quote from the arguments, audit every line, change the balance once
One café-till tool continues through three repairs. First make each quote depend only on the customer kind and amount it is given. Then make sure every order line is audited, whether or not it is accepted. Finally change the account balance once per line while still printing it and reporting an overdraft.
The lab opens in your reading language and has its own language control. Each file contains its imports, the reader, any supplied helpers and a complete main.
Repair the café till
Keep quotes independent of customer history, preserve auditing alongside acceptance, then post each balance change once.
In the frontend Derive a quote while rendering; commit it from an explicit event. Optional
A till preview can rerender while the operator changes the amount. Derive the displayed quote from the supplied amount and discount without posting an order. A button handler is the explicit boundary that invokes commit. This separates rendering frequency from intentional checkout actions.
These client-side excerpts receive commit from a client parent. They show the UI boundary only; service retries, idempotency and server/client transport are outside this example.
"use client";
type Props = { amount: number; discount: number; commit: (price: number) => void };
export function QuotePreview({ amount, discount, commit }: Props) {
const quoted = amount - discount;
return (
<>
<p>Quote {quoted}</p>
<button onClick={() => commit(quoted)}>Commit order</button>
</>
);
}
React can render again; the calculation stays free of outward writes. commit is invoked by the click handler rather than by rendering the component.
Sign in to keep your read marks on every device. Sign in
Was this helpful?