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 you make it easy to read.
Key words
| Word | What it means | Example |
|---|---|---|
| Naming convention | A set of rules for how names are written. | |
| Meaningful name | A name that says what the value is or what the code does. | total_price |
| snake_case | Small (lower-case) letters, with an underscore _ between words. | total_price |
| camelCase | No spaces. Each new word starts with a capital letter. | totalPrice |
| White space | Spaces, blank lines and indents that make code easy to see. | |
| Line length | How many characters are on one line. | |
| Readability | How easy the code is to read and understand. | |
| Variable | A name that holds a value. | |
| Function | A named piece of code that does a job. |
The cells students fill in are left blank in this preview.
Run it: hard to read
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?
Make a free account to save your work and track your progress.
Run it: easy to read
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.
Make a free account to save your work and track your progress.
A good name says what a value is
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
| Convention | How it looks | Where it is used |
|---|---|---|
| snake_case | total_price, first_name | Python: the Python style guide asks for it |
| camelCase | totalPrice, firstName | JavaScript and Java, for variables and functions |
| Mixed styles | total_Price, TotalPrice2 | Nowhere: pick one style and use it every time |
The cells students fill in are left blank in this preview.
Match camelCase to snake_case
Have a go
Loading…
Your answers are marked here and are not kept. A college account keeps every student’s marks.
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
Tip
Some teams use a longer line, such as 100 characters. The exact number matters less than everybody using the same one.
Tidy it up
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.
Make a free account to save your work and track your progress.
Watch: names and style
Press Play on the short film below. It runs the two programs from the start of the lesson, shows the two naming styles, and tidies up a function. Watch what changes for Python and what changes for you.
Film: names and code style
Names and code style
Every scene, as text
- Myth (false): Python needs good names to run the code. The lesson's program with x, y, z and d, and no spaces round its signs, runs and prints Pay 11.5. It works, but what is x? What is d? Nobody reading it can tell what it is for.
- The same program with meaningful names: tickets, fare, total and discounted_total. It prints the same Pay 11.5, and now people can read the code and fix it.
- Two naming conventions. snake_case, small letters with _ between the words, is what Python's style guide asks for. camelCase, each new word after the first starting with a capital letter, is used in JavaScript and Java. A mix such as total_Price is used nowhere, and Python reads it as a different name from total_price: pick one style and use it every time.
- White space is free. The tidy-up's function, f(a,b) with c=a*b, rewritten: meaningful names, spaces around = and *, an indent of 4 spaces, and blank lines after the function. It still works out 17.5: style is for people, not for Python.
- Long lines are hard to read: the Python style guide says 79 characters at most, and a long line makes the reader scroll sideways. The tidy-up's long print, with the names from the function before, is 107 characters on one line, so it is broken inside its brackets onto shorter lines. It still prints one line: 17.5 is the cost of 7 tickets at a fare of 2.5 each, which is a lot.
Quick check: names and style
Have a go
Quick check: names and style
4 questionsTry it: first letter and the rest
word = "price"
print(word[0])
print(word[1:])
print(word[0].upper() + word[1:])
print("total_price".split("_"))
# word[0] is the first letter. word[1:] is everything after it.
Make a free account to save your work and track your progress.
Your turn: change the style
Write three functions about naming styles. to_snake_case(name) changes camelCase into snake_case. to_camel_case(name) changes snake_case into camelCase. is_snake_case(name) is True when the name has only small letters, digits and underscores, is not empty and does not start with a digit. Press Run Tests to check.
def to_snake_case(name):
# Change camelCase into snake_case.
# "totalPrice" becomes "total_price". "age" stays "age".
# Hint: a capital letter becomes an underscore and a small letter.
return None
def to_camel_case(name):
# Change snake_case into camelCase.
# "total_price" becomes "totalPrice". "age" stays "age".
# Hint: use split("_"). word[0] is the first letter. word[1:] is the rest.
return None
def is_snake_case(name):
# True if the name is not empty, does not start with a digit, and has only
# small letters, digits and underscores. "total_price" is True. "totalPrice" is False.
return None
Make a free account to save your work, or press Start lab to work through the whole lesson.
Key words crossword
Have a go
Loading…
Your answers are marked here and are not kept. A college account keeps every student’s marks.
Exit quiz
Have a go