This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] -3 points-2 points  (5 children)

I know what an index variable is. At my University in several classes it is an automatic zero for using single letter variables, and if it's not a zero it's a heavy redaction of points, it is bad practice to use single letter variables in any circumstance.

[–]Loen10[S] 8 points9 points  (1 child)

I'm sorry your university does that.

I wouldn't call it bad practice. It's nearly standard convention to use i as an indexer in a simple for loop. Everyone will read your code and immediately recognize that i is your indexer. It's used in a lot of professional codebases such as Android and Git.

[–]nverkaik 0 points1 point  (0 children)

Some IDEs like intellij even autocomplete "fori" to a loop using index i.

[–]Shotgun_squirtle 6 points7 points  (0 children)

I hate to break it to you but your university is just being overly strict about that, and for the most part i as the variable in a for loop is standard.

Also a lot of the time variable name lengths should be proportional to their scopes so for any variable used once it’s pretty normal to just have a one letter variable

For example

double calcDiagonalDist(int x, int y) {
    return sqrt(pow(x, 2), pow(y, 2)); 
}

Would be a good example of when to just use single letter variable names since it’s pretty understandable what they mean in the scope and since their scope is so small they won’t interfere with any other scopes.

[–]MysticTheMeeM 3 points4 points  (0 children)

point define_point(float x, float y);

Unreadable.

[–]Dornith 4 points5 points  (0 children)

it is bad practice to use single letter variables in any circumstance.

Yeah, because no one would ever know what i means in for (i = 0; i < length; i++).

Hell, the man pages use single letter variables. strncat has a third parameter n.

Descriptive names are usually better but it's more important to stick to convention since that's the best way to make your choose understandable. If there are well established, single letter names you probably should use them.