←Previous page

11CLEAN CODE · NAMES

Good names tell the story.

A box's name should say what it keeps, so anyone can read your code.

IN REAL LIFE

Labels help everyone.

A good label tells you what is inside before you open it.

School supplies

"Mia, math" on a notebook tells whose it is and what it is for.

Contacts in a phone

"Grandma" is much easier to find than "Contact 7".

Supermarket signs

Signs like "Fruit" and "Bread" help you find things fast.

LOOK FIRST

Three rules for names.

Say what it keeps

apples beats a, x or thing. You know what is inside without guessing.

Python style

Use lowercase words joined with _, like total_score.

Names have rules

No number at the start, no spaces. And score and Score are two different boxes.

LOOK FIRST

Read the names line by line.

Each name tells you what it keeps or what it does.

apples = 5A box called apples. You can tell it keeps apples.
pears = 3Another clear name: pears.
total_fruit = apples + pearsTwo words joined with _. It keeps 8.
def make_juice(fruit):A function name starts with a verb: it says what it does.

RUN IT

Names that explain themselves.

Guess first. Then run real Python and check.

1

Guess the result

player_name = "Mia" total_score = 10 + 5 print(player_name, "has", total_score, "points")

What will Python show?

names.pyREAL PYTHON
OUTPUT
Ready. Press Run Python.
Python runs here thanks to Pyodide ♥

Pyodide 314.0.3 runs CPython in your browser with WebAssembly.

Open-source under the Mozilla Public License 2.0. License

Now change one thing: Rename total_score to points. Change it in both places, then run again.

YOUR TURN

Pick the clearest name.

Try each tiny question. A wrong answer becomes something to review.

0Completed
9Not tried
0Review
01

Labels in real life

Three jars are full of cookies.

Which label is the best name?

02

Name the box

A box keeps how many apples you have.

Which name is best?

03

Python style

A box keeps the total score of a game.

Which name follows Python style?

04

A broken name

score2 = 10 2score = 10 score_2 = 10

Which line makes Python stop with an error?

05

A name with a gap

my pet = "Rex"
SyntaxError: invalid syntax

How do you fix it?

06

Big and small letters

score = 5 Score = 9 print(score)

What will Python show?

07

Name a function

A function says hello to a friend.

Which name is best for it?

08

Fix PyBot's code

There are 3 cats and 8 dogs, but the boxes a and b got mixed up. Give them clear names and match the goal.

Goal output
Cats: 3
Dogs: 8
OUTPUT

Your work is saved here.

Want a fresh start? Clear only this page.

!
BIG IDEA

Clear names make code easy to read.

Name each box for what it keeps, in lowercase with _. Start function names with a verb.