Variable names
31.07.2008 16:52
Russell Coker gives some good reasons why you want to avoid using single character names for variables in your code.
I also occasionally find myself searching for uses of a variable i, only to find that half of the source code matches. A simple regular expression like \Wi\W usually works fine for me. But that already requires three times as many key presses as a fast / i search in vi.
The policy I try to stick to is to use single letter names only for loops that have an obvious purpose and are no longer than say ten lines or so. This usually removes the need to search for such variable names. Any variable that lives longer than that should have a descriptive name anyway. I'm pretty much against using index or similar generalized, verbose names for a simple loop counter. They don't tell you a lot more about the meaning than a single letter name and they make code harder to read. If it's a long loop or it's not obvious what it counts, then you should give it a proper name like pixel_cnt or something similar.
Russell also mentions that the most common loop counter names start with i because of the way variable types worked in FORTRAN. Interesting, because I usually start with n. I got used to that when I learned programming on Sinclair Spectrum. There the next BASIC keyword was entered by pressing the letter n. So n was most convenient as a for loop variable, because you could close the loop simply by hitting n twice. In nested loops letters near n had the priority, starting with m, b and so on.
