The program development life cycle
Nobody builds a large program in one go. A program development life cycle divides the work of producing a program into stages: analysis, design, coding, testing and maintenance. Each stage has its own activities and its own outputs.
What happens in each stage
Each stage hands something on, and the next stage cannot start without it. Name the five stages in order, and what each one passes on.
What must it do?
- Investigate the problem and the current system
- Consult the client
How will it work?
- Plan the modules, data, files and interface
- Plan the testing
Write the program
- Write the design in a programming language
- Find and correct syntax errors
Does it work?
- Run it with planned test data
- Compare actual results with expected results
Keep it useful
- Change it after it is in use: correct, adapt or improve it
| Stage | What happens | Main outputs |
|---|---|---|
| Analysis | The problem and the current system are investigated and the client is consulted, to agree what the program must do. | requirements specification |
| Design | The solution is planned: how the program will work, its data, files, interface and testing. | structure charts, pseudocode, test plan |
| Coding | The design is written in a programming language (implementation); syntax errors are found and corrected. | program code (modules) |
| Testing | The program is run with planned test data and the actual results are compared with the expected results. | test results, corrected code |
| Maintenance | The program is changed after it is in use: to correct, adapt or improve it. | updated program |
Why follow a life cycle?
- The project is easier to manage, plan and cost: the work is split into stages in a known order.
- Each stage ends with clear deliverables, so progress can be checked before the next stage starts.
A cycle, arranged by a model
- The arrow back to analysis makes it a cycle: when maintenance can no longer keep the program fit for purpose, a new version starts again at analysis.
- Life cycle models (waterfall, iterative, RAD) arrange these same stages in different ways.
- Testing is not left to the end: modules are tested as they are written, and again after every change.
1 Which stage produces the requirements specification?
2 In which stage are the actual results compared with the expected results?
3 Give one reason for following a life cycle.
- 1analysis
- 2testing
- 3the project is easier to manage, plan and cost; or each stage ends with clear deliverables, so progress can be checked
Activities and outputs of each stage
Watching staff at work and planning a screen layout both happen before any code is written, but in different stages. Each stage of the life cycle has its own activities, so once you know them, any activity you are given points to one stage.
The two stages with the most to learn both finish before any code is written: analysis finds out what is needed, design plans how it will work.
Analysis: find out what is needed
- Investigate the current system (and similar systems) and the problems with it.
- Interview managers and the staff who will use the system; run surveys or questionnaires; observe the current system in use.
- Carry out a feasibility study; decide which life cycle to use.
Produces: the problem definition and the requirements specification; interview notes and survey results.
Design: plan how it will work
- Algorithms: structure charts, flowcharts, pseudocode, structured English, state-transition diagrams, data-flow diagrams.
- Data: identifier tables, data and record structures, validation rules, file or database design, E-R diagrams; class (UML) diagrams.
- The user interface: input screens and report layouts.
- Testing: the test strategy and the test plan.
- The programming language, and the library routines to use.
Name the stage
Cover the Stage column, name the stage for each activity, then check.
| Activity | Stage |
|---|---|
| The client is interviewed about the current system | Analysis |
| A feasibility study is carried out | Analysis |
| The requirements specification is written | Analysis |
| An identifier table is produced | Design |
| Record and file structures are defined | Design |
| A structure chart is drawn | Design |
| The test plan is written | Design |
| Activity | Stage |
|---|---|
| An algorithm is implemented in a programming language | Coding |
| A compiler is used; syntax errors can occur | Coding |
| The IDE offers auto-complete as code is typed | Coding |
| A dry run is done with a trace table | Testing |
| The walkthrough method is used | Testing |
| The program is modified to run on new hardware | Maintenance |
| An error reported by users after release is corrected | Maintenance |
Name the requested output precisely
- For a named design output, give a structure chart, an identifier table or a test plan.
- Breaking a problem into modules, or specifying the program structure, is a design activity.
- Saying only “modules” does not name a design output.
1 Staff are observed using the current system. Which stage?
2 Input screens and report layouts are planned. Which stage?
3 The actual output of a test is recorded beside the expected output. Which stage?
4 A program is changed after release to follow a new tax law. Which stage?
- 1analysis
- 2design
- 3testing
- 4maintenance
The waterfall model
When everyone knows exactly what is wanted from the start, the stages can simply follow one another. The waterfall model is the traditional, linear life cycle: work flows down from one stage to the next, like water over a series of steps.
Principles
- The stages are done one at a time, in an agreed sequence.
- The next stage does not start until the current stage is complete.
- Each stage produces documents that are passed down to the next stage.
Benefits
- Simple to understand and manage: clearly defined stages, each with specific deliverables.
- All requirements are identified before coding, so the plan is clear.
- Cost and completion time are more predictable.
- Works well for small projects whose requirements are well understood and will not change.
Drawbacks
- No working software until late in the life cycle, so it is slow to reach users and there are no early prototypes.
- Difficult to cope with changing requirements: late changes mean returning to earlier stages.
- The client is involved mainly at the start and the end, so it does not suit a project that needs high client involvement and feedback.
- Modules are combined (integrated) near the end, so problems appear late.
1 What does the arrow down from Analysis to Design carry?
2 What does an arrow going up from Coding to Design mean?
3 A client wants to try the app early and change it as they go. Give one drawback of waterfall for this client.
- 1the requirements specification: the result of one stage is the input to the next
- 2more work is needed in design to complete coding
- 3there is no working software until late, so the client cannot try it early; late changes to requirements mean going back to earlier stages
Iterative and rapid application development
Clients often cannot say exactly what they want until they see something working. Iterative development and RAD both produce a working prototype at an early stage and expect the requirements to change as the client sees it. They differ in how the work is organised.
Iterative: repeated cycles, each producing a more complete version
RAD: prototypes built and reviewed in short cycles, in parallel, then integrated
Compare the two models
Questions ask for a principle, a benefit and a drawback of each, often set against waterfall.
| Iterative | Rapid application development (RAD) | |
|---|---|---|
| Principles |
|
|
| Benefits |
|
|
| Drawbacks |
|
|
Iterative
Principles
- Starts with a small subset of the requirements
- Each iteration designs, codes and tests a version
- The version is reviewed; feedback and more requirements go into the next iteration
Benefits
- A working model (prototype) is available at an early stage
- Design faults and misunderstandings are found sooner
- Changes to requirements cost less; smaller parts are easier to test and debug
Drawbacks
- More resources (time and people) may be needed
- Not all requirements are known at the start, so the design may need changing
- Hard to apply to a small program that cannot be split up
Rapid application development (RAD)
Principles
- Minimal detailed planning
- Prototypes of the modules built in parallel in short cycles, then integrated
- The client is involved throughout; used for time-critical development
Benefits
- Quicker development: modules worked on at the same time
- Prototypes early for the client to review
- Changing requirements easier to include; usable modules sooner; components can be reused
Drawbacks
- Difficult to estimate the cost and time; documentation often omitted
- The client must be available throughout and may keep changing their mind
- Only suits a system that can be split into modules
| Iterative | Rapid application development (RAD) | |
|---|---|---|
| Principles | Development starts with a small subset of the requirements. Each iteration designs, codes and tests a version; the version is reviewed and feedback and further requirements go into the next iteration until the system is complete. | Minimal detailed planning. Prototypes of the modules are built in parallel in short cycles, then integrated. The client is involved throughout, reviewing each prototype. Used for time-critical development. |
| Benefits | A working model (prototype) is available at an early stage, so design faults and misunderstandings are found sooner. Changes to requirements cost less. Smaller parts are easier to test and debug. | Quicker development: several modules are worked on at the same time. Prototypes early for the client to review. Changing requirements are easier to include. Usable modules are delivered sooner; components can be reused. |
| Drawbacks | More resources (time and people) may be needed. Not all requirements are known at the start, so the design may need changing later. Hard to apply to a small program that cannot be split up. | Difficult to estimate the cost and time to complete. Documentation is often omitted. The client must be available throughout, and may keep changing their mind. Only suits a system that can be split into modules. |
Iterative
Principles: Development starts with a small subset of the requirements. Each iteration designs, codes and tests a version; the version is reviewed and feedback and further requirements go into the next iteration until the system is complete.
Benefits: A working model (prototype) is available at an early stage, so design faults and misunderstandings are found sooner. Changes to requirements cost less. Smaller parts are easier to test and debug.
Drawbacks: More resources (time and people) may be needed. Not all requirements are known at the start, so the design may need changing later. Hard to apply to a small program that cannot be split up.
Rapid application development (RAD)
Principles: Minimal detailed planning. Prototypes of the modules are built in parallel in short cycles, then integrated. The client is involved throughout, reviewing each prototype. Used for time-critical development.
Benefits: Quicker development: several modules are worked on at the same time. Prototypes early for the client to review. Changing requirements are easier to include. Usable modules are delivered sooner; components can be reused.
Drawbacks: Difficult to estimate the cost and time to complete. Documentation is often omitted. The client must be available throughout, and may keep changing their mind. Only suits a system that can be split into modules.
1 Which model develops prototype modules in parallel?
2 Give one benefit of iterative development compared with waterfall.
3 Give one drawback of RAD.
- 1RAD
- 2a working prototype is available at an early stage, whereas waterfall has no working software until late
- 3the cost and time are difficult to estimate; or the client must be available throughout
Choosing a life cycle
No life cycle is best for every project. Choose one from the scenario: are the requirements fixed or likely to change, and how soon does the client need something working?
Each model in one small picture, with the kind of project it suits. The Choose when line is the fact you look for in a scenario.
Waterfall
Choose when: Requirements are fixed and agreed at the start.
Iterative
Choose when: Requirements are unclear or will change; a prototype is wanted early.
RAD
Choose when: A tight deadline; the system splits into modules; the client is available throughout.
Why different life cycles are needed
The choice depends on the approach the project needs:
- requirements agreed at the start or likely to change
- prototypes or a rapid first version needed
- the size and complexity of the problem
- the skills and size of the team
- the budget, time and resources
- how much the client can be involved
Why different life cycles are needed
The choice depends on the approach the project needs: whether the requirements are agreed at the start or likely to change; whether prototypes or a rapid first version are needed; the size and complexity of the problem; the skills and size of the team; the budget, time and resources; how much the client can be involved.
Compare the three models on the same points
Set side by side, point by point, the models show where each one is strong. The first row uses the same stage names as the life cycle.
| Waterfall | Iterative | RAD | |
|---|---|---|---|
| How the stages run | analysis, design, coding, testing: once each, in order | design, code, test repeated for each version | design, code, test for each prototype, in parallel |
| Requirements | all agreed at the start | a subset first; more added each iteration | an outline; refined from prototypes |
| First working software | late, after testing | a working prototype at an early stage | prototypes at an early stage |
| Client involvement | mainly at the start and end: unsuited to high involvement | reviews each version | throughout: reviews every prototype |
| Changing requirements | hard to include late | included in the next iteration | included as prototypes are reviewed |
| Cost and time | most predictable | measured iteration by iteration | hardest to estimate |
Waterfall
How the stages run: analysis, design, coding, testing: once each, in order
Requirements: all agreed at the start
First working software: late, after testing
Client involvement: mainly at the start and end: unsuited to high involvement
Changing requirements: hard to include late
Cost and time: most predictable
Iterative
How the stages run: design, code, test repeated for each version
Requirements: a subset first; more added each iteration
First working software: a working prototype at an early stage
Client involvement: reviews each version
Changing requirements: included in the next iteration
Cost and time: measured iteration by iteration
RAD
How the stages run: design, code, test for each prototype, in parallel
Requirements: an outline; refined from prototypes
First working software: prototypes at an early stage
Client involvement: throughout: reviews every prototype
Changing requirements: included as prototypes are reviewed
Cost and time: hardest to estimate
| Waterfall | Iterative | RAD | |
|---|---|---|---|
| Requirements | all agreed at the start | a subset first; more added each iteration | an outline; refined from prototypes |
| First working software | late, after testing | a working prototype at an early stage | prototypes at an early stage |
| Client involvement | mainly at the start and end: unsuited to high involvement | reviews each version | throughout: reviews every prototype |
| Changing requirements | hard to include late | included in the next iteration | included as prototypes are reviewed |
| Cost and time | most predictable | measured iteration by iteration | hardest to estimate |
Justify a choice from the scenario
Each choice is a chain: a fact from the scenario, the feature of the model, the result.
Ticket app: early launch
Museum app: unsettled interface
Weighing machine: fixed rules
1 A bank’s interest rules are set by law and will not change. Which model fits, and why?
2 A start-up needs a working app in six weeks and expects to change features after users try it. Which model fits, and why?
3 An answer says only that RAD is faster. Why does it earn little?
- 1waterfall: the requirements can be agreed at the start, so cost and time are more predictable
- 2RAD: prototype modules are built in parallel, so usable modules arrive quickly, and users review each prototype
- 3it is not linked to the scenario: say which fact in the scenario makes speed matter
Structure charts: purpose and symbols
Once a problem is split into modules, the team needs a map of how they fit together. A structure chart shows the modules of a program, which module calls which, and the parameters passed between them.
Purpose of a structure chart
- Drawn at the design stage, it shows how a problem is broken down into modules: the hierarchy of modules, which module calls which, and any selection or repetition of calls.
- It shows the parameters passed between modules (the interface), and so whether each module is a function or a procedure.
The seven symbols
Every symbol carries one fact about how the modules work together, so each must be drawn exactly.
| Symbol | Meaning |
|---|---|
| Module (a procedure or a function). A line joins a module to each module it calls: the calling module is above, the called module below. | |
| A parameter passed down to the called module (by value). | |
| A value passed back up to the calling module: the return value, so the called module is a function. | |
| A filled circle marks a flag: a BOOLEAN value, passed down or returned. | |
| A two-way arrow: the parameter is passed BYREF. Its value goes down and the changed value comes back up. | |
| Selection (diamond): the module calls one of the modules below; which one is decided when the program runs. | |
| Iteration (curved arrow): the module repeatedly calls the modules below, in order from left to right. |
Module
Module (a procedure or a function). A line joins a module to each module it calls: the calling module is above, the called module below.
Data passed down
A parameter passed down to the called module (by value).
Return value
A value passed back up to the calling module: the return value, so the called module is a function.
Boolean flag
A filled circle marks a flag: a BOOLEAN value, passed down or returned.
By reference
A two-way arrow: the parameter is passed BYREF. Its value goes down and the changed value comes back up.
Selection
Selection (diamond): the module calls one of the modules below; which one is decided when the program runs.
Iteration
Iteration (curved arrow): the module repeatedly calls the modules below, in order from left to right.
The last two symbols are the hardest to read. In the Quiz chart they mean:
Selection in Feedback
The diamond chooses Praise or ShowHint when the program runs.
Iteration in Quiz
The curved arrow repeats Ask, then Check, then Feedback in that order.
1 Function GetMark is called by module Report. Which way does the arrow for its return value point?
2 What does a filled circle on a parameter arrow show?
3 A parameter arrow has a head at both ends. How is that parameter passed?
- 1up, from GetMark to Report: a return value goes back to the calling module
- 2a flag: a BOOLEAN value
- 3BYREF: its value goes down and the changed value comes back up
Constructing a structure chart
A program described in words hides its shape: which module calls which, and what passes between them. A chart shows it at a glance. Build it in order: a box for each module, a line for each call, then an arrow for every parameter.
The notes are the quiz program; the steps turn them into a chart. Work through the steps with the notes beside you, then compare your chart with the one below.
Program notes
- Quiz repeatedly calls Ask, then Check, then Feedback.
- Ask takes the question number QNum and returns the Answer typed (a STRING).
- Check takes QNum and Answer and returns Correct (a BOOLEAN).
- Feedback takes Correct, QNum and Score (passed by reference). It calls either Praise or ShowHint.
- Praise takes Score by reference; ShowHint takes QNum.
Steps
The Quiz chart
Each branch keeps one colour. Check every arrow against the program notes.
- Ask returns Answer to Quiz, which passes it to Check.
- Check returns Correct, which then controls Feedback.
- The two BYREF links pass the same Score through Feedback to Praise, so an increment reaches Quiz rather than being lost in a local copy.
1 Which modules can Feedback call, and how is one chosen?
2 Which parameter does Quiz pass to Feedback BYREF?
3 Check returns Correct. Why is the circle on its arrow filled?
- 1Praise or ShowHint: one of them, chosen when the program runs (selection, the diamond)
- 2Score
- 3Correct is a BOOLEAN, a flag
From structure chart to pseudocode
A finished structure chart already gives you most of the code's first lines. Each box on the chart becomes a module, and its arrows decide the header: FUNCTION or PROCEDURE, and which parameters are BYREF.
Read each rule on the left, then find it in the code: every one ends with the module where it shows in Quiz or Feedback.
Each box becomes a module; read each symbol as code
- An arrow up (a value returned):
FUNCTION … RETURNStype, used in an expression: Ask, Check. - No single return arrow up:
PROCEDURE, run withCALL: Quiz, Feedback, Praise, ShowHint. - A two-way arrow: a
BYREFparameter of a procedure: Score. Other parameters are passed by value, the default;BYVALmay be written. - A filled circle: the value is
BOOLEAN: Correct. - A diamond: an
IF(orCASE) chooses which module to call: in Feedback. - A curved arrow: a loop around the calls, made left to right: the
FORloop in Quiz.
PROCEDURE Quiz() DECLARE QNum, Score : INTEGER DECLARE Answer : STRING DECLARE Correct : BOOLEAN Score ← 0 FOR QNum ← 1 TO 3 Answer ← Ask(QNum) Correct ← Check(QNum, Answer) CALL Feedback(Correct, QNum, Score) NEXT QNum OUTPUT "Score: ", Score ENDPROCEDURE PROCEDURE Feedback(Correct : BOOLEAN, QNum : INTEGER, BYREF Score : INTEGER) IF Correct = TRUE THEN CALL Praise(Score) ELSE CALL ShowHint(QNum) ENDIF ENDPROCEDURE
1 Ask has an arrow up for Answer, a STRING. Write its header.
2 Praise has a two-way arrow for Score. Write its header.
3 Why is Feedback run with CALL and Check is not?
- 1
FUNCTION Ask(QNum : INTEGER) RETURNS STRING - 2
PROCEDURE Praise(BYREF Score : INTEGER) - 3Feedback has no return arrow up, so it is a procedure and is run with CALL; Check returns a value, so it is a function used in an expression:
Correct ← Check(QNum, Answer)
Implementing and tracing the called modules
Once the quiz modules are written, a trace shows whether they really pass data the way the chart planned. Run the quiz with three answers and follow Score: Praise adds 1 to it BYREF, so the change reaches Quiz.
Ask returns the typed answer and Check returns a BOOLEAN. Key and Hint are the global arrays below.
FUNCTION Ask(QNum : INTEGER) RETURNS STRING DECLARE Answer : STRING OUTPUT "Question ", QNum INPUT Answer RETURN Answer ENDFUNCTION FUNCTION Check(QNum : INTEGER, Answer : STRING) RETURNS BOOLEAN RETURN TO_UPPER(Answer) = Key[QNum] ENDFUNCTION PROCEDURE Praise(BYREF Score : INTEGER) Score ← Score + 1 OUTPUT "Correct" ENDPROCEDURE PROCEDURE ShowHint(QNum : INTEGER) OUTPUT "Hint: ", Hint[QNum] ENDPROCEDURE // main program CALL Quiz()
Original quiz data
| QNum | Key | Hint |
|---|---|---|
| 1 | RAM | main memory |
| 2 | CPU | it executes instructions |
| 3 | ROM | read-only memory |
Score after Feedback
| QNum | Answer | Correct | Score |
|---|---|---|---|
| 1 | ram | TRUE | 1 |
| 2 | GPU | FALSE | 1 |
| 3 | Rom | TRUE | 2 |
Question 1 Correct Question 2 Hint: it executes instructions Question 3 Correct Score: 2
Trace the run
Each call has three things to watch: what goes in, what comes back, and what changes Score.
| Line | Statement that runs | QNum | Answer | Correct | Score | Output | Writes | What happens |
|---|---|---|---|---|---|---|---|---|
| Quiz | FOR QNum ← 1 TO 3 | 1 | QNum 1 | the first pass | ||||
| the first pass | ||||||||
| Quiz | Answer ← Ask(QNum) | "ram" | Answer "ram" | Ask outputs "Question 1", inputs ram and returns it | ||||
| Ask outputs "Question 1", inputs ram and returns it | ||||||||
| Quiz | Correct ← Check(QNum, Answer) | TRUE | Correct TRUE | Check returns TO_UPPER("ram") = Key[1]: "RAM" = "RAM" | ||||
| Check returns TO_UPPER("ram") = Key[1]: "RAM" = "RAM" | ||||||||
| Feedback | IF Correct = TRUE THEN | test TRUE: Feedback calls Praise | ||||||
| test TRUE: Feedback calls Praise | ||||||||
| Praise | Score ← Score + 1 | 1 | Score 1 | Score is BYREF: Praise changes Quiz’s own Score, 0 + 1 | ||||
| Score is BYREF: Praise changes Quiz’s own Score, 0 + 1 | ||||||||
| Praise | OUTPUT "Correct" | Correct | Output Correct | |||||
The whole run
Now all three passes, one row each. Score changes only on a pass where Check returns TRUE.
| QNum | Answer | Correct | Score | Output | What decided it | |
|---|---|---|---|---|---|---|
| start | 0 | Quiz sets Score ← 0 | ||||
Quiz sets Score ← 0 | ||||||
| QNum 1 | 1 | "ram" | TRUE | 1 | Question 1Correct | TO_UPPER("ram") = "RAM": TRUE, so Praise adds 1 to Quiz’s Score |
| TO_UPPER("ram") = "RAM": TRUE, so Praise adds 1 to Quiz’s Score | ||||||
| QNum 2 | 2 | "GPU" | FALSE | Question 2Hint: it executes instructions | TO_UPPER("GPU") ≠ "CPU": FALSE, so ShowHint; Score is unchanged | |
| TO_UPPER("GPU") ≠ "CPU": FALSE, so ShowHint; Score is unchanged | ||||||
| QNum 3 | 3 | "Rom" | TRUE | 2 | Question 3Correct | TO_UPPER("Rom") = "ROM": TRUE, so Praise adds 1 to Quiz’s Score |
| TO_UPPER("Rom") = "ROM": TRUE, so Praise adds 1 to Quiz’s Score | ||||||
1 The answers typed are now rom, cpu, RAM. What is Score after each pass?
2 What is output on the last line for these answers?
3 If Praise took Score BYVAL, what would the original run output at the end? Why?
- 10, 1, 1: only cpu matches its key (CPU)
- 2
Score: 1 - 3
Score: 0: Praise would add 1 to its own copy, and Quiz’s Score would stay 0
State-transition diagrams
Press Start on a microwave and what happens depends on what it was doing already. A state-transition diagram shows the states a system can be in and the events that move it between them. In this microwave, Start begins cooking from Idle but resumes cooking from Paused.
Start at the dot beside Idle and follow the arrows. Each label gives the event first, then after the bar what the microwave does.
Purpose
- It documents an algorithm whose response to an input depends on its current state.
- It shows every state, the events (inputs) that cause each transition, and any outputs, so missing or impossible transitions can be found before coding.
States and events
- A state is a mode or condition the system stays in until something happens: “standby mode”, “cooking”, “alarm sounding”.
- An event triggers a transition, which may change the state or leave it unchanged. Name the actual event: Start pressed, Door opened or Timer ends.
The same design as a state-transition table
A table holds the same design, one row for each arrow, with each state in the same colour as on the diagram. A question may give either one and ask for the other.
| Current state | Event (input) | Output | Next state |
|---|---|---|---|
| Idle | Start pressed | heater on | Cooking |
| Cooking | +30 s pressed | add 30 s | Cooking |
| Cooking | Door opened | heater off | Paused |
| Current state | Event (input) | Output | Next state |
|---|---|---|---|
| Cooking | Timer ends | beep | Idle |
| Paused | Start pressed | heater on | Cooking |
| Paused | Stop pressed | clear timer | Idle |
1 The oven is in Paused. Which event takes it to Idle, and what is output?
2 Which transition leaves the state unchanged?
3 How many arrows does the diagram need to match the table?
- 1Stop pressed, output: clear timer
- 2+30 s pressed, a loop on Cooking
- 36: one arrow per row
Tracing a state-transition diagram
A state-transition diagram can be run by hand, like a program. Start in the start state and follow one arrow for each input. Each step starts in the state the previous step ended in.
Trace the inputs B, A, B, A, A, A, B
One row for each input. Each row starts in the state the row above ended in.
| Current state | Input | Output | Next state | Which arrow, and why | |
|---|---|---|---|---|---|
| input 1 | S1 | B | X1 | S1 | a loop: input B keeps S1; output X1 |
| a loop: input B keeps S1; output X1 | |||||
| input 2 | S1 | A | none | S2 | input A moves S1 to S2; no output |
| input A moves S1 to S2; no output | |||||
| input 3 | S2 | B | none | S1 | input B moves S2 to S1; no output |
| input B moves S2 to S1; no output | |||||
| input 4 | S1 | A | none | S2 | input A moves S1 to S2; no output |
| input A moves S1 to S2; no output | |||||
| input 5 | S2 | A | X2 | S3 | input A moves S2 to S3; output X2 |
| input A moves S2 to S3; output X2 | |||||
| input 6 | S3 | A | X1 | S3 | a loop: input A keeps S3; output X1 |
| a loop: input A keeps S3; output X1 | |||||
| input 7 | S3 | B | X3 | S4 | input B moves S3 to S4; output X3; the halting state, so the trace stops |
| input B moves S3 to S4; output X3; the halting state, so the trace stops | |||||
The diagram answers more than one trace can: count its arrows, find where it halts, or find the shortest route there.
Answers read from the diagram
- Transitions that lead to a different state: 4.
- Transitions with an output: 4.
- Halting state: S4 (double circle).
- Fewest inputs from S1 to S4: A, A, B.
- Outputs for input B A B A A A B: X1, X2, X1, X3.
| Step | Current state | Input | Output | Next state |
|---|---|---|---|---|
| 1 | S1 | B | X1 | S1 |
| 2 | S1 | A | none | S2 |
| 3 | S2 | B | none | S1 |
| 4 | S1 | A | none | S2 |
| 5 | S2 | A | X2 | S3 |
| 6 | S3 | A | X1 | S3 |
| 7 | S3 | B | X3 | S4 |
Answers read from the diagram
- Transitions that lead to a different state: 4.
- Transitions with an output: 4.
- Halting state: S4 (double circle).
- Fewest inputs from S1 to S4: A, A, B.
- Outputs for input B A B A A A B: X1, X2, X1, X3.
The same diagram as a state-transition table: one row for each arrow
Writing the table checks your reading of the diagram: every arrow, loops included, becomes one row.
| Current state | Input | Output | Next state |
|---|---|---|---|
| S1 | A | none | S2 |
| S1 | B | X1 | S1 |
| S2 | A | X2 | S3 |
| Current state | Input | Output | Next state |
|---|---|---|---|
| S2 | B | none | S1 |
| S3 | A | X1 | S3 |
| S3 | B | X3 | S4 |
1 Start in S1 and input A, A, B. List the outputs and the final state.
2 Start in S1 and input B, B, A. Which state does it end in?
3 Which arrows leave S2, and where do they go?
- 1X2, X3; it ends in S4
- 2S2: B and B loop on S1 (output X1 each time), then A moves to S2
- 3input A to S3; input B to S1
Syntax, logic and run-time errors
Programs go wrong in three different ways, and each shows up at a different time. A syntax error stops the code being translated, a logic error gives the wrong result, and a run-time error stops the program while it runs.
Learn each type with how it is caught: the compiler finds one, testing finds the other two. The last column points to the three listings below.
| Type | What it is | How it is found | Worked example below |
|---|---|---|---|
| Syntax error | breaks the rules (grammar) of the language | the compiler or interpreter reports it; the code cannot be translated | line 5: OUTPT is a misspelt keyword |
| Logic error | the algorithm is wrong: it runs but does not give the expected result | testing: actual and expected results differ; a dry run locates it | line 3: > should be >=, so it outputs 2, not 3 |
| Run-time error | an illegal operation while running: it crashes or freezes | testing with data that triggers it; prevented by checking values first | line 5: with Count = 0 it divides by zero |
Other examples. Syntax: unmatched brackets; NEXT closing a WHILE loop. Logic: a wrong loop count; a wrong starting value; decisions in the wrong order. Run-time: an index out of range; converting a non-numeric string; an endless loop.
| Type | What it is | Examples | How it is found |
|---|---|---|---|
| Syntax error | A statement does not follow the rules (grammar) of the language. | a misspelt keyword; unmatched brackets; NEXT closing a WHILE loop | reported by the compiler or interpreter; a program that compiles has none |
| Logic error | The algorithm itself is wrong: the program runs but does not give the expected result. | a wrong operator; a wrong loop count; a wrong starting value; decisions in the wrong order | testing: actual and expected results differ; a dry run locates it |
| Run-time error | While running, the program attempts an illegal operation, so it crashes or freezes. | dividing by zero; an index out of range; converting a non-numeric string; an endless loop | testing with data that triggers it; prevented by checking values first |
One example of each
Find the error in each listing before you read the correction below.
1Total ← 0 2FOR Count ← 1 TO 3 3Total ← Total + Count 4NEXT Count 5OUTPT "Total: ", Total
1Passes ← 0 2FOR Index ← 1 TO 4 3IF Mark[Index] > 50 THEN 4Passes ← Passes + 1 5ENDIF 6NEXT Index 7OUTPUT Passes
1Sum ← 0 2FOR Index ← 1 TO Count 3Sum ← Sum + Mark[Index] 4NEXT Index 5OUTPUT Sum / Count
What happens, and the correction
Check your answers here: each row says what goes wrong, then the corrected statement and the output it now gives.
| Error | What happens | Correction | Output now |
|---|---|---|---|
| Syntax, line 5 | OUTPT is not a keyword, so the code cannot be translated | OUTPUT "Total: ", Total | Total: 6 |
| Logic, line 3 | marks 50, 72, 38, 61; a pass is 50 or more, but it outputs 2 | IF Mark[Index] >= 50 THEN | 3 |
| Run-time, line 5 | with Count = 0 it divides by zero and stops | IF Count > 0 THEN OUTPUT Sum / Count ELSE OUTPUT "No marks" ENDIF | No marks |
Syntax, line 5
OUTPT is not a keyword: the listing cannot be translated.
OUTPUT "Total: ", Total
Output: Total: 6
Logic, line 3
A pass is 50 or more. For 50, 72, 38, 61, the original test outputs 2.
IF Mark[Index] >= 50 THEN
Output now: 3
Run-time, line 5
Count = 0 would divide by zero. Test Count before calculating the mean.
IF Count > 0 THEN OUTPUT Sum / Count ELSE OUTPUT "No marks" ENDIF
Output for Count = 0: No marks
1 A program outputs 9 when the expected result is 10. Which type of error is this most likely to be?
2 IF Score > 10 THEN is typed as IF Score > 10 THN. Which type of error?
3 A program stops when it reads Item[11] from an array declared [1:10]. Which type of error?
- 1logic: it runs but does not give the expected result
- 2syntax: THN is not a keyword
- 3run-time: the index is out of range
Locate, identify and correct errors
This function looks finished, but it crashes, and its count is wrong too. InRange() should count the temperatures in the global array Temps[1:5] from Low to High inclusive.
1FUNCTION InRange(Low : INTEGER, High : INTEGER) RETURNS INTEGER 2DECLARE Day, Count : INTEGER 3Count ← 1 4FOR Day ← 1 TO 6 5IF Temps[Day] > Low AND Temps[Day] <= High THEN 6Count ← Count + 1 7ENDIF 8NEXT Day 9RETURN Count 10ENDFUNCTION
Test run
Temps holds 15, 22, 18, 9, 15 (Temps[1:5]). InRange(15, 21) should return 3.
Actual: a run-time error at line 5 when Day = 6: Temps[6] is outside the array.
Dry run after correcting line 4
A dry run compares what each line does with what it should do. The row where they differ points to the faulty line.
| Line | Statement that runs | Day | Count | Writes | What happens |
|---|---|---|---|---|---|
| 4 | FOR Day ← 1 TO 5 | 1 | Day 1 | line 4 corrected: the loop now stops at the last index, 5 | |
| line 4 corrected: the loop now stops at the last index, 5 | |||||
| 5 | IF Temps[Day] > Low AND Temps[Day] <= High THEN | test Temps[1] = 15; 15 > 15 is FALSE, so line 6 is skipped | |||
| test Temps[1] = 15; 15 > 15 is FALSE, so line 6 is skipped | |||||
| 8 | NEXT Day | test Count is still 1, but 15 should have been counted | |||
| test Count is still 1, but 15 should have been counted | |||||
Now the whole run, one row per day. Wherever the Line 5 test and Should be columns disagree, line 5 is at fault.
| Day | Temps[Day] | Line 5 test | Should be | Count | What happens | |
|---|---|---|---|---|---|---|
| start | 1 | line 3 sets Count ← 1: already one too many | ||||
line 3 sets Count ← 1: already one too many | ||||||
| Day 1 | 1 | 15 | FALSE | TRUE | 15 > 15 is FALSE, but 15 is from 15 to 21, so it should be counted: the line 5 error | |
| 15 > 15 is FALSE, but 15 is from 15 to 21, so it should be counted: the line 5 error | ||||||
| Day 2 | 2 | 22 | FALSE | FALSE | 22 is outside 15 to 21: correctly not counted | |
| 22 is outside 15 to 21: correctly not counted | ||||||
| Day 3 | 3 | 18 | TRUE | TRUE | 2 | 18 is in range: Count ← Count + 1 |
| 18 is in range: Count ← Count + 1 | ||||||
| Day 4 | 4 | 9 | FALSE | FALSE | 9 is outside 15 to 21: correctly not counted | |
| 9 is outside 15 to 21: correctly not counted | ||||||
| Day 5 | 5 | 15 | FALSE | TRUE | 15 > 15 is FALSE, but 15 is from 15 to 21, so it should be counted: the line 5 error | |
| 15 > 15 is FALSE, but 15 is from 15 to 21, so it should be counted: the line 5 error | ||||||
| end | 2 | RETURN 2, but 3 temperatures are in range | ||||
| RETURN 2, but 3 temperatures are in range | ||||||
A dry run is not the only way in. When the fault is harder to find by hand, these help:
Ways to expose a fault
- a dry run: a trace table with test data
- extra OUTPUT statements; different test values to see which fail
- an IDE: set a breakpoint in InRange to pause at a chosen statement; then single-step one statement at a time while a watch window displays the changing Day and Count values
| Day | Temps[Day] | Line 5 test | Should be | Count |
|---|---|---|---|---|
| 1 | ||||
| 1 | 15 | FALSE | TRUE | |
| 2 | 22 | FALSE | FALSE | |
| 3 | 18 | TRUE | TRUE | 2 |
| 4 | 9 | FALSE | FALSE | |
| 5 | 15 | FALSE | TRUE |
Ways to expose a fault
- a dry run: a trace table with test data
- extra OUTPUT statements; different test values to see which fail
- an IDE: set a breakpoint in InRange to pause at a chosen statement; then single-step one statement at a time while a watch window displays the changing Day and Count values
Identify and correct
The run exposed three faults. For each one, give the line number, the type of error and the corrected line written in full.
| Line | Error | Type | Corrected line |
|---|---|---|---|
| 3 | Count starts at 1, so the result is one too many | logic | Count ← 0 |
| 4 | the loop runs to 6, but the array ends at index 5 | run-time | FOR Day ← 1 TO 5 |
| 5 | > leaves out a temperature equal to Low | logic | IF Temps[Day] >= Low AND Temps[Day] <= High THEN |
Correct line 3 · logic
Count starts at 1, so the result is one too many
Count ← 0
Correct line 4 · run-time
the loop runs to 6, but the array ends at index 5
FOR Day ← 1 TO 5
Correct line 5 · logic
> leaves out a temperature equal to Low
IF Temps[Day] >= Low AND Temps[Day] <= High THEN
1 With only line 4 corrected, what does InRange(15, 21) return?
2 Which corrected line makes a temperature equal to Low count?
3 With all three lines corrected, Temps holds 21, 15, 30, 16, 14. What does InRange(15, 21) return?
- 12
- 2line 5:
>=instead of> - 33: 21, 15, 16 are from 15 to 21
Dry run, walkthrough, white-box and black-box
No program can be tried with every possible input, so testers must choose their data with care. Testing methods differ in how the test data is chosen: from the code (white-box) or from the specification (black-box). A dry run and a walkthrough work through the code by hand.
Two of these methods are ways of checking code by hand; the other two say where the test data comes from.
| Method | What it is |
|---|---|
| Dry run | Working through the code by hand with test data, recording each variable and output in a trace table. |
| Walkthrough | Going through the program a line at a time, often with others, with test data and expected outputs: an unexpected value or path shows an error. |
| White-box | Test data is chosen from the code, so that every path through the program is tested. |
| Black-box | Test data is chosen from the specification, without looking at the code; actual results are compared with expected results. A difference exposes a logic or run-time error. |
White-box: exercise the four coded paths
Each decision has a TRUE and a FALSE branch. Choose data so that every branch is taken.
1FUNCTION Charge(Kg : REAL, 2Express : BOOLEAN) RETURNS INTEGER 3DECLARE Cost : INTEGER 4IF Kg <= 2.0 THEN 5Cost ← 5 6ELSE 7Cost ← 8 8ENDIF 9IF Express = TRUE THEN 10Cost ← Cost * 2 11ENDIF 12RETURN Cost 13ENDFUNCTION
Specification
A parcel of up to and including 2 kg costs $5; a heavier parcel costs $8. Express delivery doubles the cost. A weight of 0 kg or less is invalid: return −1.
Each diamond is one IF in the code. Its labels name the tests that take each branch, and the table gives their data.
| Test | Method | Kg, Express | Purpose | Expected | Actual |
|---|---|---|---|---|---|
| 1 | white-box | 1.5, FALSE | line 4 TRUE, line 9 FALSE | 5 | 5 |
| 2 | white-box | 1.5, TRUE | line 4 TRUE, line 9 TRUE | 10 | 10 |
| 3 | white-box | 3.0, FALSE | line 4 FALSE, line 9 FALSE | 8 | 8 |
| 4 | white-box | 3.0, TRUE | line 4 FALSE, line 9 TRUE | 16 | 16 |
Test 1: white-box
Kg, Express: 1.5, FALSE
line 4 TRUE, line 9 FALSE
| Expected | Actual |
|---|---|
| 5 | 5 |
Test 2: white-box
Kg, Express: 1.5, TRUE
line 4 TRUE, line 9 TRUE
| Expected | Actual |
|---|---|
| 10 | 10 |
Test 3: white-box
Kg, Express: 3.0, FALSE
line 4 FALSE, line 9 FALSE
| Expected | Actual |
|---|---|
| 8 | 8 |
Test 4: white-box
Kg, Express: 3.0, TRUE
line 4 FALSE, line 9 TRUE
| Expected | Actual |
|---|---|
| 16 | 16 |
1 Give white-box test data that makes line 4 FALSE and line 9 TRUE, with the expected result.
2 A tester picks 2.0 kg because the specification says “up to and including 2 kg”, without reading the code. Which method is that?
3 Which method goes through the program a line at a time, often with other people?
- 1for example Kg = 3.0, Express = TRUE: expected 16
- 2black-box: the data comes from the specification
- 3a walkthrough
Black-box tests from the specification
The four white-box tests passed, yet the code is still wrong: it never checks for an invalid weight. Black-box tests come from the specification, so they can find a rule the code left out.
A black-box tester sees only the specification, never the code:
Specification
A parcel of up to and including 2 kg costs $5; a heavier parcel costs $8. Express delivery doubles the cost. A weight of 0 kg or less is invalid: return −1.
Black-box tests on the original code
Compare each expected result, from the specification, with what the original code returns.
| Test | Method | Kg, Express | Purpose | Expected | Original actual |
|---|---|---|---|---|---|
| 5 | black-box | 2.0, FALSE | boundary: the heaviest $5 parcel | 5 | 5 |
| 6 | black-box | 2.1, FALSE | boundary: just over 2 kg | 8 | 8 |
| 7 | black-box | -1.0, FALSE | abnormal: an invalid weight | -1 | 5 |
Test 5: black-box
Kg, Express: 2.0, FALSE
boundary: the heaviest $5 parcel
| Expected | Original actual |
|---|---|
| 5 | 5 |
Test 6: black-box
Kg, Express: 2.1, FALSE
boundary: just over 2 kg
| Expected | Original actual |
|---|---|
| 8 | 8 |
Test 7: black-box
Kg, Express: -1.0, FALSE
abnormal: an invalid weight
| Expected | Original actual |
|---|---|
| -1 | 5 |
Test 7 fails, and the failure names the missing rule. The repair adds that check before anything else.
FUNCTION Charge(Kg : REAL, Express : BOOLEAN) RETURNS INTEGER DECLARE Cost : INTEGER IF Kg <= 0 THEN RETURN -1 ENDIF IF Kg <= 2.0 THEN Cost ← 5 ELSE Cost ← 8 ENDIF IF Express = TRUE THEN Cost ← Cost * 2 ENDIF RETURN Cost ENDFUNCTION
Why the invalid case failed
- The specification says a weight of 0 kg or less returns −1.
- In the original code, −1.0 passes
Kg <= 2.0, so it returns 5. - The repair checks
Kg <= 0first and returns −1. Every positive weight still gives its old result.
1 Give a black-box test that uses 0 kg. What is the expected result, and what did the original code return?
2 What does the repaired function return for Charge(0.5, TRUE)?
3 Why did white-box tests 1 to 4 not find this fault?
- 1Charge(0, FALSE): expected -1; the original returned 5
- 210
- 3they were chosen from the code’s paths, and the code has no path for an invalid weight
Testing stages: stub to acceptance
A fault is easiest to find in a small piece of code, so testing starts small and widens. It happens in stages: each module on its own, then modules together, then in-house testers, users and finally the customer.
Who tests, when, and why
Each stage has its own testers and its own purpose: who tests, when, and what it checks.
| Method | Who and when | Purpose |
|---|---|---|
| Stub testing | while modules are being written, or when one does not work | A simple (dummy) module replaces the missing module. It returns a known value or outputs a message to show it has been called, so the calling code can be tested. |
| Integration | when modules that have been tested on their own are combined | Tests that the modules work together: parameters and return values pass correctly. Modules are often added one at a time. |
| Alpha | in-house, by developers or dedicated testers, as modules are coded and before release | Finds errors before anyone outside the developer uses it. |
| Beta | a small group of potential users, in the real world, before general release | Users check it works as required and report problems and suggestions, which can be addressed before general release. |
| Acceptance | the customer, at hand-over | Checks the software meets the agreed requirements, so the customer can approve (sign off) it. |
A stub in action
A module can be tested before the modules it calls are written, with a stub in their place.
// a stub: Discount() is not written yet FUNCTION Discount(Code : STRING) RETURNS REAL OUTPUT "Discount called with ", Code RETURN 5.0 ENDFUNCTION // the calling code being tested Price ← 40.0 Price ← Price - Discount("SPRING") OUTPUT "To pay: ", Price
Output
Discount called with SPRING
To pay: 35.0
The message proves the call was made; the fixed return value lets the calculation be checked by hand: 40.0 − 5.0 = 35.0.
1 Module Total is finished, but the Discount module it calls has not been written. How can Total be tested now?
2 Who carries out beta testing, and where?
3 What does acceptance testing check?
- 1stub testing: a simple dummy Discount returns a known value or outputs a message, so Total can be run and checked
- 2a small group of potential users, in their real setting, before general release
- 3that the software meets the agreed requirements, so the customer can sign it off
Test strategy, test plan and test data
Testing without a plan tends to try the easy cases and miss the edges. A test plan lists each test with its data, the type of test data and the expected outcome, worked out before the program is run.
Two documents, decided at different times: the strategy first, at the design stage, then the plan that carries it out.
Test strategy
Decided at the design stage. It sets the approach: which testing methods are used and when, who tests, and what is tested: flow of control, validation, files. Representative inputs are chosen when exhaustive testing is impractical.
Test plan
The detailed list of tests: each test’s number, purpose, test data and its type, and expected outcome (worked out before testing). The actual outcome is recorded when the test is run.
Types of test data
Most faults hide at the limits, which is why the type of each test value matters. Each type keeps one colour in every table and example.
| Type of test data | Meaning | Outcome |
|---|---|---|
| Normal | typical valid data, well inside the range | accepted |
| Abnormal (erroneous) | data that should be rejected: out of range, or the wrong type | rejected |
| Extreme | the largest and the smallest values that are still valid | accepted |
| Boundary | each side of a limit: the last valid and the first invalid value | accepted / rejected |
| Type of test data | Meaning | Outcome |
|---|---|---|
| Normal | typical valid data, well inside the range | accepted |
| Abnormal (erroneous) | data that should be rejected: out of range, or the wrong type | rejected |
| Extreme | the largest and the smallest values that are still valid | accepted |
| Boundary | each side of a limit: the last valid and the first invalid value | accepted / rejected |
A test plan: integer ages 11–17, inclusive
Put the valid range on a number line first: the test values sit at its edges and just outside them. Each coloured value then becomes one row of the plan.
| Test | Data | Type | Purpose | Expected |
|---|---|---|---|---|
| 1 | 14 | normal | a typical valid age | TRUE |
| 2 | 11 | extreme / boundary | the lowest valid age | TRUE |
| 3 | 17 | extreme / boundary | the highest valid age | TRUE |
| 4 | 10 | boundary (abnormal) | just below the range | FALSE |
| 5 | 18 | boundary (abnormal) | just above the range | FALSE |
| 6 | -3 | abnormal | a negative age | FALSE |
| Test | Data | Type | Purpose | Expected |
|---|---|---|---|---|
| 1 | 14 | normal | a typical valid age | TRUE |
| 2 | 11 | extreme / boundary | the lowest valid age | TRUE |
| 3 | 17 | extreme / boundary | the highest valid age | TRUE |
| 4 | 10 | boundary (abnormal) | just below the range | FALSE |
| 5 | 18 | boundary (abnormal) | just above the range | FALSE |
| 6 | -3 | abnormal | a negative age | FALSE |
1 A month number must be a whole number from 1 to 12. Give the two extreme values.
2 Give the two boundary pairs, with the expected outcome of each value.
3 Give one abnormal value of the wrong type.
- 11 and 12, both accepted
- 20 rejected | 1 accepted; 12 accepted | 13 rejected
- 3for example “May” or 3.5
Choosing test data for a test plan
A test is useful only when its result points to one cause. Good test data checks one thing at a time: each invalid test breaks one rule, and a loop is tested with no data, one item and many items.
Several rules: break one rule per test
A username must have (1) 6 to 10 characters, (2) a letter first and (3) no spaces. If a test broke two rules, a rejection would not show which check works.
| Test data | (1) 6 to 10 characters | (2) letter first | (3) no spaces | Expected |
|---|---|---|---|---|
| "Rowan7" | ✓ | ✓ | ✓ | accepted |
| "Row7" | ✗ broken | ✓ | ✓ | rejected |
| "Rowan202512" | ✗ broken | ✓ | ✓ | rejected |
| "7Rowan" | ✓ | ✗ broken | ✓ | rejected |
| "Ro wan7" | ✓ | ✓ | ✗ broken | rejected |
| Test data | Rule broken | Reason |
|---|---|---|
| "Rowan7" | none | valid: all three rules met |
| "Row7" | rule 1 | too short |
| "Rowan202512" | rule 1 | too long |
| "7Rowan" | rule 2 | starts with a digit |
| "Ro wan7" | rule 3 | contains a space |
A sequence ended by a terminator
CountHigh() counts the values above 50 and the values of 50 or less; 0 ends the input and is not counted.
Read the plan beside the loop: it runs CountHigh with several values, with one, and with none, and puts 50 and 51 either side of the limit.
PROCEDURE CountHigh() DECLARE Value, High, Low : INTEGER High ← 0 Low ← 0 INPUT Value WHILE Value <> 0 IF Value > 50 THEN High ← High + 1 ELSE Low ← Low + 1 ENDIF INPUT Value ENDWHILE OUTPUT High, " high, ", Low, " low" ENDPROCEDURE CALL CountHigh()
| Input sequence | Purpose | Expected output |
|---|---|---|
| 72, 15, 50, 51, 0 | a mix, including 50 and 51 either side of the boundary | 2 high, 2 low |
| 60, 99, 0 | only high values | 2 high, 0 low |
| 5, 50, 0 | only low values | 0 high, 2 low |
| 51, 0 | one high value, then the terminator | 1 high, 0 low |
| 0 | no values: the terminator on its own | 0 high, 0 low |
Inputs: 72, 15, 50, 51, 0
a mix, including 50 and 51 either side of the boundary
Expected: 2 high, 2 low
Inputs: 60, 99, 0
only high values
Expected: 2 high, 0 low
Inputs: 5, 50, 0
only low values
Expected: 0 high, 2 low
Inputs: 51, 0
one high value, then the terminator
Expected: 1 high, 0 low
Inputs: 0
no values: the terminator on its own
Expected: 0 high, 0 low
Choosing test data: a checklist
- normal data, both sides of every limit, and abnormal data of each kind
- one rule broken in each invalid test
- data covering each decision outcome or required result class, and different executable paths
- for loops and sequences: no data, one item and many items
1 Which of these usernames breaks only rule 3: "Sam Lee1", "Sa m", "SamLee1"?
2 What does CountHigh output for the inputs 50, 51, 0?
3 Why test CountHigh with 0 on its own?
- 1"Sam Lee1": it has 8 characters and starts with a letter, but has a space
- 2
1 high, 1 low - 3the loop body never runs, so it checks the output for no data: 0 high, 0 low
Testing every possible return value
A result that no test ever produces is a result nobody has checked. When a function can return only a few values, plan one test for each possible result, and work out each expected result before you run it.
NextGroup uses (N MOD 3) + 1 to cycle between three group codes. Its contract accepts only INTEGER inputs 1, 2 or 3. The tests use only those valid inputs and check every group it can return.
FUNCTION NextGroup( N : INTEGER) RETURNS INTEGER RETURN (N MOD 3) + 1 ENDFUNCTION
Derive the complete result range
One planned test per possible result
Three inputs, three different results: together these tests reach every value the function can return.
Written as a test plan, each row gives an input and the return worked out before the call is run.
| Input N | N MOD 3 | Expected return |
|---|---|---|
| 1 | 1 | 2 |
| 2 | 2 | 3 |
| 3 | 0 | 1 |
After running each call, record its actual return and compare it with the expected value. A wrong result identifies a failed test; merely reaching a result is not enough.
Repeating one test does not cover every result
- Calling NextGroup(2) three times should return 3 each time.
- Those calls test result 3 again and again; they give no evidence for results 1 and 2.
- The three different inputs above cover the full result set.
1 A function returns (N MOD 2) + 1 for valid inputs 1 to 4. List every result it can return.
2 Give the fewest inputs that test every result, with the expected return for each.
3 NextGroup(3) is called twice. Which results have been tested?
- 11, 2
- 2two inputs, for example 1 (returns 2) and 2 (returns 1)
- 3only result 1
Maintenance and avoiding faults
A program in use still needs work. Some errors only appear with rare data, and the requirements, hardware and laws around a program change. Software does not wear out: maintenance means changing the code.
Three types of maintenance
The reason for a change decides its type: a fault to correct, a change around the program, or an improvement.
| Type | When it is needed | Worked example: a school library app |
|---|---|---|
| Corrective | the program does not work as expected: a bug (a logic or run-time error) found after the program is in use | A fine is charged when a book is returned on its due date. The comparison is corrected. |
| Adaptive | the requirements change, new technology or hardware is used, library modules change, or legislation changes | The school moves to tablets and a new law limits what pupil data may be kept. The app is changed for both. |
| Perfective | the program works, but its performance or maintainability is improved | Searching the catalogue is slow: a faster search is written and repeated code is moved into a module. |
| Type | When it is needed | Example |
|---|---|---|
| Corrective | the program does not work as expected: a bug (a logic or run-time error) is found after the program is in use | a total is wrong when a list is empty; a password is shown instead of hidden |
| Adaptive | the requirements change, new technology or hardware is used, library modules change, or legislation changes | running on new hardware; a new tax rule; extra features users ask for |
| Perfective | the program works, but its performance or maintainability is improved | a faster search; repeated code moved into a module; clearer identifiers |
Two questions decide the type
Ask the top question first, and the second only if the program works. The box you reach is the type.
Every corrective change is a fault that slipped through. These habits, used while the program is written, mean fewer of them.
Avoiding faults in the first place
- a clear requirements specification, so the right problem is solved
- meaningful identifiers, so the code is easier to understand, debug, test and maintain
- comments, indentation, white space and keywords in capitals
- modules with local variables and parameters
- constants for fixed values: entered once, changed in one place
- tried and tested library routines
- validating input and checking values before an operation
Classify the change
| Change | Type |
|---|---|
| Bugs are reported in the first week of use | corrective |
| The program is changed to work with new hardware | adaptive |
| The code is changed to improve the speed of response | perfective |
| Staff need features that were not originally agreed | adaptive |
| A new law changes how customer data must be stored | adaptive |
| Comments and meaningful names are added for future editors | perfective |
Avoiding faults in the first place
- a clear requirements specification, so the right problem is solved
- meaningful identifiers, so the code is easier to understand, debug, test and maintain
- comments, indentation, white space and keywords in capitals
- modules with local variables and parameters
- constants for fixed values: entered once, changed in one place
- tried and tested library routines
- validating input and checking values before an operation
Which type of maintenance is each change? Use the two questions.
1 Bugs are reported in the first week of use.
2 The program is changed to work with new hardware.
3 The code is changed to improve the speed of response.
4 Staff need features that were not originally agreed.
5 A new law changes how customer data must be stored.
6 Comments and meaningful names are added for future editors.
- 1corrective
- 2adaptive
- 3perfective
- 4adaptive
- 5adaptive
- 6perfective
Analyse and amend an existing program
Changing one line of a working program can quietly break another. To amend a program, analyse it first: trace it, and find every line that depends on the part being changed.
To amend a program, analyse it first: trace it, and find every line that depends on the part being changed.
Two new requirements: process 1 to 7 days, and also output the day of the highest temperature. Temps[1:7] holds 18, 21, 24, 19, 17, 22, 19. NumDays must be at least 1 and no greater than the seven stored values.
1PROCEDURE Summary() 2DECLARE Day, Total, Max : INTEGER 3Total ← 0 4Max ← Temps[1] 5FOR Day ← 1 TO 7 6Total ← Total + Temps[Day] 7IF Temps[Day] > Max THEN 8Max ← Temps[Day] 9ENDIF 10NEXT Day 11OUTPUT "Mean: ", Total / 7 12OUTPUT "Highest: ", Max 13ENDPROCEDURE 14 15CALL Summary()
Output: Mean: 20.0 · Highest: 24
1PROCEDURE Summary(NumDays : INTEGER) 2DECLARE Day, Total, Max, MaxDay : INTEGER 3Total ← 0 4Max ← Temps[1] 5MaxDay ← 1 6FOR Day ← 1 TO NumDays 7Total ← Total + Temps[Day] 8IF Temps[Day] > Max THEN 9Max ← Temps[Day] 10MaxDay ← Day 11ENDIF 12NEXT Day 13OUTPUT "Mean: ", Total / NumDays 14OUTPUT "Highest: ", Max, " on day ", MaxDay 15ENDPROCEDURE 16 17CALL Summary(5)
Output: Mean: 19.8 · Highest: 24 on day 3
The amendments
Each change, where it goes and why. Write each new line in full, and say where it goes.
| Line (new) | Change | Why |
|---|---|---|
| 1 | add the parameter NumDays : INTEGER | the number of days now varies |
| 2 | declare MaxDay | a new value to output |
| 5 | insert MaxDay ← 1 | day 1 is the highest so far |
| 6, 13 | replace the literal 7 with NumDays | loop and divide for the given number of days |
| 10 | insert MaxDay ← Day | record the day of each new highest value |
| 14 | output MaxDay as well | the new requirement |
| 17 | call Summary(5) | supply the required day count in the caller |
1 What does the amended program output for CALL Summary(3)?
2 Why is MaxDay set to 1 before the loop?
3 After the change, which old test should be run again, and what must it output?
- 1
Mean: 21.0thenHighest: 24 on day 3 - 2Max starts as Temps[1], so day 1 is the highest so far until a larger value is found
- 3the old seven-day run,
Summary(7): it must still outputMean: 20.0andHighest: 24, now followed byon day 3