The basics of the basics: what programming is made of
Strip away languages and frameworks and programming rests on a few ideas: building blocks, three ways to control flow, abstraction, and a loop of solving, testing, and practicing.
Languages come and go, and frameworks change every few years. Underneath them, the ideas that make up programming have barely moved in decades. This note collects those ideas in one place, with the sources they come from, so they are easy to come back to.
Programs are written for people
The classic MIT textbook Structure and Interpretation of Computer Programs (Abelson and Sussman) puts it plainly in its preface:
“Programs must be written for people to read, and only incidentally for machines to execute.”
A computer will run messy code just as happily as clean code. The difference shows up later, when a person (often your future self) has to understand it, change it, or find out why it broke. Every fundamental below serves that reader as much as the machine.
Three mechanisms: primitives, combination, abstraction
The same book opens its first chapter by saying that every powerful language has three mechanisms:
- Primitive expressions: “the simplest entities the language is concerned with”, such as numbers, text, and basic operations.
- Means of combination: “by which compound elements are built from simpler ones”.
- Means of abstraction: “by which compound elements can be named and manipulated as units”.
In code, all three appear in a few lines:
// primitives: numbers and the * operator
3 * 3;
// combination: building a bigger expression from smaller ones
(3 * 3) + (4 * 4);
// abstraction: giving the idea a name, then using it as a unit
function square(x) {
return x * x;
}
square(3) + square(4);
Once you can see these three moves, most code reads as variations on them: small pieces, combined, then named so they can be reused without thinking about their insides.
Three ways to control flow
How does a program decide what to do next? In 1966, Corrado Böhm and Giuseppe Jacopini published a result now known as the structured program theorem. It shows that any computable function can be expressed with just three control structures:
| Structure | What it means | Typical form |
|---|---|---|
| Sequence | Do one step, then the next | statements in order |
| Selection | Choose between steps based on a condition | if / else |
| Iteration | Repeat a step while a condition holds | while, for |
// sequence
let total = 0;
const prices = [12, 5, 30];
// iteration
for (const price of prices) {
// selection
if (price > 10) {
total = total + price;
}
}
Everything else a language offers, from switch to map to exceptions, is a more convenient way to write one of these three.
Data and state
Programs work on data. At the most basic level that means:
- Values of different types: numbers, text, true/false.
- Variables: names that point to values, so they can be used and changed later.
- Collections: lists and key–value maps that hold many values together.
The part that causes the most bugs is state, meaning values that change over time. In the loop above, total is state: its value depends on how many times the loop has run. Knowing at every point what each variable holds, and who is allowed to change it, is a large part of what programming actually is.
Decomposition and abstraction
Real problems are too big to hold in your head at once. In her 2006 essay Computational Thinking (Communications of the ACM), Jeannette Wing describes the core skill as:
“using abstraction and decomposition when attacking a large complex task or designing a large complex system.”
Decomposition means splitting a problem into smaller ones that can be solved separately. Abstraction means hiding the details of each part behind a name and a clear contract, as square does above. Functions, modules, and services are all the same idea at different sizes.
A useful test: if you can describe what a function does in one short sentence without mentioning how it does it, the abstraction is probably in the right place.
A loop for solving problems
Writing code is the last step of solving a problem, not the first. George Pólya’s How to Solve It (1945) was written about mathematics, but its four steps map closely onto programming:
- Understand the problem. What goes in, what should come out, and what are the edge cases?
- Make a plan. Break it down, and pick the data and control structures you’ll need.
- Carry out the plan. Write the code, one small piece at a time.
- Look back. Does it work, is it clear, and how could it be better?
Most of the time saved by experience comes from steps 1 and 2, not from typing faster in step 3.
Testing and debugging
A program that runs is not yet a program that is correct. In his 1972 Turing Award lecture, The Humble Programmer, Edsger Dijkstra warned:
“Program testing can be a very effective way to show the presence of bugs, but it is hopelessly inadequate for showing their absence.”
Tests are still essential. They just are not proof. Two habits follow from this:
- Test the edges: empty input, a single item, very large values, invalid data.
- Debug by narrowing down: reproduce the problem, form a guess about the cause, check it, and repeat until the guess is confirmed. Reading the error message carefully, all of it, solves more problems than it should.
Practice, for a long time
Finally, none of this sticks from reading alone. In Teach Yourself Programming in Ten Years, Peter Norvig argues that “it takes about ten years to develop expertise in any of a wide variety of areas”, and that the key is deliberate, focused practice. Among his recommendations:
- “Program. The best kind of learning is learning by doing.”
- “Talk with other programmers; read other programs.”
- “Learn at least a half dozen programming languages.”
Summary
- Write code for the next person who reads it.
- Build from primitives, combine them, and name the combinations.
- Control flow is sequence, selection, and iteration.
- Keep track of state: what each variable holds and who changes it.
- Decompose big problems and hide details behind clear abstractions.
- Understand, plan, build, look back.
- Tests find bugs; they don’t prove their absence.
- Practice deliberately, over years.
References
- Harold Abelson and Gerald Jay Sussman, Structure and Interpretation of Computer Programs, MIT Press. Preface and section 1.1, “The Elements of Programming”.
- Corrado Böhm and Giuseppe Jacopini, “Flow Diagrams, Turing Machines and Languages with Only Two Formation Rules”, Communications of the ACM 9(5), 1966. Summary: Structured program theorem.
- Jeannette M. Wing, Computational Thinking, Communications of the ACM 49(3), March 2006.
- George Pólya, How to Solve It, Princeton University Press, 1945. Summary: How to Solve It.
- Edsger W. Dijkstra, “The Humble Programmer”, Communications of the ACM 15(10), 1972.
- Peter Norvig, Teach Yourself Programming in Ten Years.