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.
- Constant context
- A native context requiring a permitted compile-time value computation. A read-only runtime binding is a separate property.
- Build profile
- Compiler options that affect the built program. The supplied source may also contain explicit diagnostic configuration.
- Diagnostic
- A tool or program message. Its effect on compilation or execution must be established separately.
- Application precondition
- A fact the program needs about the current request before it performs the associated operation.
When a program can establish a fact
Compile time is the phase in which tools analyze and translate source; runtime is the phase in which the prepared program executes. A build can check grammar, types and requirements for constant expressions. Input read during execution introduces facts that must be handled by the running program.
A successful build establishes only what its configured checks cover. Runtime validation decides whether a particular request is acceptable before the program changes state. Diagnostic assertions and warnings have their own behavior and configuration. Track the condition checked, the paths that reach it and what happens after a failed check rather than treating every check as the same guarantee.
01Source and input
Separate the carton template from the order
A warehouse station is adding a carton-dispatch command. Its calculator converts boxes to units using a fixed carton template. The command can preview one carton or commit a requested unit count. Before releasing it, review which facts the build checks and which requests the running command must refuse.
Start with the carton calculator. The selected template holds two units per box. This reduced program reads the number of boxes and prints the resulting unit count. Use valid integer input 2. Which quantity can change for the next order without changing the template declaration?
import { readFileSync } from "node:fs";
function readCount(): number {
return Number(readFileSync(0, "utf8").trim());
}
const perBox = 2;
const count = readCount();
console.log(count * perBox);
npx tsc main.ts --noEmit --skipLibCheck --target es2022 --module nodenext --types node && node main.ts
Standard input (escaped): "2\n"
02Native checking
Can the request use the template’s constant declaration?
A proposed port uses a native constant declaration for the input count as well as the template. The intended benefit is to reject invalid requests before the command runs. Inspect the actual declaration below: use input 2 if the displayed command reaches execution. Does this declaration accept a runtime read, and would that answer establish the station’s range rule?
import { readFileSync } from "node:fs";
function readCount(): number {
return Number(readFileSync(0, "utf8").trim());
}
const perBox = 2;
const count = readCount();
console.log(count * perBox);
npx tsc main.ts --noEmit --skipLibCheck --target es2022 --module nodenext --types node && node main.ts
Standard input (escaped): "2\n"
03Runtime obligation
Follow the ordinary dispatch past its diagnostic
The runtime input binding is restored. Now review the dispatch function with a valid parsed integer; parsing failures are outside this reproduction. The run first reads input 8 as an ordinary request in units. It then previews one carton using the fixed two-unit template, before handling the stored ordinary request. Both paths must enforce 0 through 6; only an accepted ordinary request may increase sent. The source includes a diagnostic before that increase. Inspect the exact command and source configuration before deciding whether it prevents the invalid dispatch.
import { readFileSync } from "node:fs";
function readCount(): number {
return Number(readFileSync(0, "utf8").trim());
}
const perBox = 2;
let sent = 0;
function dispatch(count: number, preview: boolean): string {
if (preview) {
if (count < 0 || count > 6) return "refused";
return "preview";
}
console.assert(count >= 0 && count <= 6, "count");
sent += count;
return "sent";
}
const count = readCount();
console.log(dispatch(perBox, true), sent);
console.log(dispatch(count, false), sent);
npx tsc main.ts --noEmit --skipLibCheck --target es2022 --module nodenext --types node && node main.ts
Standard input (escaped): "8\n"
Try one change
In a local copy of the dispatch program, make an invalid ordinary request produce refusal before the optional diagnostic and before sent changes. Keep accepted preview behavior intact. Run a valid ordinary count, an invalid ordinary count and an invalid preview using the same supported build profile. The worksheet checker assesses your evidence decisions; this local code change is a separate application to verify with the native command.
Check this idea later
In the frontend A number prop is not an accepted request range Optional
The station screen can receive a number outside the dispatch range. Both excerpts choose the displayed count or Refused using a runtime condition. The number annotation alone does not make that decision, and refusing a display does not validate the dispatch command’s ordinary path.
export default function Count({ count }: { count: number }) {
return <p>{count >= 0 && count <= 6 ? count : "Refused"}</p>;
}
The component evaluates the acceptance condition during rendering and selects the displayed value. The number annotation alone admits values outside this application range.
Sources: React conditional rendering,Svelte if blocks.
04Practice
Decide what is ready for the station
Complete the release review on three new candidates. Check a larger fixed carton template with changing order input, compare active and inactive diagnostics, then review a refusal patch after an accepted dispatch has already changed the counter. Record the source-based conclusions needed to protect that existing state.
Review the supplied complete sources and configuration changes. The new candidates use source/model predictions; their outcomes are not presented as captured native runs.
Review the dispatch candidates
Check the new carton, compare diagnostic configurations and protect previously committed units.
Sources
Sign in to keep your read marks on every device. Sign in
Was this helpful?