Tuesday, October 21, 2014

Picking Good Variable Names

Actually picking any sort of name is important in programming but we can focus on variable names for a bit. The CS Teaching Tips team tweets and posts a lot of great suggestions. Today they send out a tip about variable names.

Avoid using x and y as variable names to prevent students from confusing variable assignments with mathematical expressions.

CS Teaching Tips Logo

I’ve been working on my variable naming for demos and sample code this semester. The x and y confusion is part of it. It’s tough because I learned in an era where short, even one character, names were the rule not the exception. Memory was expensive and long identifiers took up room in memory. Plus there was a certain amount of laziness on many people’s part. Well at least on mine. None of this is true anymore. Well that part about needing to save room with short variable names is no longer true.

Loop control variables in FORTRAN were almost always I, J, K, L, M, and N back in the 1970s. Variable names that started in those letters were automatically integers and so this was easy. Old habits die hard and I still tend to use those variables for FOR loops. I’m trying to get away from that in class.

I find that using “index” as a loop control variable, especially when the loop is iterating through a string or an array, makes things a bit more clear to students. It also helps reinforce the idea that variable names should have meaning. It’s hard to tell students, “yes I am using x, y, and I but you should all use meaningful names.” and be taken seriously. Using meaningful variable names in demos may take a little bit of extra effort but in the long run I think everyone is better off when teachers model the practice they try to teach.

CS Teaching TipsThere are lots more teaching tips at http://csteachingtips.org/ and you can also see tips from there on the side bar of this blog if you read it in a web browser. Follow @CSTeachingTips on Twitter too!

7 comments:

Anonymous said...

After years of grading AP exams ... it's also incredibly easy to interchange an "x" with a "y", or to misread one as the other.

Garth said...

Years ago I got in the habit of using the first three letters of a variable to give the type, i.e. intNumRabbits, strNameRabbit, etc. It makes life so much easier when I have to come back to an old program. I got away for that because Scratch and Small Basic do not declare types. Now that I am back to Java I need to use it again. I also use lower case first letter for variables and camel-hump notation. Anything to make life easier.

Mike Zamansky said...

Garth -- that's reminiscent of Hungarian notation.

Alfred --

Picking good variable names is important but i for a loop index makes sense and is intuitive. In fact, if I use index, it's a short order until the kids switch to i.

On the other hand x and y only really make sense when we're talking coordinates.

Then I've seen code with crazy long variable names and that's also not any better.

Maybe the answer is to have math people extend their alphabets.

Mike Zamansky said...

I remember back around 95 I was grading a USACO problem. One of the kids taking part was a super math prodigy.

He used variables like:
x
xx
xxx
xxxx
xxxxx
xxxxxx
etc.

It made sense to him. He also couldn't understand why I had to match his answers (really long strings of digits) with the actual answers. I think he figured that the rest of the world was as smart as him and could just see that his answers were right.

Alfred C Thompson II said...

I don't mind if students use single letters for loop control variables in their own programs. I just want to make sure the ones I use in demos are helpful and not confusing.

Unknown said...

I remember the one of my students saying with pride: "Wow, I made such a complex program today, I got all the way to the -n- variable!"

No matter how did I name the variables in the demos (sensibly, I agree with this post fully), I could not get him using anything else than the i, j, k... sequence (and yes, he punished himself more that once trying to understand and debug his own work).

Michael S. Kirkpatrick said...

And then there's the other bad practice that people don't talk about: typing. Consider this example:

for (int i = 0; i < some_value; i++) array[i] = 5;

If a variable should never become negative, don't use a variable type that can become negative. If some_value is 4,000,000,000, you now have an integer overflow triggering an exception because of an invalid index.

Far too many books, teachers, professors, and tutorials use "int" for any integer variable out of sheer laziness; it's shorter than typing "unsigned int" or "uint32_t." While it's generally not a problem for the code you're demonstrating, it trains students to do the exact same thing and use int everywhere...which leads to integer overflow bugs.