Wednesday, November 20, 2013

The Problem With i

I had an interesting problem from a student the other day. He was working his way letter-Ithough an array in a couple of places and one of them was doing something wrong. It was tricky because most of what was happening was correct but sometimes it wasn’t. Drove us both crazy. As you might expect there were a couple of places where values were being changed but it looked consistent so the results should have been constant. But they weren’t. It took me a while to understand what was going on but I finally figured it out.

The index for a change was being calculated using two index values –> n and i. Pretty common stuff. Unfortunately one place was [n + 1] where it should have been [n + i] A lower case i is easy to mistake for a 1. So is an uppercase I or even a lowercase L. In fact there are a lot of numbers and letters that are easy to confuse. Zero and Oh for example. S and 2 for another. One solution is to avoid some letters completely. I know of places that ban the use of some easily confused letters in places where context does not make them stick out clearly.

For indexes in loops though the best answer is probably index control variables that are more than one letter long. Row and Column probably make better variable names than r and c and definitely better than i and n when you are indexing through a grid or a table for example. Index is probably a lot better than I as well. But most of us are lazy so we stick to single letter loop control variables far too often.

It’s not a problem until it is a problem. When it is a problem renaming the loop control variables is probably a good way to make some sorts of errors in logic pop out. Something we’ll be talking about in class this week. I love the teachable moment.


dougpete said...

Let's through "l" into the mix and it gets even worse! I think many of us who cut our teeth with Fortran use i for the first index, then j, then k just because of the integer naming rules.

I moved to variable names like "index" and "counter" which made things so much easier to debug. Particularly for the early learner, your example is the perfect reason for using meaningful variable names.

Mike Zamansky said...

Doug - glad I'm not the only dinosaur out there.

A couple of other issues include font and how much to expect from your editor.

On font - I can't use l for list even in simple examples because it visually looks like a 1 or an I so I have to either break convention and use L or use something longer (sometimes this makes sense, sometimes it doesn't).

As far as the editor goes - now, with autocompletion and the like, we can just type the first couple of letters of our variables and have the editor do the rest. I have mixed feelings as to whether this is a good idea or not.

Alfred Thompson said...

I also started with FORTRAN. I suspect that a lot of what is "normal" is a result of people who either started with FORTRAN or learned programming from someone who started with FORTRAN. It's probably up to us to "fix" the problem.

Unknown said...

The residuals of old computer languages are pervasive. FORTRAN lives ... argh! This is the same as a math student using 'i' as a variable ... not a good practice since 'i' is the imaginary number of a complex number. And there are the electrical engineers who use 'j' for the imaginary component of a complex number cause 'i' is something else. :) How do you explain this to students? :)

Garth said...

What a bunch of old farts we are. I started in FORTRAN and still use the i,j,k indexing for loops. I definitely need to break the habit. Now that I think of it I do not know any "young" programming teachers. That must mean something significant. Come to think of it I do not know many programming teachers at all.

Alfred Thompson said...

Garth, one day I have to find a way to get you to a CSTA annual conference. Even without the sessions the conversations and networking with other computer teachers would be wonderful for you.

Guy Haas said...

I did a lot of programming in DEC10 assembler with maximum of 6 characters for all labels.

I use a lot of 3 character names (e.g., idx, row, col). In addition to all that's been said, the editors I use highlight all matches when a search is performed. So when looking at say a for-loop, if you search for "i" most of the time it's a disaster; but if you search for "idx" you see all uses of the index variable. This makes understanding the loop a lot easier.