3 ms·
You forgot 'i'. Yes, I know you're iterating over something. Best case scenario: you named that something well so all I have to do in your great big for loop
by nailer 17y ago
You forgot 'i'.
Yes, I know you're iterating over something.
Best case scenario: you named that something well so all I have to do in your great big for loop is scroll up, work out what you meant, and apply that to the code I care about, being mildly annoyed.
Worst case scenario: the something is also badly named and I have to open up a debugger and set a trace to work out whether the iterable is people, channels, cows, or dinosaurs.
Yes I know this is common behaviour. So are many other bad things.
- panic 17y agoIf you can't figure out what the "i" is for, the code has other problems.
- nailer 17y agoI didn't say it can't be figured out at all - please don't put works into my mouth. Code is read much more than it's written and should be created for maximum comprehension. A simple one-line method call should be understandable without having to read any surrounding line.
- crpatino 17y agoRespectfully disagree >Best case scenario: ... all I have to do in your great big for loop is scroll up... Once you have to scroll up, you are toast. I mean within a function, not within a loop. There is a rule of thumb of how many things can the mind process at the same time: It is 7 plus-minus 2. You can treat the loop as one element in the function context and analyze the loop context at a different stack frame inside your head. While working in a loop, you have to think about the list, the index and the element, so you are down to 4 more things you can do before "running out of memory". I think that's why the "for each" loop has been successful across many languages. I also think that that's why "i" is preferred over "index"; it is a known idiom that helps you think about the "i-th element" and saves one slot in your brain. >Worst case scenario: the something is also badly named and I have to open up a debugger and set a trace to work out whether the iterable is people, channels, cows, or dinosaurs. Eventually, anyone who has been around for a while inherits some abomination like that. It is not funny. However, no amount of coding standards prevent morons for doing their thing. I would not sacrifice an useful and well known artifact for the sake of idiot-proofing.