4 ms·
Thank you. I'd read CC ... 15+ years ago, but couldn't remember this specific step (but I know I've internalized it). I had criticism from folks a couple year
by mgkimsal 4y ago
Thank you. I'd read CC ... 15+ years ago, but couldn't remember this specific step (but I know I've internalized it). I had criticism from folks a couple years back because I used "x" and "i" as variable names in loop counting - for (x=0; x<limit; x++) - that sort of thing. I had folks block my PR because "don't use short variables - that's unprofessional". These were loops inside a test file, and, at that time (8 months after launch), I was committing the first test to the project. It was blocked while I had to think of something better, like for(loop=0; loop<limit; loop++) - which was deemed "acceptable".
This concept from CC was at the back of my mind, but couldn't articulate it, or even remember where I'd first read about it, but it's been something I've worked at adopting and growing over the years. 6 line loops - x/i/j/k/etc are all valid and usable. In some ways, it's actually more helpful to me, because the longer names indicate something concrete/real, and the single letter names indicate local/throwaway values.
- samsquire 4y agoFor 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]`.
- jacquesm 4y agoSounds like your colleagues are cargo-culting rather than arguing from experience or knowledge. My standard solution for this is to name the loop counter for the things being counted. So your example would be: for (horse=0;horse<n_horses;horse++) { } or for (horse in horses) { } or equivalent depending on whatever language you use.