Friday, February 01, 2019

What is a variable and why is it so hard to understand?

Variables in computer science are complicated. No, really they are. I didn’t consciously  realize how complicated until I read “What is a variable?” on the blog at CACM. Reading that article, which was not an easy read for me, did give me some insight as to why philosophy majors so often make good software developers. I’m not sure I want to have my beginning students read it though. It may leave them with more questions than answers. Maybe in an advanced placement course?

When teaching beginners we need to keep things simple and we have to remember that they don’t have the same base set of knowledge that more experienced people have. So what then is a variable?

A variable name is first of all an identifier. We have lots of identifiers in computer programming. Method names, object names, class names, and variables which are identifiers for something. What is that something?

I like to talk about variables as locations in memory. I used to say boxes but I’m not sure that really works. Students know, or should, that everything is stored in memory as ones and zeros so suggesting a location in memory feels more logical to me. The obvious next question is how big is that location. The answer depends on what is stored in it.

I talk about parking spaces in a parking lot. There are different sizes of spaces: Motorcycles, compact cars, regular cars, and trucks and busses. Spaces are all different sizes and the most efficient use of them is to hold the vehicles they were designed to hold. A motorcycle in a spot for a semi-trailer truck is wasting space. A large truck will overflow as space for a compact car. Size of the memory location depends on what is stored in it. Space can be wasted if a larger space than is needed is allocated.

Size and type are then related attributes or properties of the memory location named by the variable name. So is the variable the location or the name? Does it really matter? Probably if you want to get into the philosophy behind it all but not something beginners are really ready to get into. Maybe it is enough that they know how to declare and use a variable in the first course.

In many ways the first programming course is the hardest to teach and perhaps the hardest for many students to take. Finding that line between deep understanding and too much information to handle at one time is not easy.

2 comments:

Anonymous said...

Then we talk about OOP variables that contain more than one type of item. Then we discuss making their own types which can store all sorts of things. Now it is really ugly. When I was teaching with Small Basic and Scratch variables were "easy". Now not so much. Those parking spaces have semi trucks in them and the semis are just full of other stuff.

Brian Sea said...

Huh... I talk about abstraction layers with a variable being the second layer (above literals). The analogy I use is a sign on the highway that say "Oakland 50" or a person's name. Each of them is tells is how to find the literal (Oakland or the person themselves). I then should students how to turn repeated changing literals into a variable (pattern matching). This gives students a procedure to follow until the concept actually sinks in.

It takes a bit but students have a much easier time with inheritance and polymorphism when this analogy is used.