Easier to read with cream paper, a tint, more contrast or larger text? Choose your colours and text, once, for every page.

Preview

Coding conventions

This is the first page of the lesson. Read it and have a go at the activities. The rest is in the full lesson.

45 minutes

Can you read this?

Here is a line from a real program: x = a * b - c.

What is x? What are a, b and c? Nobody knows. The writer will not remember in six months.

Now read this: final_price = price * quantity - voucher. You understand it at once. The same code is easy to read when it follows good habits.

Key words

WordWhat it meansExample
VoucherA code or ticket that takes money off the price.5 pounds off
VariableA name that holds a value.price
FunctionA named block of code that does one job.total_price()
ConstantA value that never changes while the program runs.MAX_SIZE
ConventionA habit that a whole team agrees to follow.
Naming conventionRules for how to write the names of variables and functions.total_price
camelCaseA style where words are joined and each new word starts with a capital.totalPrice
CommentA note in the code for people. The computer ignores it.# add the tax
DocstringA note in triple quotes inside a function. It says what the function does.
ModularBuilt from small separate parts, each with one job.
IndentationSpaces at the start of a line. They show which lines belong together.
ReadabilityHow easy the code is to read.
MaintainabilityHow easy the code is to change later.
Version controlA tool that keeps every version of your files.

The cells students fill in are left blank in this preview.

Names that say what things are

A good name tells the reader what a value is or what a function does.

Use whole words: average_mark, not am. Use a verb for a function: calculate_total. Use a noun for a value: price.

A team also agrees one style. That is the naming convention.

Try it: before and after

Code: python
# BEFORE: it works, but it is hard to read
def f(a,b):
  x=0
  for i in a:
    x=x+i
  return x-b


# AFTER: the same job, easy to read
def total_price(prices, voucher):
    """Add up the prices and take off the voucher."""
    total = 0
    for price in prices:
        total = total + price
    return total - voucher


print(f([5, 3, 2], 4))
print(total_price([5, 3, 2], 4))

Common naming styles

StyleLooks likeOften used for
snake_casetotal_pricePython variables and functions
camelCasetotalPriceJavaScript and Java variables and functions
PascalCaseTotalPriceClasses in most languages
UPPER_CASEMAX_SIZEConstants: values that never change

The cells students fill in are left blank in this preview.

Spot the bad code

Have a go

Spot the bad code

4 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

Your answers are marked here and are not kept. A college account keeps every student’s marks.

Comments: say why, not what

A comment is a note for people. The computer ignores it.

Do not repeat the code. # add 1 above count = count + 1 says nothing new. Say why: # the first row is the header, so skip it.

A docstring goes inside a function and says what it does and what it gives back.

Small parts and clear layout

Modular code is made of small functions. Each does one job. You can test each one. You can reuse it.

Good structure helps too. Put imports at the top, then constants, then functions. Use the same indentation everywhere. Python uses four spaces. In Python it is not only style: the indentation is part of the language.

The lesson goes on

4 more parts of this lesson are in the full curriculum, with the tasks, the final check and the labs. A college account gives your students all of it.