Building Well-Structured Programs
Once code works, the next skill is keeping it understandable as it grows. Structured programming builds programs using only three fundamental control structures:
| Structure | Definition |
|---|---|
| Sequence | Statements run in order |
| Selection | Choosing a path with if/else |
| Iteration | Repeating with loops |
The structured program theorem proves these three suffice for any computation. Unrestricted jumps produce spaghetti code, an unmaintainable tangle. Structured programming ensures every block has one entrance and one exit.
Pitfall: Believing working code needs no structure. Structure is for future human understanding, not today's output.
Top-Down Design & Modularity
Good structure relies on top-down decomposition: breaking a large task into smaller sub-tasks or functions until each piece is simple. Two design virtues guide this process:
| Virtue | Description |
|---|---|
| High Cohesion | Each unit does one well-defined thing |
| Low Coupling | Units depend on each other as little as possible |
Together, high cohesion and low coupling let you change one part of a program without breaking others. Always measure code quality by human readability, single-entry blocks, and modularity.