Never, ever again will I use the letter i as a counter in a loop

Ok, so I recently read an article, maybe by Joel Spolsky, about not using i, j and k as counters.  Those familiar constructs of

For i = 1 to Ubound(SomeArray())
….
next i

should be using a full variable name of some sort.

  The code I’m working on right now in the Auto FE Updater has a number of loops within loops.   As the outer loop got longer and longer I decided to change the use of the letter i to something more meaningful.  In this case DatabaseCounter.  No problem, just change the Dim statement, change the compile errors complaining about the letter i and done.  No problem.

(Several hours later)

Something isn’t working right as I’m doing my final testing.  I threw in debug statements here, there and just about everywhere.  I examined the code with the proverbial fine tooth.  Change the data I’m testing and now it’s working even stranger.

I spend at least an hour examining about 120 lines of code and suddenly see the problem.

I was using a 1 (digit 1) and not an i (letter i) in an array() reference.   Yeah, yeah, looks obvious in this font.  But not quite so obvious in Courier.  Of course that 1 had been wrong all along.  But in the limited testing I was doing before it wasn’t so obvious that there was a problem.  

Now I may, very likely will continue, to use the letters j and k in very short 3 or 8 line loops.  But never again will I use the letter i.

Published Tue, Sep 14 2010 23:01 by Tony
Filed under: ,

Comments

# re: Never, ever again will I use the letter i as a counter in a loop

As I was reading through your article it was turning in mind that to suggest to use J,K etc. for loops as these are my favorites with L,H etc. but you said it at the end. Mostly I use uppercase letters for loops.

You can also change the default settings of VBA Editing Font to Courier New or something similar that helps to distinguish i, l (one) & 1 (L)  clearly.

Regards,

Saturday, September 18, 2010 1:25 AM by a.p.r. pillai

Leave a Comment

(required) 
(required) 
(optional)
(required) 
If you can't read this number refresh your screen
Enter the numbers above: