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.
- Source checkpoint
- A named before/after boundary in the supplied source. It is a worksheet location, not evidence that a debugger captured a pause.
- Invocation
- One execution of a function, with its own parameters, locals and return destination.
- Suspended caller
- A calling execution waiting for a callee result before its surrounding operation can finish.
- Output prefix
- The text produced by completed output statements at a chosen boundary. Later values do not rewrite that text. This worksheet does not measure when buffered text reaches a terminal.
State at an execution boundary
A trace records execution as a sequence of chosen boundaries. At each boundary, identify the operation that comes next, the values already established and the output already produced. A source line can contain several operations, so say whether the checkpoint is before or after the operation being discussed.
Each function invocation owns its parameters and local values. A caller can be waiting for a helper to return while the helper has already computed its own result. Nested calls can also have separate locals with identical names. Keep an entry for each active invocation and resume the waiting caller only when its call finishes.
01Next operation
Check the row before changing the fee calculation
A support engineer is documenting an order-pricing reproduction. The diagnostic reaches the expected final quote, but the worksheet says the fee helper still holds the old subtotal. Before changing the arithmetic, check whether the row describes the right moment and invocation.
The reduced diagnostic reads a base amount and one fee in integer minor units. Use valid input 2 4; input validation and overflow are outside this reproduction. The proposed P row says subtotal 2 and includes the add line in its output. P is immediately before the marked output statement. Which part of that row can the completed addition justify?
import { readFileSync } from "node:fs";
function add(base: number, amount: number): number {
const subtotal = base + amount;
// Checkpoint P: immediately before this output operation.
console.log("add", subtotal);
return subtotal;
}
function main() {
const [base, amount] = readFileSync(0, "utf8").trim().split(/\s+/).map(Number);
let total = base;
console.log("start", total);
total = add(base, amount);
console.log("done", total);
}
main();
npx tsc main.ts --noEmit --skipLibCheck --target es2022 --module nodenext --types node && node main.ts
Standard input (escaped): "2 4\n"
02Pending caller
A corrected helper row does not complete the caller assignment
The local row is corrected. The next proposed row copies that subtotal into main’s total at P. Keep the same input and source: main called add to obtain the right-hand side of an assignment. Has that assignment completed while add is still at P?
import { readFileSync } from "node:fs";
function add(base: number, amount: number): number {
const subtotal = base + amount;
// Checkpoint P: immediately before this output operation.
console.log("add", subtotal);
return subtotal;
}
function main() {
const [base, amount] = readFileSync(0, "utf8").trim().split(/\s+/).map(Number);
let total = base;
console.log("start", total);
total = add(base, amount);
console.log("done", total);
}
main();
npx tsc main.ts --noEmit --skipLibCheck --target es2022 --module nodenext --types node && node main.ts
Standard input (escaped): "2 4\n"
03Invocation identity
Name the fee-group invocation when the source line repeats
The full reproduction contains nested fee groups, so a source line alone is no longer enough to label a row. Use input 1 2 1: base 1, an outer fee 2 and an inner fee 1. The supplied setup builds that chain. quote logs its base and remaining group count, applies one fee, then calls quote on the inner group. An absent group returns the accumulated base. Review Q in the outer quote invocation, after its recursive child has returned but before its resume output.
import { readFileSync } from "node:fs";
function add(base: number, amount: number): number {
const subtotal = base + amount;
// Checkpoint P: immediately before this output operation.
console.log("add", subtotal);
return subtotal;
}
type FeeGroup = {
fee: number;
remaining: number;
inner: FeeGroup | null;
};
function quote(base: number, group: FeeGroup | null): number {
const remaining = group?.remaining ?? 0;
console.log("enter", base, remaining);
if (group === null) return base;
const child = quote(add(base, group.fee), group.inner);
// Checkpoint Q: immediately before this output operation.
console.log("resume", base, child);
return child;
}
function main() {
const [base, ...fees] = readFileSync(0, "utf8").trim().split(/\s+/).map(Number);
let group: FeeGroup | null = null;
for (let i = fees.length - 1; i >= 0; i--) {
group = { fee: fees[i], remaining: fees.length - i, inner: group };
}
let total = base;
console.log("start", total);
total = quote(base, group);
console.log("done", total);
}
main();
npx tsc main.ts --noEmit --skipLibCheck --target es2022 --module nodenext --types node && node main.ts
Standard input (escaped): "1 2 1\n"
04Condition checks
Keep the last condition check in the trace
A worksheet for an iterative pricing pass needs condition rows as well as value rows. It can process the same fees without nested calls. Start with total 4, fees [2, 1] and index 0. While index is less than the list length, add fees[index] to total and advance index. The condition is checked before each body execution and once more before exit. The final false check is a step even though no body follows it.
| Boundary | total | index |
|---|---|---|
| Condition true | 4 | 0 |
| Body complete | 6 | 1 |
| Condition true | 6 | 1 |
| Body complete | 7 | 2 |
| Condition false; exit | 7 | 2 |
This is an authored worksheet for the stated loop behavior, not a captured debugger session. Choose smaller boundaries when the distinction being investigated lies inside a body statement.
Try one change
In a local copy of the first program, move the final output statement in main to immediately before the assignment that calls add. Predict the complete output prefix at P, then run the displayed native command with the same input. Record the changed statement boundary and keep the helper’s locals separate from main’s destination.
Check this idea later
05Practice
Complete the support worksheet for another order
Finish the pricing reproduction’s worksheet for fresh orders. First locate a fee helper’s computed local. Then stop at an inner return while its parent is still waiting. Finally follow three fees, including a discount, to a helper that has not printed yet. A matching final quote cannot substitute for these intermediate rows.
Use the supplied source to correct the worksheet fields. The lab assesses source-based tracing decisions; its checkpoints are authored questions, not captured debugger pauses.
Review the pricing support note
The same nested-group program covers a single fee, an inner return and a three-fee order with a discount.
Sources
TypeScript
Sign in to keep your read marks on every device. Sign in
Was this helpful?