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 1
2 limit = 20
3 total = 0
4 price = int(input("Price: "))
5 total = total + price
6 print(total)
7 // Version 2 (refined)
8 limit = 20
9 total = 0
10 while total < limit
11 price = int(input("Price: "))
12 total = total + price
13 endwhile
14 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.

1 / 19

Press Next. Watch version 1 stop after one price, then watch version 2 keep asking.

Step 1 of 19: 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..

Watch out: Running without errors is not the same as meeting the requirements. Always check the code against what it was asked to do.

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.

8 of 8 still to sort.

Each card is something a reviewer spotted in a program that adds up prices. Decide which of the three review checks found it.

Watch out: A missing feature and a wrong answer feel similar, but they're different checks. Missing = requirements. There but wrong = how well it works.

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?

Link 1 of 4

First link · your turn

Where should those lines live instead of four copies?

2
Locked — reveal the link above first
3
Locked — reveal the link above first
4
Locked — reveal the link above first

Two kinds of subprogram

FunctionvsProcedure

Lead with the last row. It's the one that decides it.

Focus

What is it?

Function

A subprogram

Procedure

A subprogram

The insight

Same family. Both are subprograms, so both help make a program modular.

What does it do?

Function

Performs one specific task

Procedure

Performs one specific task

Example

Function

getPrice() asks for a price and hands it back to the main program

Procedure

showRules() displays the rules on screen, and that's all

Returns a value (or values) to the main program?

Function

Yes

Procedure

No

Have a go

Review it, then refine it

x = 0 y = int(input("Price: ")) x = x - y print(x)

This program should let the user keep entering prices while the total is less than 20, then display the total. Describe how a programmer could review and improve this program. [4 marks]

0 words · your answer stays on this page and is not sent anywhere.

Exam line: Tie each improvement to a specific problem in the code you were given.

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

1Review: how well it works, how readable it is, does it meet the functional requirements?
2Running without errors doesn't mean every requirement is met.
3Improve by simplifying logic, using subprograms, correcting errors, and adding iteration where input must repeat.
4Readable code uses comments, clear layout, good identifiers and naming conventions.
5Modularity: independent, self-contained subprograms, each with one specific task.
6A function returns a value or values to the main program; a procedure doesn't.

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 well the code works, how readable it is, and whether it meets the functional requirements.
How can reviewing against the functional requirements find an improvement?
It shows where the code doesn't do something it should yet, such as accepting more than one input.
Name three ways of improving code.
Simplifying logic, breaking large sections into subprograms, and correcting errors.
How can iteration improve a program that only takes one input?
Put the input inside a loop, e.g. a while loop that keeps asking while the total is less than the limit.
Why should code be readable and maintainable?
So that other people can understand it and work with it easily.
Four good programming practices that make code readable?
Comments, program layout, identifiers, and naming conventions.
What is modularity?
Breaking software into independent, self-contained units, or modules.
In structured programming, what is a program divided into?
Subprograms, each designed to perform one specific task.
How does a subprogram reduce repetition?
The code is written once in the subprogram and called whenever it's needed.
Why can a modular program be developed faster?
Several programmers can develop different subprograms concurrently.
Apart from less repetition and faster development, give three benefits of improving modularity.
Easier to read, easier to test, easier to maintain.
What are the two kinds of subprogram?
Functions and procedures.
What makes a function different from a procedure?
A function returns a value or values to the main program. A procedure doesn't.

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 posted

More OCR GCSE Computer Science topics

See the full OCR Computer Science curriculum →

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.