PDA

View Full Version : Index Variables



MethodMan
09-13-2002, 10:48 PM
Whenever I write for loops, I always do the first one starting with i as the index variable.(and go on down the alphabet from there) In books the indecies always appear as i, j and k, does anyone know how this came to be, and do also follow this rule?

face_master
09-13-2002, 10:51 PM
A book of mine says that the usage of i as the index variable dates back to Fortran or something like that.

Shiro
09-14-2002, 02:24 AM
A book of mine says it's from math, where they use i a lot as index. Thinking of this, I remember that i, j and k were used a lot as indices. And p, q and r are used a lot as variables, at least in my math and computer science books. And don't forget about x, y and z in geometry.

DavidP
09-14-2002, 06:07 AM
Although I do use i, j, and k as index variables, I like to use x, y, and z first.

This probably springs from the fact that I work with graphic modes a lot, and so I can directly relate the indexes to (x,y) positions on the screen.

RoD
09-14-2002, 06:11 AM
call me weird but i've always used integers with actual names that are named after what they are doing, it makes it easier for others to read and understand what my program is doing, but hell ignore me i'm also a huge commenter.

DavidP
09-14-2002, 06:19 AM
>i'm also a huge commenter.

That is good. Lots of times even good coders who do comment there code dont actually comment it enough. People dont realize the importance of commenting code very liberally.

I have a friend who is currently looking for work in the game industry, and he has a sample game which has developed. In his game he used comments very liberally, there was comments on nearly every line of code.

When he showed his code to game companies who he was wanting to sign on with, they saw his use of comments liberally and really gave him a "thumbs up" for doing that. They said that comments in code is one of the main things they look for when somebody submits sample code to them.

RoD
09-14-2002, 06:21 AM
Well good then i'm on the right track. My CS2 teacher insists we don't comment to increase time effecency(sp), god she IS dumb.

Shiro
09-14-2002, 06:35 AM
>My CS2 teacher insists we don't comment to increase time effecency(sp), god she IS dumb.

Sure she didn't mean: don't comment too much?

Maybe a tip: Commenting is important, but it should be useful commenting. I sometimes see students doing a project at our company who do a lot of unnecessary commenting. Things like "increase variable A", "compare values", etc. Commenting should not describe code, but explain code. Why increasing variable A? Why comparing values?

Readers of your code usually understand the programming language so the understand what you are doing, but they would like to know, at certain places in the code, why you are doing specific things.

Anyway, if you're used to commenting, that's great, but try to improve your way of commenting. When I was a student, I often let a friend of mine, who was proffessional programmer, review the code. Very useful, someone who teaches programming often looks at other aspects of the code than someone for whom programming is his/her job.

RoD
09-14-2002, 06:42 AM
thats why i like this site, i can post code and have the comments and style reviewed. I perfer comments like

//Increasing variable a to continue loop

rather then like u said

//increasing variable a

Shiro
09-14-2002, 07:02 AM
> //Increasing variable a to continue loop

That is a much better comment. But a programmer could probably see from your code that you're increasing variable a. More information would give a comment like:

// End not reached, continue loop.

This, explains why to continue the loop. I also find it hard to do useful commenting. Normally I don't comment lines, but blocks of functionality. Only when in a line something special is done or more comment is required, then I would put it there. But these are things for which I ask my more experienced collegues to review.

ygfperson
09-14-2002, 08:33 AM
Originally posted by face_master
A book of mine says that the usage of i as the index variable dates back to Fortran or something like that.

i is for iterator

i use i,j,k,etc... all the time in simple loops. sometimes i use 'count' if it starts getting complicated.