I had an interesting problem from a student the other day. He was working his way though 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.