AdvertisementYour brand could be hereYour brand hereOn every Darslab page · students, parents and teachersinfo@darslab.comAsk for pricesPricesinfo@darslab.com
☰ Year plan · Computing

Variables and values

Week 8 · Unit 4: Be a storyteller · 3 lessons
2

Choosing clear names

Computing 5: Unit 4, pp. 44–57
GoalChoose clear, meaningful names for variables and use each name in exactly the same way every time.
New words
variable name · the label that tells us which variable we are usingmeaningful · giving a clue about what it is forunique · different from every other name in the programpurpose · what something is for
Explanation

A variable name should tell us the purpose of the variable. A good name makes a program easy to understand for anyone who reads it, including you next week. Good names are coins, lives and player name. Poor names are x, a or thing, because they tell us nothing. Each variable must have a unique name, or we cannot tell them apart. A name should be short, not a whole sentence. And a name must be written the same way every time: the same letters, the same capital letters and the same spaces. In Scratch you pick the name from a list, which helps, but in an algorithm you must take care.

Poor nameBetter nameWhy
xlivestells what it counts
acoinstells what it stores
thingplayer nametells the purpose
number of coins the player has found so farcoinsshort is easier to read
Worked examples
whenclickedsetcoinsto0setlivesto3
A game has two numbers, so it needs two variables with different names. Here the names coins and lives tell us what each one is for.
StepInstruction
1Start
2Set Points to 10
3Change Points by −4
4Change Points by −3
5Output points left
A reader can be confused by a name that changes. This algorithm uses ‘Points’ in step 2 and ‘points left’ in step 5. They may be two variables, or one variable written twice. Use one name, such as ‘Points’, in both steps.
The name of a variable does not have to be long to be clear. ‘Time left’ is better than ‘t’, and it is better than ‘the number of seconds that remain before the game ends’.
Class activity

Name detective (pairs). Write six things on slips of paper: how many apples are in the basket, the name of a pet, how many minutes are left, the winner’s name, the number of steps taken, and the height of a plant. Each player gives every slip a clear name of one or two words. Swap with your partner. Can they tell what is stored, just from the name? Rename any that confuse. Which names were easy and which were hard?

Practice
1
Choose the better name for each: (a) n or apples, (b) t or time left, (c) stuff or player name.
Name 1Name 2
anapples
bttime left
cstuffplayer name
2
Give two reasons why variables should have clear names.
3
A pupil makes two variables for a two-player game, and calls both ‘score’. Why is this a problem? Suggest two better names.
4
Find the mistake in this algorithm.
StepInstruction
1Create a variable called Lives
2Set Lives to 3
3Change Life by −1
4Output Lives