The whole, before the parts

Programming is giving a machine instructions precise enough that it needs no judgment to follow them. One idea carries the subject: a program is built from small pieces whose behaviour you can state exactly, so that the whole can be understood one piece at a time. Everything below — control flow, functions, naming, debugging — is a technique for keeping each piece small enough to state, and for noticing when you no longer can. The habit worth more than any language feature is being able to say, before running the code, what it should do. Citation needed

For example, a program that totals a shopping basket must read prices, combine them according to a rule, and produce a result. Before implementing the steps, state what result is expected for a small basket and for an empty one. Citation needed

The concepts move from stored values and execution order to reusable pieces and readable names. Handling errors, stating what must remain true, and investigating failures then help check that the pieces still deliver the intended behavior. Citation needed

Terminology and core concepts

  1. State and assignment

    A running program is a set of named values — its state — and a variable is a name for one of them; assignment replaces the value a name refers to, which is why order of execution matters at all. Citation needed

    What is a program's state at a moment?
    the values of all its variables Citation needed
    What does assignment do to a variable?
    replaces its current value Citation needed
    After x = 3 then x = x + 1, what is x?
    4 Citation needed
    Why does swapping two assignments change a program?
    the second reads state the first wrote Citation needed
    What does it mean for a value to be immutable?
    it cannot be changed after creation Citation needed
  2. Control flow

    Three shapes compose every program: sequence (do this, then that), selection (do one branch or the other), and iteration (do it again while a condition holds). Citation needed

    What decides which branch of a selection runs?
    a condition evaluated at that moment Citation needed
    When does a while loop stop?
    when its condition becomes false Citation needed
    What causes an infinite loop?
    the body never falsifies the condition Citation needed
    How many times does a loop over indices 0 to n-1 run?
    n times Citation needed
    What is an off-by-one error?
    a loop bound wrong by exactly one step Citation needed
  3. Functions

    A function packages a computation behind a name, taking inputs as parameters and handing back a return value; the caller needs the name and the contract, not the body. Citation needed

    What does a caller need to know about a function?
    its name, parameters, and what it returns Citation needed
    What is a parameter?
    a named input a function receives Citation needed
    What is a side effect?
    any change beyond the return value Citation needed
    Why are side-effect-free functions easier to test?
    same inputs always give same output Citation needed
    What is the scope of a local variable?
    the function that declares it Citation needed
  4. Decomposition

    Decomposition is splitting a problem into functions so that each does one nameable thing; a function you cannot name in a short phrase is doing more than one. Citation needed

    What signals a function is doing too much?
    it has no short honest name Citation needed
    What does decomposition buy the reader?
    understanding one piece at a time Citation needed
    What is the cost of a function used only once?
    a name and a jump for the reader Citation needed
    When should repeated code become a function?
    when the copies must change together Citation needed
  5. Naming and readability

    Code is read far more often than it is written, so a name is an investment: it should say what the thing is for, not how it is stored. Citation needed

    Who is the main audience of source code?
    the next reader, not the machine Citation needed
    What should a variable name describe?
    its purpose, not its type Citation needed
    Which is the better name for a count of retries: n or retries?
    retries Citation needed
    What should a comment explain that code cannot?
    why, not what Citation needed
  6. Errors and failure paths

    Every operation that touches the outside world can fail, and the failure path is part of the program: handling means deciding, at each level, whether to recover, report, or stop. Citation needed

    Which operations must be assumed able to fail?
    any that touch files, networks, or input Citation needed
    What does an exception do when not caught?
    propagates up until something handles it Citation needed
    What is wrong with silently ignoring an error?
    the failure resurfaces later, far from its cause Citation needed
    Where should an error be handled?
    at the level that can actually decide what to do Citation needed
    What should an error message carry?
    what failed, with the values involved Citation needed
  7. Invariants and reasoning

    An invariant is a statement about state that holds at a chosen point every time execution passes it; a loop invariant held before and after each pass is how you argue a loop is correct without running it. Citation needed

    What is a loop invariant?
    a claim true before and after every iteration Citation needed
    What do the invariant and the exit condition together give?
    what is true when the loop ends Citation needed
    What is a precondition?
    what must hold before a function runs Citation needed
    What does an assertion do?
    stops the program when a claimed invariant fails Citation needed
  8. Debugging discipline

    Debugging is hypothesis testing against a running program: reproduce the failure, guess a cause, make the one observation that would refute it, and shrink the failing case until the bug has nowhere to hide. Citation needed

    What is the first step of any debugging session?
    reproduce the failure reliably Citation needed
    Why shrink a failing input?
    less code runs, so fewer places to look Citation needed
    What should a debugging observation be designed to do?
    refute one specific hypothesis Citation needed
    What does it mean when a bug vanishes under observation?
    the observation changed timing or state Citation needed
    What is the last step after fixing a bug?
    add a test that would have caught it Citation needed

Problems that prove it

  1. Trace this by hand: total = 0; for each i from 1 to 4, total = total + i × i. What is total at the end?

    Drills: tracking state through iteration without running the code.

    Derivation steps
    1. Start: total = 0.
    2. i = 1: total = 0 + 1 = 1.
    3. i = 2: total = 1 + 4 = 5.
    4. i = 3: total = 5 + 9 = 14.
    5. i = 4: total = 14 + 16 = 30.

    Citation needed

  2. State the loop invariant for summing an array: after processing the first k elements, what must be true of sum?

    Drills: using an invariant to argue a loop correct.

    Derivation steps
    1. Invariant: sum equals the total of elements 0 through k-1.
    2. Before the loop, k = 0 and sum = 0: the total of no elements. Holds.
    3. Each pass adds element k and increments k, restoring the claim.
    4. The loop exits when k = n; the invariant then says sum is the total of all n elements.

    Citation needed

Sources

  • Brian Kernighan and Rob Pike, The Practice of Programming — style, debugging, and design taught through working code.
  • Harold Abelson and Gerald Jay Sussman, Structure and Interpretation of Computer Programs — functions and abstraction as the substance of programming.
  • Steve McConnell, Code Complete — the encyclopedia of construction-level craft, naming included.

Learning path

Continue through Software Engineering in order, using the concepts above to connect this topic to the next.