player_score and lives read like English — anyone glancing at the Stage knows exactly what each number means.Learning Goals 3 min
By the end of this lesson you will be able to:
- Tell the difference between a good variable name (
score,lives,time-left,cat-speed) and a bad one (x,a,var1,score2), and explain why. - Use the lowercase-with-hyphens style for two-word names (
cat-speed, notCat Speedorcatspeed) so your scripts read cleanly in joins and say-blocks. - Use the Rename variable right-click action on an existing variable to fix a bad name without breaking any block that already uses it.
Warm-Up — read Aljay's script 7 min
Aljay shares his Scratch project with you the night before it's due. You open it. The cat sprite has this stack:
when flag clicked
set [x v] to (0)
set [a v] to (3)
set [var1 v] to (100)
forever
if <touching [Sprite2 v] ?> then
change [x v] by (1)
change [a v] by (-1)
end
end
You want to help him fix a bug, but you have no clue what these variables represent. Is x the cat's score? The cat's x-position? Aljay's age? What's a? Apples? Attacks? Lives?
Reveal: what Aljay actually meant
Aljay told you over WhatsApp: x is score, a is lives, var1 is the starting timer in seconds. The script works fine for Aljay, because he wrote it and remembers — but for anyone else (including future-Aljay six weeks from now), the script is a puzzle. Today's lesson is the cheapest, easiest, most professional habit in programming: name the variable what it actually is.
You'd never name your dog "Animal" or your sister "Person2". Variables are the same. The name is the documentation.
New Concept — the name is the only clue 15 min
Scratch doesn't care what you call a variable. x, banana, WhyAmIHereLol — all valid. The code runs the same. So you are not picking the name for Scratch. You're picking it for the next person who reads your script. That person is almost always future-you three weeks from now, after you've forgotten what you were thinking. Sometimes it's your teacher grading the project. Sometimes it's a teammate.
Good names answer "what does this hold?"
A good variable name should let someone read one block and know exactly what's going on. Compare:
change [a v] by (1)
a? You'd have to scroll through the project to figure it out.
change [score v] by (1)
Some good names, some bad ones
Here's a comparison table you can copy into your notebook:
- Good:
score,lives,time-left,cat-speed,player-name,high-score,bombs-remaining,current-level. - Bad:
x(means nothing),a(means less than nothing),var1(it's literally the default),score2(which score? what's wrong with score 1?),thing(vague),n(mathy but cryptic),temp(only ok for ten seconds),data(please no).
Notice the bad ones aren't wrong in Scratch — they all work. They're just unkind. They make the reader work harder than necessary.
Two-word names: use lowercase-with-hyphens
A lot of the best names need two or three words: a "high score" or "time left" or "cat speed". Scratch lets you type spaces in names — high score is allowed — but spaces look messy when you drop the variable inside other blocks:
say (join [Your score is ] (high score))
say (join [Your score is ] (high-score))
Lowercase is also kinder than capitals. CatSpeed, Cat_Speed, catSpeed all work but feel like shouting. cat-speed reads like a label, which is what it is.
Rename, don't restart
What if you've already built half a project with bad names? Don't panic and don't make a new variable. Right-click the variable in the Variables palette (or right-click its watcher on the Stage) and pick rename variable. Type the new name. Scratch updates every block in your project that uses that variable, automatically. Nothing breaks.
change [x v] by (1)
change [score v] by (1)
x to score. Same block, every change [x v] by () in the whole project becomes change [score v] by () at the same time. No script-hunting needed.Worked Example — fixing Aisyah's mystery project 12 min
Aisyah hands you her project. The watchers say x: 0, a: 3, var1: 60. You're going to rename them all in seven steps, without breaking a single block.
x, a, var1 — get renamed to score, lives, time-left. Renaming updates every block in the project at once (see the ✏️ badge).Step 1 — Open the project and look at the scripts
Open Aisyah's .sb3. Click each sprite. Read the scripts. From context (where the blocks are used), you figure out: x is the score, a is lives, var1 is the time-left counter.
Step 2 — Open the Variables palette
Click the orange Variables palette on the left. You see x, a, var1 listed.
Step 3 — Right-click "x"
Right-click on the word x in the palette (or on the x watcher on the Stage). A small menu opens. Pick rename variable. Type score. OK.
Look at any script that used x — it now says score. Scratch updated every block. No script broke.
Step 4 — Rename "a" to "lives"
Same again. Right-click a → rename variable → type lives → OK.
Step 5 — Rename "var1" to "time-left"
Right-click var1 → rename variable → type time-left (lowercase, hyphen, no spaces) → OK.
Step 6 — Re-read Aisyah's main script
Click the cat. Look at the script you couldn't understand before. Now it reads:
when flag clicked
set [score v] to (0)
set [lives v] to (3)
set [time-left v] to (60)
forever
if <touching [Sprite2 v] ?> then
change [score v] by (1)
change [lives v] by (-1)
end
end
Step 7 — Use the new names in a say-block
Add a quick announce to the cat so the new names show up in the player's speech too:
when this sprite clicked
say (join [Score: ] (score)) for (2) seconds
What you just did: made a project that's twice as readable in about ninety seconds, without touching a single script. Renaming is the highest-leverage edit in all of programming. Free quality.
Try It Yourself — three naming drills 15 min
Goal: In a fresh project, make four variables with good names: score, lives, level, high-score. Use the lowercase-with-hyphens style. Then build a tiny stack that resets all four at the flag.
when flag clicked
set [score v] to (0)
set [lives v] to (3)
set [level v] to (1)
set [high-score v] to (0)
Think: Even just the dropdowns now read like a real game's UI. score, lives, level, high-score — anyone walking past your laptop knows what this is.
Goal: Find a bad-name project. Open the one you (or a friend) built before — or take Aljay's script from the warm-up — and rename every poorly-named variable. Use the Rename variable right-click action. Don't delete anything. Then write a one-line "before / after" note for each rename.
when flag clicked
set [bombs-remaining v] to (5)
set [player-speed v] to (10)
forever
move (player-speed) steps
if <touching [Bomb v] ?> then
change [bombs-remaining v] by (-1)
end
end
Think: The same script with n and s would be a mystery. With bombs-remaining and player-speed it tells its own story. The name is the story.
Goal: Priya is building a teh-tarik shop simulator and emails you her variable list: p, x2, thing, data, z. She tells you they mean: price-per-cup, cups-sold-today, current-customer-name, total-money-earned, and minutes-since-opening. Build a fresh project with five well-named variables and one stack that uses all five in a say block.
when flag clicked
set [price-per-cup v] to (4)
set [cups-sold-today v] to (0)
set [current-customer-name v] to [Aisyah]
set [total-money-earned v] to (0)
set [minutes-since-opening v] to (0)
say (join [Hi ] (current-customer-name)) for (2) seconds
say (join [We've sold ] (cups-sold-today)) for (2) seconds
say (join [Earned RM ] (total-money-earned)) for (2) seconds
Think: The say-blocks practically write themselves once the names are good. current-customer-name goes straight into "Hi ___" with zero confusion. Bad names would force you to write a comment next to every block.
Mini-Challenge — Daniel's two scores 5 min
"Why does Daniel have score1 and score2?"
Daniel built a two-player ping-pong game. He needs two scores — one for each player. So he made two variables: score1 and score2. Here's his stack:
when flag clicked
set [score1 v] to (0)
set [score2 v] to (0)
forever
if <touching [Player1Goal v] ?> then
change [score2 v] by (1)
end
if <touching [Player2Goal v] ?> then
change [score1 v] by (1)
end
end
What would you rename score1 and score2 to? And how would you rename them without breaking the script?
Reveal one good answer
Rename score1 to player-1-score and score2 to player-2-score. Or even better, if the two players have names: aisyah-score and daniel-score. The fix: right-click each variable in the Variables palette, pick rename variable, type the new name, OK. Every block updates at once.
when flag clicked
set [player-1-score v] to (0)
set [player-2-score v] to (0)
forever
if <touching [Player1Goal v] ?> then
change [player-2-score v] by (1)
end
if <touching [Player2Goal v] ?> then
change [player-1-score v] by (1)
end
end
Look how much easier the bug-spotting just got — you can see that touching Player 1's goal increases Player 2's score, which is the correct ping-pong rule. With score1 and score2 that read had to happen in your head; with the new names the script reads itself. Good names turn debugging into reading.
Recap 3 min
You learned that variable names are for the human reader, not for Scratch. Good names describe what the variable stores — score, lives, time-left, cat-speed — and read like English when dropped into other blocks. Bad names (x, a, var1, score2) work but force everyone who reads your project to guess. Use lowercase-with-hyphens for multi-word names so they look tidy inside join and say blocks. And when you find a bad name in old code, right-click → rename variable fixes every block in the project at once — no script-hunting required.
- Descriptive name
- A variable name that tells the reader what's stored.
score,lives,player-speed. The opposite of a placeholder name likexorvar1. - Lowercase-with-hyphens
- The naming style used throughout this curriculum: all letters lowercase, hyphens between words (
cat-speed, notCat SpeedorcatSpeed). Tidy inside joins, easy to read, never-shouting. - Rename variable
- The right-click menu action in the Variables palette (or on a Stage watcher) that changes a variable's name and updates every block in the project that uses it. Safer than deleting and re-creating.
- Self-documenting code
- Code that explains itself because its names are so clear no comments are needed. Good variable names are the easiest way to write self-documenting Scratch.
Homework 2 min
The Name-The-Game Drill. Build a tiny "shopkeeper" project with five well-named variables.
- Make five variables, all lowercase-with-hyphens: items-sold, money-earned, customer-name, shop-open, current-item-price.
- Reset all five at the flag (set [items-sold v] to (0), etc.; for
customer-nameuse set [customer-name v] to [nobody]; forshop-openuse0). - When the space key is pressed: change [items-sold v] by (1) and change [money-earned v] by (current-item-price).
- When the cat is clicked: it should say (join [Total earned: RM ] (money-earned)) for (2) seconds.
- Now deliberately rename one variable using the right-click rename variable action — change
items-soldtocups-sold. Watch every block update.
Save as HW-L2-33-Shopkeeper.sb3. Click the flag, press space a few times, click the cat to hear the total. Everything should read like a sentence.
Bring back next class:
- The
.sb3file. - Your answer to: "Why is
cups-solda better name thancsorn? Write two sentences using examples from your own project."
Heads up for next class: SCR-L02-34 meets a big choice that hides inside the Make-a-Variable dialog — the checkbox For all sprites vs For this sprite only. It changes who can see your variable, and gets a lot of multi-sprite games into a lot of trouble.