GCSE · Computer Science · OCR · Spec J277
Refining algorithms
Your program runs. No errors, right answer, job done? Then someone reads the requirements back to you, and it only ever takes one input. Welcome to refining.
Refining a program
It works. So why isn't it finished?
Step through two versions of the same program. Version 1 runs without a single error. Version 2 is the refined one.
Before you press Next: version 1 has no errors. Does it do everything it's supposed to?
1 // Version 12 limit = 203 total = 04 price = int(input("Price: "))5 total = total + price6 print(total)7 // Version 2 (refined)8 limit = 209 total = 010 while total < limit11 price = int(input("Price: "))12 total = total + price13 endwhile14 print(total)
Variables
Output
Step 1: The job: let the user enter more than one price, keep asking while the total is less than the limit of 20, then show the total. Version 1 first.
Press Next. Watch version 1 stop after one price, then watch version 2 keep asking.
Reviewing code
Which check caught it?
Pick a card, then pick the check that would catch it.
Still to sort
How well it works (0)
The code is there, but it does the wrong thing.
Where the line is: Here the feature exists and gives a wrong result. If the feature is missing altogether, it's a requirements problem.
How readable it is (0)
Could someone else understand it and work with it easily?
Meets the functional requirements (0)
Does it do everything it was asked to do?
Where the line is: Here something the program should do isn't there at all. Code that's there but gives a wrong result belongs under how well it works.
Each card is something a reviewer spotted in a program that adds up prices. Decide which of the three review checks found it.
Modularity
Reason it through
The same few lines that read a price appear in four places. Why does moving them into a subprogram make the program better?
First link · your turn
Where should those lines live instead of four copies?
WHAT YOU'VE LEARNED
A quick recap of today's lesson.
Your program runs. Brilliant. Now check it actually does the job, and make it easier for everyone else to work with.
What you need to know
- Reviewing code means checking three things: how well it works, how readable it is, and whether it meets the functional requirements.
- A program can run without a single error and still miss a requirement, like accepting only one input when it needs more.
Have a goPriya's to-do app saves one task, shows it and closes with zero errors. "Finished!" she says. The brief says it must store a whole list of tasks. Has she finished?
Not yet: it misses a functional requirement.
Zero errors only shows it runs. A review also checks it against what it was asked to do, and storing one task isn't storing a list.
- Improving code can mean simplifying logic, breaking large sections into subprograms, or correcting errors.
- Iteration keeps a program asking for input, e.g. a while loop that repeats while the total is less than the limit.
Have a goA loop says while total < limit. The limit is 20 and the total has just become exactly 20. Does it ask for another input?
No. The loop stops.
20 isn't less than 20. The tempting yes reads < as 'less than or equal to', but the loop only repeats while the total is below the limit.
- Readable, maintainable code is code that other people can understand and work with easily.
- Good practice that helps: comments, a clear program layout, sensible identifiers and consistent naming conventions.
- Modularity is breaking software into independent, self-contained units. In structured programming, those are subprograms, each doing one specific task.
- Write code once in a subprogram and call it whenever it's needed: less repetition, plus easier reading, testing and maintenance.
- Several programmers can develop different subprograms at the same time, which speeds up development.
- Function or procedure? Both are subprograms, but a function returns a value (or values) to the main program and a procedure doesn't.
Have a goA subprogram called getTotal() works out the total and hands it back to the main program. Function or procedure?
A function.
It returns a value to the main program. Doing a task isn't the test; both kinds do that. Handing a value back is what makes it a function.
The big picture
Refining code means reviewing it (how well it works, how readable it is, and whether it meets its functional requirements) and then improving it: adding iteration, simplifying logic, correcting errors and breaking it into subprograms. Functions return a value to the main program; procedures don't.
Key points
Worked example
Problem
Requirement: print "Child price" for ages under 12 and "Full price" otherwise. The program is: a = int(input("Age: ")), then if not (a < 12) then print("Full price") else print("Child price") endif. Review it and refine it.
⚠ Watch out
Treating "it runs" as "it's finished". A program can produce no errors at all and still fail a functional requirement. The other trap: calling any subprogram that does a task a function. Both kinds do a task; only a function returns a value to the main program.
Memory hook
Run it, read it, check the brief. Then fix it, tidy it, split it.
Check yourself
Without looking back: what three things does a review check, which improvement would let a program take more than one input, and what single difference separates a function from a procedure?
Flashcards
(13)What three things does a code review check?
How can reviewing against the functional requirements find an improvement?
Name three ways of improving code.
How can iteration improve a program that only takes one input?
Why should code be readable and maintainable?
Four good programming practices that make code readable?
What is modularity?
In structured programming, what is a program divided into?
How does a subprogram reduce repetition?
Why can a modular program be developed faster?
Apart from less repetition and faster development, give three benefits of improving modularity.
What are the two kinds of subprogram?
What makes a function different from a procedure?
Tap any card to flip it, or use Study as deck to go through them one at a time. In the full lesson these run as a spaced-repetition deck — you rate each card Hard, Good or Easy and the tricky ones keep coming back until they stick.
Learning with Lightbulb is opening soon
You can use this lesson now. Join the waitlist and we'll let you know when the full Lightbulb experience is ready.
Keep me postedMore OCR GCSE Computer Science topics
How this lesson was checked. This OCR GCSE Computer Science (specification J277)lesson was published through Lightbulb Learning's human-designed editorial process — the educational standards, accuracy rules and publication checks it must pass were authored and approved by Philip Halpin. It passed subject-specific assessment, automated educational checks and technical publication verification before going live (publication checks completed 3 November 2026). Published pages are monitored, human spot-checking is ongoing across the lesson library, and anything found wrong is corrected or withdrawn. How our lessons are made and checked. Spotted a mistake? Email hello@lightbulblearning.co and we'll review it.