Terminology (3)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.
- Short-circuit evaluation
- Establishing a logical result without evaluating an unnecessary operand.
- Eager argument
- A call argument computed before the called function or method chooses how to use it.
- Fallback
- An alternative candidate used under an explicit absence or validity policy.
Conditional evaluation
Short-circuit evaluation uses an earlier operand to decide whether a later operand is needed. A skipped expression produces no function call, state change or failure from that expression. This makes a logical expression part of the program’s control flow as well as a way to obtain a result.
The guarantee belongs to the operator or lazy operation being used. Moving a call into a temporary or an ordinary argument can make it run before a later choice. Track the selected value and the effects that occurred separately, including any work a helper has already done.
01Demanded work
The skipped side does not call its helper
A search dropdown previews a cached candidate before deciding whether it can select a row. Its provider supplies one fallback when needed. Position 0 is valid. The selected row can look right even when a disabled lookup ran, a cached zero was lost or a failed fallback was fetched twice.
The dropdown’s enablement probe records provider calls, so a disabled lookup can be checked for unwanted work. With enabled false, predict the boolean result and the number of helper calls.
let calls = 0;
function fetchValue(): number {
calls += 1;
return 9;
}
const enabled = false;
const eligible = enabled && fetchValue() >= 0;
console.log("eligible", eligible, "calls", calls);
npx tsc main.ts --noEmit --skipLibCheck --target es2022 --module nodenext --types node && node main.ts
Save one complete example at a time under the shown filename in a fresh folder. Run the shown command. The browser lab supplies its editor, input fixtures and runner.
02Values and eager arguments
A fallback can be chosen lazily or computed too soon
Inspect the complete native choice below. Predict its selected value and its call count separately; a plausible selected value does not establish which work ran.
let calls = 0;
function fetchValue(): number {
calls += 1;
return 9;
}
const cached = 0;
const chosen = cached || fetchValue();
console.log("picked", chosen, "calls", calls);
03Across helper calls
Equal values do not reveal which path supplied them
A cached invalid number and a freshly fetched invalid number can be equal. This complete example produces one of each and records the actual fetch count.
let calls = 0;
function fetchValue(): number {
calls += 1;
return -2;
}
const fromCache = -2;
const fromLookup = fetchValue();
console.log("same", fromCache === fromLookup, "calls", calls);
04Practice
Repair the dropdown lookup
The complete program reports an enabled probe, an unverified cache choice and a verified choice. Input, counters and previously correct helpers are supplied.
The supplied reader decodes three integers: enabled (0/1), cached (-1 alone means absent) and fallback. The unverified report preserves any present candidate for inspection; a verified result position must be nonnegative. Each report resets the counters so the current helper’s effects are visible.
Start the lab
Guard the probe, preserve a cached value, then avoid retrying work that a helper already performed.
In the frontend A default value can preserve zero and empty text. Optional
A component’s optional prop can have a meaningful empty value. Choose the default from the prop’s absence contract, and keep required work separate from conditional display. These excerpts select a heading; they make no claim about how often a framework renders.
export function Heading({ title }: { title: string | null }) {
const heading = title ?? "Untitled";
return <h1>{heading}</h1>;
}
The nullish fallback preserves an explicitly supplied empty title.
Sources: React: Conditional rendering,Svelte: $derived.
Sign in to keep your read marks on every device. Sign in
Was this helpful?