Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Surely there's nothing wrong with single-character variable names, used appropriately.


What's an example of an appropriate single-char variable name, in your opinion?


i


i,j, sometimes n. When working with coordinates - x, y, z.


If 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...


Better to use 'rate' or 'irate'.


personally, I find this more than ok:

for (int i=0; i<items.length; i++) { //do something small with items[i] }


Correct!

However, I should confess that I usually write it as ii:

  for (int ii = 0; ii < items.length; ii++) ...


Why?


Then it's not a single character variable name anymore


It 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.


Or you could just search for "i =" or "int i" or "for"?


Sure 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.


x, as in "f x = x + 2". What else are you going to call it? "addend"?


f = (+ 2)

(I'm not a huge fan of point-free, I'm just being facetious ;)




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: