Readability
Readable code is code that another person can follow easily. It is written so the purpose of each part and the overall structure are clear, not just so the computer can run it.
Readable code is easier to read, understand and maintain. Maintenance covers correcting errors, updating a program when requirements change and reusing code later. If a program has to be changed, clear code saves time because you can find the right section quickly instead of working out what every line does.
Readability also helps when code is reviewed, tested, debugged or reused. Other programmers can check it, and so can you when you return to it weeks later. When you test a program, clear code lets you compare one section at a time with its expected output. When someone updates it later, they can change one part without having to decode every line.
Techniques such as layout, indentation, comments, meaningful identifiers and white space should make the purpose and structure of a program clearer. They change how the code looks to a reader, not what the program does. They cannot fix a logic error or add a feature; the instructions still have to be correct.
Good practice combines several techniques rather than relying on one. A comment is more useful beside a well-named variable inside an indented block than in a cramped, cryptic program. This short program is arranged so a reader can see its job, its input and its two outcomes.
# Saving for a trip
ticket_cost = 45
weekly_saving = 12
weeks_saved = int(input("How many weeks have you saved? "))
money_saved = weekly_saving * weeks_saved
if money_saved >= ticket_cost:
print("You can book the trip")
else:
print("Keep saving")Output when the user types 4:
How many weeks have you saved? 4
You can book the trip Edexcel GCSE Computer Science · Readability