4 ms·
For Java and Python for each loops I often use "current" or a singular name for the thing being iterated. If it's a nested loop then I use "other". For C and J
by samsquire 4y ago
For Java and Python for each loops I often use "current" or a singular name for the thing being iterated. If it's a nested loop then I use "other".
For C and JavaScript I use single letter indexes ijkl or "current".
I think it's a personal preference. I don't think there is a wrong way or a right way. Your team needs to agree though. I would be annoyed if I implemented a deep feature and the PR was blocked due to something as inane and useless as this.
- DanHulton 4y agoI don't mind "i", but if there's an inner loop with "j" as well, you are BEGGING for confusion.
- PeterisP 4y agoOften "i" and "j" would appear when the code is the implementation of some math formula which would idiomatically use "i" and "j" when written 'properly' on paper or LaTeX.
- samsquire 4y agoI got down to "k" too. What about "x" and "y"? One important consideration of nested loops is row major access pattern rather than column major pattern, so access array[i][j] rather than array[j][i] due to the data locality of each. You shall get a performance boost by accessing them in that order. I worked on parallelizing nested loops across OS threads and load balancing between outer loops for different progressions through the loop. I also wrote some code to reverse the loop direction to avoid a cache miss when the indexes go back to 0. https://github.com/samsquire/multiversion-concurrency-control#concurrent-loop-concurrentloopjava-and-tickpy https://github.com/samsquire/multiversion-concurrency-contro...
- DanHulton 4y ago`x` and `y` are also problematic, though slightly less so. The biggest issue I have is that they're similar in appearance. `abc` is much better, because they're clearly different from each other, if you're married to single-letter values. `i` and `j` are usually indicative an index for something, and indicating what you're indexing makes the code clearer to read and easier to maintain. `idxUsers`, `idxPosts`, for example. `x` and `y` are a bit trickier because they're very frequently the canonical positional co-ordinates, and renaming them can actually make it _harder_ to read, so (like all things in naming) is a real judgment call, but frequently can be replaced with `col` and `row`, which, to me reads _much_ clearer, especially when using row-major access pattern: `array[row][col]`.