GCSE · Computer Science · Edexcel · Spec 1CP2

Code readability techniques

Two programs do exactly the same job. One makes sense at a glance; the other looks like someone fell asleep on the keyboard. Which would you rather fix next term?

Computer Science · Readable code

Three tests for a name

Python will happily run lots of names. Tick every set each name belongs in, then check. Which names make it into the middle?

  • A Python accepts it (no spaces, doesn't start with a number)
  • B Tells a reader what it holds
  • C Snake case: lower case, underscores between words
  1. score
  2. x
  3. height_in_feet
  4. h
  5. last_name
  6. lastName
  7. 2nd_score
  8. last name

Computer Science · Why bother?

It works. So what's the problem?

Maya's program gives exactly the right answers. It is a wall of one-letter names with no comments and no gaps between sections. She is about to hand it in.

Which of these is closest to what you think right now?
How sure are you?

Computer Science · What the computer sees

Watch the interpreter skip the helpful bits

Step through this Python program. Notice which lines the highlight lands on and which it jumps over.

Same program, two versions

Hard to readvsEasy to read

Both versions ask for a score and say Pass or Try again. The computer treats them identically. Which would you rather fix next term?

Focus

What the computer does

Hard to read

Runs it and prints Pass or Try again

Easy to read

Runs it and prints Pass or Try again

The insight

Identical behaviour. Readability makes no difference to the computer, so every difference below is for the human.

Identifiers

Hard to read

p and s

Easy to read

pass_score and pupil_score

Comments

Hard to read

None

Easy to read

One on the line before the code, saying what the program is for

Whitespace

Hard to read

Every line packed together

Easy to read

A blank line between the variable assignments and the if that follows

Computer Science · Python indentation

One line is in the wrong place

This program should say "Well done!" only to pupils who score 50 or more. It runs without an error, but a pupil who scores 20 still sees Well done!. Which line is the problem?

A pupil's Python program — which line goes wrong?

Computer Science · Exam practice

Fix this program for the next reader

def f(l): t = 0 for i in l: t = t + i return t x = f([3, 5, 2]) print(x)

The program below works correctly but is difficult to read. Describe two changes that would improve its readability. For each change, say how it helps a reader. [4 marks]

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

WHAT YOU'VE LEARNED

A quick recap of today's lesson.

The computer doesn't care how your code looks. The humans who read it really do.

What you need to know

  • Good programming practice means making code easy for anyone to read, understand, improve and maintain later.
  • The four practices to know are comments, program layout, identifiers and naming conventions.
  • Readable code matters because engineers work in teams, others may adapt it, and you may forget it after time away.
  • Have a goYour mate says: "It runs, so who cares what it looks like?" Name one person who might care in a year's time.

    Another programmer on the team, someone adapting it, or you, after time away.

    Code gets read and changed long after it is written, so working today is not the same as being easy to maintain.

  • A comment is text in the code that the compiler or interpreter ignores; it is there for the human reader.
  • Comments explain what is not obvious or summarise a subroutine, at the end of a line or on the line before.
  • Have a goWhich one needs a comment more: the line count = count + 1, or a ten-line subroutine that works out a pupil's grade?

    The ten-line subroutine.

    Comments earn their place by explaining what is not obvious or summarising a subroutine; count = count + 1 already explains itself.

  • Separate sections with whitespace (return and space characters), such as a blank line between an assignment and the selection after it.
  • Indentation is part of layout. In Python it is essential syntax that decides how the program runs.
  • In other languages indentation isn't required, but code without it is harder to read.
  • Identifiers are the names for variables, constants and subroutines. Meaningful ones make code self-documenting: score beats x.
  • Some naming rules are non-optional: no spaces and no starting with a number. Python's variable convention is snake case, like last_name.
  • Have a goA classmate names a variable 3rd_goal and says it describes the goal perfectly. Will Python accept it?

    No. A name can't start with a number.

    Being meaningful is not the same as being allowed: the naming rules are non-optional, so a clear name can still be illegal.

The big picture

Code has two audiences: the computer, which only needs it to run, and people, who need to read, fix and change it. Comments, whitespace, indentation and well-chosen names are how you look after the people. In Python indentation also decides how the program runs.

Key points

1The computer doesn't care how code looks, so readability is for people, including you later.
2Comments are ignored when the program runs, and blank lines separate code without changing what it does.
3In Python, indentation is syntax: it changes which lines run and when.
4A name can be legal but meaningless (x), or meaningful but illegal (2nd_score).
5Maintainability is the ease with which maintenance, such as correcting errors, can be performed.

Worked example

Problem

This program works, but it is hard to read. Improve its readability without changing what it does. h=float(input("Height in metres: ")) if h>=1.2: print("You can ride") else: print("Too short")

⚠ Watch out

Thinking "it runs, so it's finished". A name can run and still be a poor name (x), a name can be meaningful and still be illegal (2nd_score), and in Python indentation is never just decoration.

🧠

Memory hook

Write for the next human who opens the file. The computer copes either way, except in Python, where the indentation is both the layout and the logic.

✓

Check yourself

Take x = int(input()) in a program with no comments. Name one change that would help a reader and say what it tells them.

Flashcards

(13)
What is good programming practice?
Steps taken to make code easier to read and understand for anyone who may need to improve, use or maintain it later.
Name four good programming practices.
Comments, program layout, identifiers and naming conventions.
Why does readable code matter?
Engineers work in teams, others may need to adapt it, and the original programmer may find it hard to understand after time away.
What is maintainability?
The ease with which maintenance, such as correcting errors, can be performed.
What is a comment?
Text in the source code that the compiler or interpreter ignores. It makes the code easier for a programmer to understand.
What should a comment explain, and where can it go?
Parts that are not obvious, or a summary of a subroutine or piece of code. At the end of the line it relates to, or on the line before.
What is whitespace used for in layout?
Return and space characters separate code, for example an assignment from the selection blocks that follow.
How does indentation differ between Python and other languages?
In Python it is essential syntax that decides how the program is executed. In other languages it is not required, but code without it is harder to read.
What is an identifier?
The name a programmer gives to a variable, constant or subroutine.
What does self-documenting code mean?
The code explains itself, because meaningful identifiers show what a variable holds and how it is used.
Give a better identifier than h for a height in feet.
height_in_feet. A reader can see what it holds. Likewise score is better than x.
What is a naming convention, and which parts are non-optional rules?
A standard way to write identifiers. Names cannot include spaces or start with a number.
What is Python's convention for variable names?
Lower case with underscores separating words (snake case), for example last_name or pupil_id.

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 Edexcel GCSE Computer Science topics

See the full Edexcel Computer Science curriculum →

How this lesson was checked. This Edexcel GCSE Computer Science (specification 1CP2)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 21 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.