AQA 7517, section 4.13.1.3
Implementation is the stage where the design is turned into a working program. The data model becomes real data structures, and the planned algorithms are written as code: instructions in a programming language that the computer can run.
What happens in implementation
- The data model becomes data structures: variables, records, lists, files or database tables.
- The algorithms (pseudo-code or flowcharts) become code in a chosen programming language.
- The modules from the design are written, using the documented interfaces.
- The user interface is built.
- The program is run, tested and debugged as it is written.
Good practice when writing code
Well-written code is easier to debug, test and maintain.
- Use meaningful identifier names (4.1.1.2), such as
date_due. - Add comments that explain why, not just what.
- Break the code into small subroutines, each with one job, matching the design.
- Use local variables and parameters, and avoid globals.
- Validate input.
- Follow a consistent style, with indentation and layout.
- Use version control to keep a history of changes.
- Test each part as you go, instead of waiting until the end.
Debugging
Programs rarely work first time. Debugging is finding and fixing errors (bugs).
| Type of error | Cause | Example | How it shows |
|---|---|---|---|
| Syntax error | Breaking the rules of the language | A missing bracket or colon | The program won't run, and the translator reports it |
| Runtime error | Something goes wrong while running | Dividing by zero, a missing file | The program crashes |
| Logic error | The program runs but gives wrong results | Using < instead of <= |
No error message: the output is just wrong |
Logic errors are the hardest to find. Techniques include:
- Trace tables: following the code by hand, writing the values of variables at each step.
- Print statements: displaying values at points in the code to see what is happening.
- A debugger: running the program step by step, setting breakpoints to pause it, and inspecting variables.
- Dry runs with simple test data.
- Reading the code carefully, or explaining it to someone else.
Articulating how a program works
Writing, debugging and testing plenty of programs builds an important skill: being able to explain how a program works, and to convince someone that it is correct and efficient. There are three kinds of evidence:
- logical reasoning: explaining why each part does what it should;
- test data: showing the results of running it with carefully chosen inputs;
- user feedback: evidence that real users find that it does what they need.
An iterative approach: the critical path first
A program is rarely built all in one go. It is usually built iteratively, with prototyping or an agile approach, and the most important part, the critical path, is built first.
- The program is built in stages. Each stage produces a working version that can be tested and shown to users, and the next stage improves or extends it.
- The critical path is the set of features that are essential, or on which other parts depend, or that carry the greatest risk. These are tackled first, so that the core of the program works early and the biggest problems are found while there is still time to fix them.
- Less important extras ("nice to have" features) are added afterwards, if time allows.
Example: for the library system:
| Stage | What is built | Why |
|---|---|---|
| 1 | Store books and students; lend and return a book | The critical path: nothing else works without these |
| 2 | Report overdue books | An important requirement that depends on loans |
| 3 | Search by title or author | Useful, but the library can work without it |
| 4 | Email reminders, nicer interface | "Nice to have" |
At the end of each stage there's a working program, so if time runs short, the most important parts are done.
Worked example
Task: a student has 4 weeks to build a stock-control program. Describe how they could implement it iteratively, and how to argue that the first stage is correct.
- Week 1 (critical path): implement the data structures for stock items (a dictionary of records) and the subroutines to add stock and sell an item, which reduces the quantity. This is the essential core.
- Week 2: add a low stock report and saving to a file. Show the working program to the shopkeeper and note feedback.
- Week 3: improve the user interface based on the feedback, and add searching.
- Week 4: final testing, fixes and documentation.
To argue the first stage is correct: use logical reasoning (selling subtracts the amount from the quantity, and refuses if there is not enough stock), show test data (selling 3 of 10 leaves 7; selling 11 of 10 is refused), and give user feedback (the shopkeeper confirms it matches how they work).
Check yourself
What is implemented in the implementation stage?
The models and algorithms from analysis and design, as data structures and code that a computer can understand.
Give the three types of error, with an example of each
Syntax error (a missing bracket), runtime error (dividing by zero, causing a crash), and logic error (the program runs but gives wrong output, such as using < instead of <=).
What is meant by solving the critical path first?
Building the essential features, those that other parts depend on or that carry the most risk, first, so that the core of the program works early and problems are found in time.
What three things can be used to argue that a program is correct and efficient?
Logical reasoning, test data and user feedback.