← Fundamentals
Fundamentals

Compile time and runtime

Which checks protect the dispatch counter?

Identify which facts the configured build checks and which each running request must establish.

Outcome

Identify what the configured build establishes and what each runtime path must establish before changing application state.

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?

main.ts
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
import { readFileSync } from "node:fs";

function readCount(): number {
    return Number(readFileSync(0, "utf8").trim());
}

const perBox = 2;

const count = readCount();

console.log(count * perBox);
TypeScript terminal.sh
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
npx tsc main.ts --noEmit --skipLibCheck --target es2022 --module nodenext --types node && node main.ts

Standard input (escaped): "2\n"

For the calculator with input 2, what did the source fix and what did this run obtain?

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?

main.ts
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
import { readFileSync } from "node:fs";

function readCount(): number {
    return Number(readFileSync(0, "utf8").trim());
}

const perBox = 2;

const count = readCount();

console.log(count * perBox);
TypeScript terminal.sh
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
npx tsc main.ts --noEmit --skipLibCheck --target es2022 --module nodenext --types node && node main.ts

Standard input (escaped): "2\n"

Which result follows from this exact declaration and command?

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.

main.ts
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
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);
TypeScript terminal.sh
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
npx tsc main.ts --noEmit --skipLibCheck --target es2022 --module nodenext --types node && node main.ts

Standard input (escaped): "8\n"

With ordinary input 8 under the shown configuration, which effect occurs?
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
A dispatch counter is already 4. A patch returns refused for request 9 but first resets the counter to 0. Does that satisfy refusal without changing committed state?
A preview of -1 returns refused while the counter stays 4. The ordinary -1 path has not been tried. What does the observation establish?
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.

Count.tsx
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
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.

  1. easy · F-PHASE-V4-1 Review the four-unit carton
  2. medium · F-PHASE-V4-2 Compare diagnostic configurations
  3. hard · F-PHASE-V4-3 Review a late refusal patch
Sources

Report a problem in this lesson

For typos, mistakes and things that don’t work. We read every report and fix what’s wrong, but we don’t reply here, so a question won’t get an answer.