Programming Practice · Lesson 1 of 2
Program Construction
Use a repeatable programming workflow: analyse, design, code, test and refine. Keep predicted results for normal, abnormal, extreme and boundary data. Use execution traces to connect the algorithm to the observed output.
By the end, you can
- Analyse a problem.
- Design an algorithm.
- Write pseudocode.
- Implement the solution.
- Test with normal, abnormal, extreme and boundary data.
- Debug errors.
- Refine and document the solution.
Core programming workflow
Practical programming has a workflow, and following it is what separates a solution that comes together from an afternoon of guessing.
Analyse first. Write down the inputs, the outputs, and the rules the program must follow, in your own words. If the requirement cannot be written in five lines, it is not yet understood well enough to code.
Design next: decompose into tasks, choose the data structures, and write pseudocode or draw a flowchart. Trace it with a small example. Everything here is cheap to change, and nothing later is.
Implement in small pieces, running the program after each one. Get the input working and echo it back. Add one calculation and check it. Add the loop. Add the output formatting. A program that has run correctly ten times during its construction has ten fewer places for an error to hide.
Test with a plan written in advance: normal data that should be accepted, abnormal data that should be rejected, extreme data at the largest and smallest acceptable values, and boundary data either side of each end of every valid range. Record the expected result before running, then the actual.
Debug by narrowing rather than by staring. Reproduce the fault with the smallest input, print or step through the values, find the first step where the actual value differs from the expected, and fix that. Retest everything afterwards, then tidy the code — better names, a comment where the logic is not obvious, repetition moved into a procedure — and run the tests one final time.
Worth remembering
- Analyse, design, implement in pieces, test to a plan, debug, refine.
- Run the program after every small addition rather than at the end.
- Write the expected results before running the tests.
Worked example
Build a mark classifier for marks 0 to 100 and test 50 (normal), −5 (abnormal), 0 and 100 (extreme), and −1 and 101 (just beyond the boundaries), then explain each result from the conditions.
Practice
Warm-up · not marked
Check your understanding
Answer in your own words, then compare with the worked answer. Nothing here is saved or scored.
Worked answer
The input and its expected result, so the actual output can be compared objectively.
Check your method, units and explanation against it.
Marked practice
Set 1 of 5 · easiest first · Checked as you go, with the worked answer. Sign in or create a free account to save your progress.