6.1.2 Reading and writing programs

Reading, Writing, Analysing and Refining Programs

Programmers spend at least as much time reading and refining code as they do writing it from scratch. Being able to look at an unfamiliar program and understand what it does — then improve it — is a core practical skill.

  • Reading: tracing execution, following variable values, predicting output
  • Writing: translating a specification or algorithm into working code
  • Analysing: identifying what the code does, spotting errors, evaluating efficiency
  • Refining: improving existing code — fixing bugs, improving clarity, making it more efficient

Worked Example: Read, Analyse, Refine

Below is a program that finds the largest number entered by a user. Read it, identify an issue, and see the refined version.

Original (has an issue):

largest = 0               # Issue: assumes all numbers are positive
count = int(input("How many numbers? "))
for i in range(count):
    n = int(input("Enter a number: "))
    if n > largest:
        largest = n
print("Largest:", largest)

Issue: initialising largest = 0 means the program fails if all input numbers are negative (e.g. -5, -3, -8 — it would incorrectly output 0).

Refined:

count = int(input("How many numbers? "))
numbers = []
for i in range(count):
    n = int(input("Enter a number: "))
    numbers.append(n)
largest = numbers[0]       # Initialise to first value, not 0
for n in numbers:
    if n > largest:
        largest = n
print("Largest:", largest)

Initialising largest to the first element in the list rather than 0 ensures correctness for any set of numbers, including all-negative inputs.

Original (has an issue):

int largest = 0;
int count = int.Parse(Console.ReadLine());
for (int i = 0; i < count; i++) {
    int n = int.Parse(Console.ReadLine());
    if (n > largest) largest = n;
}
Console.WriteLine("Largest: " + largest);

Refined:

int count = int.Parse(Console.ReadLine());
int[] numbers = new int[count];
for (int i = 0; i < count; i++) {
    numbers[i] = int.Parse(Console.ReadLine());
}
int largest = numbers[0];
for (int i = 1; i < count; i++) {
    if (numbers[i] > largest) largest = numbers[i];
}
Console.WriteLine("Largest: " + largest);

Original (has an issue):

Dim largest As Integer = 0
Dim count As Integer = Integer.Parse(Console.ReadLine())
For i As Integer = 0 To count - 1
    Dim n As Integer = Integer.Parse(Console.ReadLine())
    If n > largest Then largest = n
Next
Console.WriteLine("Largest: " & largest)

Refined:

Dim count As Integer = Integer.Parse(Console.ReadLine())
Dim numbers(count - 1) As Integer
For i As Integer = 0 To count - 1
    numbers(i) = Integer.Parse(Console.ReadLine())
Next
Dim largest As Integer = numbers(0)
For i As Integer = 1 To count - 1
    If numbers(i) > largest Then largest = numbers(i)
Next
Console.WriteLine("Largest: " & largest)

 Key Takeaways

  • Reading code means tracing execution mentally and predicting what the program will output for given inputs.
  • Analysing code means identifying what it does, where it may fail, and how it could be improved.
  • Refining code means making targeted improvements — fixing logic errors, improving variable naming, handling edge cases.
  • Always initialise variables to a meaningful value, not an arbitrary one — largest = 0 is a common logic error when inputs could be negative.