chaboud
a year 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.
gus_massa
a year ago
Why 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.
jmillikin
a year ago
I 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.
arp242
a year ago
You 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.
throw4950sh06
a year ago
You need better tooling. My editor can highlight all references using the type checker.
evoke4908
a year ago
> Someone shouldn’t have to use source inspection assistive coding tools to parse the code you write
ruszki
a year ago
This is nonsense. You can parse every code with simple Notepad, if you have time. You don’t need to use even search, if you wish.
Or you can make your life easier, and use tools which helps you.
Once again, regardless of codebase.
achierius
a year ago
"If you have time" is the operative point here. The idea is to make it efficient to quickly search for occurrences.
So then you get back to the LSP question -- but for larger codebases I can tell you, those don't always work as well as you'd hope. I don't use 'ii' but it's admittedly much easier to just `rg` for variables than hope my editor plugin decides to work today.
strken
a year ago
In general I agree with you, but in the specific case of loop variables, how much code do you need to search through? How big is your loop if you need to reach for rg to understand it?
chaboud
a year ago
“You need better tooling” is inconsiderate of others. Sometimes you’re in a browser reading over a CR, or you’re sitting at someone else’s machine, or debugging an emergency via a smartphone.
Whatever the case, is it really that hard to use a two-letter loop counter? Try it sometime. When building at scale, it just makes sense to be considerate of diversity and do the little things to that make life easier for others.
throw4950sh06
a year ago
When building at scale, it's not possible to rely on stuff like "everybody should name the iterators differently than it's normally done".
It's not inconsiderate, we are doing a job. If you can't do your job, you don't have place in my team. And properly using an IDE is definitely part of the job. What are you using that can't open web VSCode that has all these features?
chaboud
a year ago
There's a big difference between "rely on" and "be considerate of". I'll take from the recently-created throw-away account that you know this already, and, based on this interaction, I don't think I mind not having a place in your team.
throw4950sh06
a year ago
As a manager of a large engineering organization, I'm unfortunately forced to not be considerate in order to make the org scale to dozens/hundreds of people. Proper usage of tools is crucial and I spend considerable resources to teach the engineers in my org about the tools we use. We took a great deal of effort to make the dev experience full featured, accessible and universal.
You click a button and get a web browser based IDE where you have everything ready, connected to a cloud computer with a full dev env that mirrors our prod env; that from any branch and the button is right there in pull request UI. There is no reason why someone in this org should require everyone to write double letter iterator var names just because they couldn't be bothered to play the video where I taught how to use the advanced code search and the feature could be available to them in less than a minute of fooling around if they tried.
On this scale, following standards is absolutely necessary. If my engineer has to stop and think "but wait, in this company they do it differently" - this times 150 people I currently manage - then I failed at my job.
Some devs hated me when I came into this (much smaller back then) company few years ago and the first step I did was to put a global linter and formatter there. They were angry about how they would format it better than the formatter. Well that's nice - but that doesn't scale to hundreds of people. People make mistakes, stop caring, have different opinions... And all these questions in their minds are again my failure at my job. It's my job to answer these once and forever and to make it easy to follow the rule.
While this is a personal anecdote, I was a large corporate principal engineer for many years and I am mirroring what I saw working there. I can't say this approach to making software engineering work at scale is uncommon.
Please note that here I'm talking about working at a job, usually for very good money. Nothing of this applies to personal projects, most open source projects and so on. In these cases I'm very much on your side.
greyfade
a year ago
I would suggest that if you need to search every instance of the variable `i`, your loop is too large and needs to be refactored. Variables like this should only be used in a highly restricted scope that fits on your screen in 36-point font. If the scope is any larger, you need to search, and at that point, you have bugs.
jraph
a year ago
Many 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.
andoando
a year ago
just search for "int i", "let i", etc?
pavel_lishin
a year ago
That finds the start of the loop, but not occurrences of that index inside the loop.
lemonwaterlime
a year ago
If 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.
pxx
a year ago
I've seen this specific example used to speed up find-next, but it's not like the false positives are frequent enough to bother.
card_zero
a year ago
I use i for the outer loop and ii for the inner loop, like they're Roman numerals.
liampulles
a year ago
If 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
a year ago
I'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
a year ago
But 'i' is not a car, it is the index of a car.
jbaber
a year ago
People 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.
leni536
a year ago
Optimizing 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
a year ago
That’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.
cryptonector
a year ago
Clearly 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.