Meaningful identifiers for variables and constants

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

Meaningful identifiers for variables and constants

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which identifier is better for a variable that stores the total cost of a customer's shopping?
A TotalToPay
B T
2. Which identifier is better for a variable that stores how much an employee earns per hour?
A HourlyRate
B X
3. Why should a programmer choose meaningful identifiers?
A They make the program run faster
B They make the program easier to read and maintain
4. Which line is easier to understand?
A T ← P * Q
B TotalToPay ← Price * Quantity
5. When is a single-letter identifier acceptable?
A As an index in a loop, such as i or j
B For a value such as a customer's total bill

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

Left columnRight column
1DECLARE TotalToPay : REAL AA meaningful identifier: the name tells the reader the variable holds the total amount a customer has to pay
2CONSTANT HourlyRate ← 12.50 BT is a poor identifier because it does not say what it holds. Rename it to something like TotalToPay so the purpose is clear.
3A single letter name is acceptable when it is conventional, such as i as an index counting through an array CX ← X + 1
4Single letters are also conventional for coordinates, such as X and Y for a position on a grid DA meaningful constant name that says the fixed value is the pay for each hour worked
5T ← Price * Quantity EFOR i ← 1 TO 10
6X ← Hours * Rate FX is used for a pay amount, but X normally means a coordinate. Rename it to something like TotalPay to show what it holds.

Definitions

meaningful identifier

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
CONSTANT VatRate ← 0.2 Price ← 50 TotalToPay ← Price + Price * _______
2.
HourlyRate ← 15 HoursWorked ← 40 __________ ← HourlyRate * HoursWorked
3.
X ← 4 // horizontal position _ ← 7 // vertical position OUTPUT X, Y
4.
Hours ← 8 __________ ← 12.5 Pay ← Hours * HourlyRate
5.
CONSTANT PassMark ← 50 INPUT Mark IF Mark >= ________ THEN

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A DECLARE HourlyRate : REAL
B DECLARE TotalToPay : REAL
C HourlyRate ← 12.5
D X ← HourlyRate * 8
Question 2
A X ← 12.5
B Hours ← 8
C Wage ← HourlyRate * Hours
D OUTPUT Wage
Question 3
A T ← 0
B TotalToPay ← TotalToPay + 4.5
C OUTPUT TotalToPay
Question 4
A DECLARE Name : STRING
B INPUT Name
C X ← LENGTH(Name)
D OUTPUT NameLength
Question 5
A CONSTANT VatRate ← 0.2
B INPUT Price
C A ← Price * VatRate
D OUTPUT VatAmount

Answer Key

Meaningful identifiers for variables and constants
Section A: Quick Questions
Q1
A — TotalToPay
Q2
A — HourlyRate
Q3
B — They make the program easier to read and maintain
Q4
B — TotalToPay ← Price * Quantity
Q5
A — As an index in a loop, such as i or j
Section B: Match the Pairs
Q1
DECLARE TotalToPay : REAL → A meaningful identifier: the name tells the reader the variable holds the total amount a customer has to pay
Q2
CONSTANT HourlyRate ← 12.50 → A meaningful constant name that says the fixed value is the pay for each hour worked
Q3
A single letter name is acceptable when it is conventional, such as i as an index counting through an array → FOR i ← 1 TO 10
Q4
Single letters are also conventional for coordinates, such as X and Y for a position on a grid → X ← X + 1
Q5
T ← Price * Quantity → T is a poor identifier because it does not say what it holds. Rename it to something like TotalToPay so the purpose is clear.
Q6
X ← Hours * Rate → X is used for a pay amount, but X normally means a coordinate. Rename it to something like TotalPay to show what it holds.
Section C: Definitions
Q1
meaningful identifier — A name that describes what a variable or constant holds, such as TotalToPay instead of T, so the program is easy to read and maintain.
Section D: Fill in the Blanks
Q1
CONSTANT VatRate ← 0.2 Price ← 50 TotalToPay ← Price + Price * VatRate
Q2
HourlyRate ← 15 HoursWorked ← 40 TotalToPay ← HourlyRate * HoursWorked
Q3
X ← 4 // horizontal position Y ← 7 // vertical position OUTPUT X, Y
Q4
Hours ← 8 HourlyRate ← 12.5 Pay ← Hours * HourlyRate
Q5
CONSTANT PassMark ← 50 INPUT Mark IF Mark >= PassMark THEN
Section E: Spot the Error
Q1
D — X says nothing about what it holds. The variable declared to hold the final amount is TotalToPay, so the line should be TotalToPay ← HourlyRate * 8.
Q2
A — A single letter X does not tell the reader this is the pay per hour. It should be HourlyRate ← 12.5, which also matches its use on line 3.
Q3
A — T is a poor name because it does not describe the value. The variable should be TotalToPay so that every line of the program refers to the same meaningful identifier.
Q4
C — X does not describe the number of characters it stores. A meaningful name such as NameLength should be used: NameLength ← LENGTH(Name).
Q5
C — The single letter A hides that this value is the VAT charged. It should be VatAmount ← Price * VatRate so the program is easy to read.