4 ms·
What's an example of an appropriate single-char variable name, in your opinion?
by aasarava 17y ago
What's an example of an appropriate single-char variable name, in your opinion?
- damovisa 17y agopersonally, I find this more than ok: for (int i=0; i<items.length; i++) { //do something small with items[i] }
- cema 17y agoCorrect! However, I should confess that I usually write it as ii: for (int ii = 0; ii < items.length; ii++) ...
- jrockway 17y agoWhy?
- MaMa 17y agoThen it's not a single character variable name anymore
- Silhouette 17y agoIt gives you something you can quickly search for: hit <incremental search keyboard shortcut> followed by "i" twice and you're there. Do the same with a single-character name, and you hit any time that character appears anywhere. I personally find the doubling awkward, and with modern editors I use semantic searching much more than raw text scans so the benefit of doubling is marginal, but there is at least one sensible reason you might choose to do it.
- jrockway 17y agoOr you could just search for "i =" or "int i" or "for"?
- Silhouette 17y agoSure you could, but while you're busy trying to find the exact combination of spacing and punctuation that starts the loop you want, and then manually looking through the loop to find every further reference to the same variable, the guy who called it "ii" and found it with the first four keystrokes is already being productive. As I said before, it's probably not as useful with modern IDEs that have semantic search capabilities, but there is a genuine reason some people prefer the "ii" style.
- jasonkester 17y agoi
- deleted 17y ago[deleted]
- roundsquare 17y agoIf you are working in finance, you might use common single letter variables, for example if you are creating a black-scholes function. E.g. r = interest rate, etc...
- wglb 17y agoBetter to use 'rate' or 'irate'.
- jrockway 17y agox, as in "f x = x + 2". What else are you going to call it? "addend"?
- Xichekolas 17y agof = (+ 2) (I'm not a huge fan of point-free, I'm just being facetious ;)
- anatoly 17y agoi,j, sometimes n. When working with coordinates - x, y, z.