GCSE · Computer Science · OCR · Spec J277

Maintainability: commenting

Months later your program breaks. You open it and think: who wrote this?! You did. Comments are notes from today-you to tomorrow-you.

Computer Science · Commenting

Which comments are worth writing?

Every comment below was written for one short subroutine, bookingCost(tickets, isMember), which works out what a cinema booking costs. The detail shows the line each comment sits with. Sort each one: what is it actually doing for the reader?

Still to sort

Explains a non-obvious part (0)

Tells the reader something the code alone doesn't make clear.

Where the line is: If you could work it out just by reading the line, it isn't explaining anything. It belongs in 'adds nothing'.

Summarises a subroutine or block (0)

Says in one sentence what a whole chunk of code is for.

Where the line is: A summary covers several lines at once; an explaining comment is about one tricky line.

Adds nothing the code doesn't already say (0)

Correct, but it just repeats the code in words.

Where the line is: Being accurate isn't enough. A comment has to tell the reader something they couldn't already see.

6 of 6 still to sort.

Here's the big idea: the compiler or interpreter ignores every comment. They're written for the programmer reading the code. So the only question about a comment is: does it help that reader?

Watch out: A comment can be completely true and still be useless. If it just says what the line already says, it tells the reader nothing new.

Comments and maintainability

?

Reason it through

Why does commenting your code make it easier to maintain?

Link 1 of 3

First link · your turn

A programmer opens some well-commented code to work out what it does. What do the comments do for them?

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

Placing comments

Which comment is in the wrong place?

This procedure updates a player's score at the end of a level. Every comment is true, but one is placed so it seems to belong to the wrong line. Find it.

A student's code — which line goes wrong?

Do I really need comments?

"It's my code. I'll remember."

Maya is writing a program just for herself. "Comments are for other people," she says. "I wrote it, so I'll always know how it works."

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

Your turn to write

Put it in your own words

Explain how adding comments to a program improves its maintainability. [3 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.

Notes in your code that the computer ignores, but every programmer who reads it (including future you) will thank you for.

What you need to know

  • A comment is text in your source code that the compiler or interpreter completely ignores.
  • Comments are written for people: they help programmers understand the code.
  • Have a goPredict: you add fifty comments to a program that already works. Does it run any differently?

    No. It runs exactly the same.

    The compiler or interpreter ignores comments, so they change nothing about how the program runs. They only change what a human reader sees.

  • That means a comment earns its place by helping the reader understand something.
  • Use comments to explain parts of the code that aren't obvious.
  • Use them to summarise what a subroutine, or a particular piece of code, does.
  • Have a goYour mate Sam writes score = 0 and adds // set score to 0. "Best comment ever," he says. Is he right?

    Not really. It adds nothing.

    It's accurate, but the code already says exactly that. A comment earns its place by explaining something non-obvious or summarising a chunk of code.

  • Put a comment at the end of the line it's about, or on the line immediately before it.
  • Space comments carefully, so it's obvious which comment goes with which line of code.
  • Have a goA comment explaining line 5 has ended up on line 6, directly above line 7. Which line will a reader probably think it's about?

    Line 7.

    A comment sits at the end of its line or on the line immediately before it, so one sitting above line 7 looks like it belongs to line 7, not line 5.

  • Readable code is easier to maintain. Maintainability means how easily maintenance, like correcting errors, can be done.
  • Skip comments and code gets hard to maintain or fix, and other programmers may not understand it.
  • Even the person who wrote the code may struggle to understand it after time away from it.

The big picture

A comment is text in source code that the compiler or interpreter ignores; it's there to help programmers understand the code. Good comments explain the parts that aren't obvious, or summarise a subroutine or a piece of code, and they sit at the end of the line they relate to or on the line immediately before it, spaced so it's clear which line they belong to. That makes code more readable, so it's easier to maintain. Maintainability is how easily maintenance, such as correcting errors, can be done. Without comments, code gets hard to maintain or fix, for other programmers and for the original programmer too.

Key points

1The compiler or interpreter ignores comments: they're written for programmers, not the computer.
2A good comment explains a non-obvious part, or summarises a subroutine or a piece of code.
3A comment goes at the end of its line or on the line immediately before, spaced so it's clear which line it belongs to.
4Comments make code more readable, so it's easier to maintain; without them, code is hard to fix and hard to understand, even for whoever wrote it.

Worked example

Problem

This subroutine has no comments. Decide where comments would genuinely help, and write them. function canRide(height) if height >= 120 then return true else return false endif endfunction

⚠ Watch out

Thinking a comment is useful just because it's accurate. print(name) // print the name is true, but it only repeats the code. A useful comment explains what isn't obvious, or summarises a subroutine or a piece of code.

🧠

Memory hook

Write every comment for future you. The computer skips it; future you is the one who'll read it.

✓

Check yourself

Open a program you've written. Add a comment where a line isn't obvious, then delete one that only repeats its line.

Flashcards

(8)
What is maintainability?
The ease with which maintenance activities, such as correcting errors, can be performed on code.
What is a comment?
Text within source code that is ignored by the compiler or interpreter, used to help programmers understand the code.
How do comments make code easier to maintain?
They make the code more readable, so a programmer trying to understand it can, and understood code is easier to maintain.
Name the two jobs a useful comment does.
It explains a part of the code that isn't obvious, or it summarises a subroutine or a particular piece of code.
Where can a comment be placed?
At the end of the line it relates to, or on the line immediately before it.
Why does careful spacing of comments matter?
It makes clear which comment goes with which line of code.
Give three consequences of not commenting code.
The code may be difficult to maintain or fix; other programmers may not understand it; the original programmer may find it hard to understand after time away.
What's wrong with a comment that just repeats what its line does?
It adds nothing. The code already says it, so the reader learns nothing new.

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.