CSC 288 · Structured Programming
Our notes
2 min readWhat makes a program good
In order of importance for most work:
- Correct: it does what the specification says for every valid input, and handles invalid input sensibly.
- Readable and maintainable: another programmer (or you in six months) can understand and change it.
- Robust: it does not crash on unexpected input.
- Efficient: it uses reasonable time and memory.
Most of a program's cost comes after it is first written (fixing and changing it), so readability pays.
Where structured programming came from
In the 1960s large programs full of GOTO jumps ("spaghetti code") were hard to understand and full of errors. Two ideas changed this:
- Böhm and Jacopini (1966) showed that any program can be written with only three control structures: sequence, selection and iteration.
- Dijkstra's letter "Go To Statement Considered Harmful" (1968) argued that free jumps make programs hard to reason about.
Niklaus Wirth designed Pascal (1970) to teach these ideas.
The three structures
- Sequence: statements one after another.
- Selection: choose a path (if, case).
- Iteration: repeat (while, repeat, for).
Each has one entry and one exit, so blocks can be nested and read from top to bottom.
Rules of good style
- Meaningful names: totalScore, not t; constants named (const PassMark = 40;).
- Indentation shows structure; one statement per line.
- Comments explain why, not what the code obviously does.
- Small modules: a procedure does one job and fits on a screen.
- No magic numbers: write MaxStudents, not 250 scattered through the code.
- Avoid global variables; pass parameters.
- Check input and give clear messages.
- Consistent layout for begin and end.
Example: unclear and clear
Unclear:
if a>39 then writeln('P') else writeln('F');
Clear:
const PassMark = 40;
...
if score >= PassMark then
writeln('Pass')
else
writeln('Fail');
The second says what the number means and why the test is written that way.
Expressions
Write expressions so their meaning is obvious: use brackets when precedence may confuse a reader, (a + b) / 2 rather than relying on the reader to remember the rules, and avoid side effects inside expressions.
Exams usually ask
- State the characteristics of a good program.
- Explain structured programming and the three control structures.
- List rules of good programming style.
Common mistake
Thinking structured programming just means "no GOTO". The deeper point is single-entry, single-exit blocks that can be understood (and proved correct) one at a time.
Written by the UniHive team in our own words. Spotted a mistake? Tell us on WhatsApp.
No reading links here yet
Our team adds free books, videos and practice for each topic, and opens every link before it goes up.