Course map

Topic 001 / Core

Programming fundamentals

Chapter 1.1 · Programming, Data Structures, and Complexity

What and why

The front matter assumes you can already write basic programs; this topic does not re-teach programming. It fixes the small set of constructs the whole book speaks in — values and types, control flow, functions, composite data, errors, and state — because every later design decision eventually becomes code made of exactly these parts, and every large-system failure has a small-code ancestor here. Builds on: the Before-You-Start bridge (front matter).

Diagram

Where each construct returns at system scale:

In a program At system scale
A function: inputs → output, or an error A request/response handler (→ 10)
Choice of data structure Caches, indexes, queues (→ 2)
Cost of a loop Complexity under production load (→ 3)
Shared mutable state Race conditions (→ 7)
Error handling Deliberate failure behavior, everywhere

How it works

The six constructs, and what the book expects of each:

  1. Values and types — know what a thing is; most subtle bugs are type confusions (string vs. number, text vs. bytes, local time vs. UTC).
  2. Control flow — conditionals and loops; every loop has a cost that scales with its input (→ 3).
  3. Functions — the signature is a contract: what goes in, what comes out, what can fail. The book treats APIs as functions at a distance.
  4. Composite data — group what belongs together (records/objects, lists, maps); structure chosen well makes code simple (→ 2).
  5. Errors — distinguish expected failures (bad input, missing record) from unexpected ones (bugs); handle or propagate each deliberately — never silently swallow.
  6. State — know what each piece of data is: local and short-lived, or shared and long-lived. Shared mutable state is where correctness problems begin (→ 7).

Practical example

A money transfer in pseudocode — three of the six constructs doing real work:

function transfer(accounts, from_id, to_id, amount):
    if amount <= 0:                 return error("amount must be positive")
    if from_id not in accounts:     return error("unknown account: " + from_id)
    if to_id not in accounts:       return error("unknown account: " + to_id)
    if accounts[from_id] < amount:  return error("insufficient funds")
    accounts[from_id] -= amount     # shared state mutated —
    accounts[to_id]   += amount     # safe only while single-threaded (→ 7)
    return ok()

The contract is explicit (what can fail and how), and every check comes before any mutation — move the recipient check after the debit and a missing account leaves a half-finished transfer even with a single thread. The comment marks the exact line that stops being safe the moment two callers run at once — that single observation is the seed of Chapter 1.3 and, much later, of database concurrency control (→ 71).

When to use it

The judgment this topic teaches is restraint:

  • Use the simplest construct that works: a function before a class, a class before a framework, a plain map before a clever structure.
  • Avoid abstractions built for imagined future needs — the book returns to this as rule 7 (no forced complexity) at every scale.
  • Failure scenarios — the beginner bugs that return as production outages: boundary errors (off-by-one, empty input), missing-value errors (null where a value was assumed), shared mutable state (→ 7), and silently swallowed exceptions.
  • Simpler alternative: less code. Code that needs a comment to explain what it does should be rewritten until it doesn't.

Practice

(mechanism kind — build a tiny version; 15–30 min; no lab services — any language). Write a program that reads a text file of order lines (order_id,amount), prints the total per order sorted by amount, skips malformed lines but counts and reports them, and errors loudly if the file is missing. Then feed it an empty file, a file of only malformed lines, and a huge line — for each, say whether the behavior was designed or accidental. Done when nothing that happens surprises you.

Key takeaway

Programs are six constructs; systems are the same six at a distance, and sloppiness in any of them compounds with scale. Done when you can: write a small program whose every failure mode is deliberate, and point to the line where shared state would break it. Used next in → 2 (data structures), → 3 (complexity), → 7 (thread safety).

References

None — this topic makes no external claims.

Source directory

Search the guide