6 ms·
I thought this was going to be a bit of dry humor before I followed the link. It’s actually serious, and fairly informative (albeit a bit scary). The first pla
by chaboud 2y ago
I thought this was going to be a bit of dry humor before I followed the link. It’s actually serious, and fairly informative (albeit a bit scary).
The first place I worked had a soft-but-hard rule (enforced by ridicule) that single-letter variable names not be used. Loop counters like i, j, and k were frowned upon. However, counters like ii, jj, and kk were fine for matrix operations, xx and yy for image loops, etc.
There were a couple of key guidelines to follow:
1. If it can have more meaning than sample, pixel, or matrix position, give it more meaning, but two characters repeated is fine for loop counters (again, with meaning).
2. Whatever you use should be quickly findable with text search tools of the era (the late ‘90’s).
Someone shouldn’t have to use source inspection assistive coding tools to parse the code you write. When someone else (or you, in a few months) comes upon this code and needs to understand it, they should be able to use find to do it.
- cryptonector 2y agoClearly that rule (no single-letter variables) is pretty dumb. Instead you want something like TFA so that you can easily understand and adhere to the local conventions for such variable names.
- leni536 2y agoOptimizing for grep is fair, however searching for whole words is typically possible in most contexts. I don't have a problem for grepping single letter variable names, although I admit that whole word or word boundary search is not as obvious and slightly less convenient than just plain substring search.
- chaboud 2y agoThat’s basically it. Using double-letter loop counters is a very simple courtesy that one can pay to other developers or oneself in the future.
- gus_massa 2y agoWhy is ii better than i??? I may understand that instead of x,y it may be better to use px,py for positions and vx,vy for speed. Perhaps xx,xy for position. But I'd never use yy.
- lemonwaterlime 2y agoIf you’re doing any kind of scientific computing or electronics computations, “i” or “j” mean the imaginary number sqrt(-1). Some software, such as Matlab, have those letters reserved for the common case of imaginary roots of a polynomial, and “ii” and “jj” as variables is a common coding convention. Whether “i” or “j” is your letter for imaginary numbers depends on if you’re an electrical engineer or not.
- card_zero 2y agoI use i for the outer loop and ii for the inner loop, like they're Roman numerals.
- pxx 2y agoI've seen this specific example used to speed up find-next, but it's not like the false positives are frequent enough to bother.
- jmillikin 2y agoI use `ii' and `jj' etc for all my loop counters, because it's easier to search for in any editor. Searching for `i' requires learning the local editor's way of marking word boundaries, otherwise it'll highlight every instance of the letter `i' which is useless. Searching for `ii' has less noise because very few words have that sequence in them, and `jj' is even rarer.
- jraph 2y agoMany editors can search for whole words. Some editors like Kate highlight whole words by just selecting them (and it will not highlight letters in words). Some also do that just by placing the cursor on a word.
- arp242 2y agoYou can use \bi\b in most regexps. Or \<i\> in Vim (pressing "*" will do that for the word under the cursor). This is kind of basic stuff that was around in the 90s, possibly even 80s.
- 2y ago
- liampulles 2y agoIf I'm looking at a for loop, my expectation is that the loop variable will be i. I'm against single letter vars as a general rule but I think this is a fair exception
- bluerooibos 2y agoI'd prefer i to be a description/name of the thing that is being iterated. If your array is named cars then you can safely assume i will be a car, but depending on the complexity of the code it just adds another step for me to parse, when I could have just named it car to begin with. That's a simple case where the data being iterated over is clear. When the data is complex, it becomes even more useful to descriptively name i so that you can more easily see what you're working with.
- eutectic 2y agoBut 'i' is not a car, it is the index of a car.
- jbaber 2y agoPeople mentioning ii instead of i for searchability, then being countered with the sometimes difficulty of searching with word boundaries are missing an important point: A variable with big enough scope you'd be searching for it should have a full real name, no i or ii. Even when it's a loop counter, if it's present for more than a page, please tell me what you're counting.