Read, analyse and refine programs (Edexcel GCSE Computer Science): Revision Notes
Read, analyse and refine programmes
What does it mean to read, analyse and refine code?
When you're working with programming problems, you'll often need to examine existing code and make it better. This involves three important steps:
Read means carefully examining the code to understand exactly what it does and how it works. You need to follow the logic step by step.
Analyse means identifying problems, inefficiencies, or areas where the code could be improved. This might include spotting repeated code, unnecessary steps, or poor programming practices.
Refine means making changes to improve the programme. This could involve making it more efficient, easier to read, or following better programming practices while still meeting all the original requirements.
When you refine code, you must ensure it still meets all the specified requirements, including best practice guidelines and efficiency standards.

Understanding code efficiency through examples
Example 1: Range checker program
Let's look at a programme that checks which range a number falls into. The original code uses multiple separate if statements to test different ranges.
import random
number = 0
number = random.randint(1, 100)
print("The number is", number)
if (number < 10):
print("Too small")
if (number >= 10) and (number <= 50):
print("Lower side")
if (number > 50) and (number <= 80):
print("Higher side")
if (number > 80) and (number <= 100):
print("Too high")
Analysing the problem
When you examine this code carefully, you can spot a major inefficiency. The programme checks every single condition, even after finding the correct range. For example, if the number is 5, the programme will:
- Check if it's less than 10 (yes, so print "Too small")
- Still check if it's between 10-50 (unnecessary)
- Still check if it's between 51-80 (unnecessary)
- Still check if it's between 81-100 (unnecessary)
This wastes processing time because a number can only belong to one range.
Refining the solution
The refined version uses if...elif...else statements instead. This creates a more efficient structure where only one condition is executed:
import random
number = 0
number = random.randint(1, 100)
print("The number is", number)
if (number < 10):
print("Too small")
elif (number <= 50):
print("Lower side")
elif (number <= 80):
print("Higher side")
else:
print("Too high")
This improved version stops checking conditions once it finds the correct range, making the programme much more efficient.
Data storage efficiency
Example 2: Storing multiple values
Another common area for improvement is how data is stored. Consider this programme that adds seven numbers:
total = 0
var1 = 11
var2 = 22
var3 = 33
var4 = 44
var5 = 55
var6 = 66
var7 = 77
total = var1 + var2 + var3 + var4 + var5 + var6 + var7
print(total)
Analysis and refinement opportunities
This code works but has several inefficiency issues:
- It uses seven separate variables when the data could be stored more efficiently
- The addition line is very long and hard to read
- Adding more numbers would require creating more individual variables
A more efficient approach would be to use a list to store the data and a loop to process it:
numbers = [11, 22, 33, 44, 55, 66, 77]
total = 0
for number in numbers:
total = total + number
print(total)
Or even more efficiently using Python's built-in sum() function:
numbers = [11, 22, 33, 44, 55, 66, 77]
total = sum(numbers)
print(total)
Key strategies for code analysis
When examining code for potential improvements, look for these common patterns:
- Repeated similar code - Can it be simplified using loops or functions?
- Inefficient conditional statements - Could
if...elif...elsereplace multipleifstatements? - Poor data storage - Could lists, dictionaries, or other data structures work better?
- Unnecessary operations - Are there steps that don't need to happen?
- Readability issues - Is the code clear and easy to understand?
The most effective code improvements often involve recognising these patterns and applying appropriate programming structures to address them.
Exam tips
Essential Exam Strategies:
- Always read the code line by line first to understand what it does
- Identify the specific requirements you need to meet
- Look for patterns of inefficiency like repeated conditions or variables
- Consider whether built-in functions could replace custom code
- Make sure your refined version still produces the same correct output
- Test your improvements by tracing through with sample data
Key Points to Remember:
- Read code carefully to understand its purpose and logic flow
- Analyse by identifying inefficiencies, repeated code, or poor practices
- Refine by improving efficiency while maintaining the same functionality
- Use
if...elif...elseinstead of multiple separateifstatements when checking mutually exclusive conditions - Consider using lists and loops instead of many individual variables for similar data