Variables – Misconception: Students think computers have intelligence and so know what value should be associated with a variable name

It is a good idea to stress that variables should be named in a way that explains the purpose of those variables, but this sometimes leads students to make assumptions about the ultimate outcome of a program’s execution because variable names are, in reality, arbitrary. It is important that students do not assume the value of a variable based on the variable’s name. For example, they should not assume that because a variable is called max, it will hold some maximum value. The machine will process such a variable the same way regardless of the name.

The following code segments produce equivalent results even if they are not equivalent regarding style.

if(a > b):
	max = a
	max = b
print max
if(a > b):
	john = a
	john = b
print john
if(a > b):
	min = a
	min = b
print min

All three segments will print the larger of values a and b. The name of the variable does not affect the results. However, it should be noted that naming the variable max makes more sense and is better style than naming it john because john tells us nothing about what the variable represents, and calling the variable min would be confusing and suggests we were going for the minimum value and made a mistake in the code.

Often, variables get values from the user, and a programmer cannot control what a user inputs. Students should be aware of this when dealing with user input to ensure that values are valid and make sense in the context of the program. This is up to the programmer since a computer is not smart enough to deal with it. Video Length: 2:12