PDA

View Full Version : i,j, n,m, x,y,z ... We dont know the alphabet

lightatdawn
11-19-2002, 03:02 PM
Not working unexpectedly today, so I'm home bored out of my tree, and got to thinking (ya, it happens).

[warning: This will only seem interesting if you are a) bored out of your mind, like me or b) slow in the head (arguably also like me)]

Us programmers have strange habits in terms of naming conventions. We commonly use "i" as a counter. This makes some sense as its short form for integer. "n" is also common (number). So far so good. Yet when a nested loop is found and we need a new counter, we commonly proceed to j (or m after n). Apparently this is simply because j comes after i in the alphabet as has nothing whatsoever to do with our origional reason for chosing a logical variable name.

I tried using inc1 (for increment) and inc2 as its use was more apparent, but I have some kind of personal religious belief against using numbers in my variable names, so that fell through. x y and z are reserved for spacial coordinates, and it would be heresy to use them as simple counters. Doesn't it seem arbitrary to anyone else that we use two separate methods to derive an appropriate name? Why start in the middle of the alphabet and then proceed down the line from there?

The point? Yes, I sort of have one... I think there are lots of "standard" things we do that have no logical basis and yet are taught de facto. We have no idea why we do them, but we do it anyhow. Things like treating structs and classes like totally different creatures. We do it because its always been done that way. We no longer know why.

... Okay so I need to get outside, but its really wet. I'd say its ........ing harder than an alcoholic on cheap beer night, but thats probably too rude for the PG rating, so I wont... wait... doh...

Brian
11-19-2002, 03:45 PM
int ia, ib, ic, id;

?

moi
11-19-2002, 03:59 PM
(i, j) is used frequently in math with vectors.

Sang-drax
11-19-2002, 04:20 PM
Yes, the math language is to blame

a,b,c,k = number constants
k,m,n = any integer (l is too similar to 1)
x,y,z = coordinates in 1,2 and 3 dimensions
u,v,w = vectors
A,B = matrices
i,j = matrix indexing

etc.

btw, questions like yours are quite interessing.

Waldo2k2
11-19-2002, 04:32 PM
lol
throw some hungarian notation in there
lol

[crappy code]
for (UINTi=0;UINTi<=5;UINTi++)
{
std::cout<<"Hungarian notation sucks"<<std::endl;
}
[/crappy code]

lightatdawn
11-19-2002, 06:07 PM
>>btw, questions like yours are quite interessing.

Ah, thank you for making my thread feel less like pointless pontification. :cool:

a,b,c,k = number constants
k,m,n = any integer (l is too similar to 1)
x,y,z = coordinates in 1,2 and 3 dimensions
u,v,w = vectors
A,B = matrices
i,j = matrix indexing

Yes, thats pretty much the way I understand it too. I'm just not sure when this became some sort of unofficial standard. I mean, why a,b,c,k? Why i,j for matrices? Theres got to be reasons buried somewhere for this stuff.

Eibro
11-19-2002, 06:53 PM
I read in a JavaScript book that using 'i' as your count variable is a programming tradition which originally started in (insert old programming language here). Now look what you've done; I have to go get that book back and find out what it said.

-KEN-
11-19-2002, 07:46 PM
with loop variables I usually use x, y, z, i, and j. Anything else and I tend to get confused and write stupid things... for(int boobie=0....:)

Eibro
11-19-2002, 07:47 PM

alpha
11-19-2002, 10:15 PM
haha, i use i and count as my usual counters. don't really know why, just do.

oh, also...
i,j,k are used in unit vector notation for the three dimensions as well.

11-20-2002, 03:39 AM
>>>
I think there are lots of "standard" things we do that have no logical basis and yet are taught de facto. We have no idea why we do them, but we do it anyhow.
<<<

One of the first real high level programming languages was FORTRAN. In Fortran, variable with names beginning with I, J, K... up to O I think, (rarely use that many), were implicitely typed as integers, where variables beginning with the other letters were implicitly real.

Of course you could declare a variable to be different thus...

REAL*4 I

... made a 32 bit real and...

INTEGER*2 X

... made a 16 bit integer.

As many people didn't bother declaring their variables for loop counters, I, then J then... and old habits die hard.

Sang-drax
11-20-2002, 05:46 AM
Originally posted by lightatdawn
I mean, why a,b,c,k?

The word "constant" is spelled "konstant" in many other germanic languages (swedish, german etc.)

In Fortran, variable with names beginning with I, J, K... up to O

There's a good explanation.

I also think it's quite natural to type like that.

int i;
char c;
char ch;
long l;
class Dog;
Dog d;
Dog dog;
//etc.