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

Preview

Names and code style

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

40 minutes

Which program would you fix?

Two programmers write the same program. One uses the names x, y and z. The other uses tickets, fare and total.

Both programs have a bug and you must fix one today. Which one would you rather fix? Code is read many more times than it is written. Style is how we make it easy to read.

Key words

WordWhat it meansExample
Naming conventionA set of rules for how names are written.
Meaningful nameA name that says what the value is or what the code does.total_price
snake_caseSmall (lower-case) letters, with an underscore _ between words.total_price
camelCaseNo spaces. Each new word starts with a capital letter.totalPrice
White spaceSpaces, blank lines and indents that make code easy to see.
Line lengthHow many characters are on one line.
ReadabilityHow easy the code is to read and understand.
VariableA name that holds a value.
FunctionA named piece of code that does a job.

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

Run it: hard to read

Code: python
x=5
y=2.5
z=x*y
d=z-1
print("Pay",d)

# It works. But what is x? What is d?
# Can you tell what this program is for?

Run it: easy to read

Code: python
tickets = 5
fare = 2.5
total = tickets * fare
discounted_total = total - 1
print("Pay", discounted_total)

# The same program. Now the names explain what it does.

Meaningful names

A good name says what a value is. fare is better than f. is_late is better than flag.

Do not make names too long. number_of_tickets_the_customer_wants is too much. A name has no spaces and does not start with a digit. Python sees Total and total as two names.

Two naming conventions

ConventionHow it looksWhere it is used
snake_casetotal_price, first_namePython: the Python style guide asks for it
camelCasetotalPrice, firstNameJavaScript and Java, for variables and functions
Mixed stylestotal_Price, TotalPrice2Nowhere: pick one style and use it every time

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

White space and line length

White space is free. Use it. Put spaces around symbols such as = and +: total = a + b. Leave a blank line between two functions. Indent by 4 spaces.

Keep lines short. The Python style guide says 79 characters at most. A long line forces the reader to scroll sideways. Break it into two lines.

A team picks one style

data-callouts.tip.label

Some teams use a longer line, such as 100 characters. The exact number matters less than everybody using the same one.

Tidy it up

Code: python
def f(a,b):
  c=a*b
  return c
n=7
print(f(n,2.5),"is the cost of",n,"tickets at a fare of",2.5,"each, which is a lot")

# Rewrite this code. Use meaningful names, snake_case,
# spaces around signs, 4-space indents and two shorter lines.

Quick check: names and style

Have a go

Quick check: names and style

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.

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.