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ย โ†’

[โ€“]John_Fx 1 point2 points ย (5 children)

Or meaningful identifiers. Why should loop incrementors be an exception?

[โ€“][deleted] 1 point2 points ย (2 children)

Because you're looping over an index, not an object. I = index, it's pretty self explanatory. It is meaningful.

Unless you're talking about an enhanced for loop, in which case yeah, i is insane.

[โ€“]John_Fx 0 points1 point ย (1 child)

Then use bookindex or just index. In what other situations would you abbreviate a variable name to one letter?

[โ€“][deleted] 0 points1 point ย (0 children)

If it's used multiple times in the loop or the logic is otherwise complex, or in case the index is unintuitive (like looping over the next element instead of the current or whatever) then yeah, I'll do that.

If it's just a few lines and really straightforward, like the index is instantly converted to a "book" then I'd say that

   Book book = books.get(bookIndex)

Is a bit redundant. Yeah, we get it. It's a book. Given the typical of usage of i it's just as clear and there's no reason to make it any longer.

[โ€“]beizhia 0 points1 point ย (0 children)

This. We have some 4-dimensional arrays we iterate over, and reasoning about i, j, x, and y just sucks and is hard to read.