Live data from Hacker News

Ask HN: I prefer single letter variables

news.ycombinator.com

11–20 of 52 posts

Re: Ask HN: I prefer single letter variables

#12
post #8

conversely, what is the case for single letter names, even in the situations you bring up? Why would someone chose to use single letter names?

There are many cases where a single letter variable name is clearest and easiest to read.

My loop iterators are mostly single letter. A coordinate is likely to be an x, y, or z. A unit vector aligned to an axis will be i, j, or k. A point is likely to be p. A vector data structure v. The first transformation of a variable x is likely to be x’, the second x’’, etc.

Re: Ask HN: I prefer single letter variables

#15
Exceptions exist, sure. The title is that you prefer single letters generally but the submission text is more about exception situations for throwaway variables, so it doesn't quite sound consistent. The title and submission text also contain no concrete question, so I'm not really sure what you're looking for here.

Even in your example cases, though, compare these two lines:

   a = arr.filter(n => n.flag === true);
   a = arr.filter(user => user.flag === true);
Which one is more clear here?

I hate ${JavaStyleVariableName}s that are so long that it takes extra mental effort to parse the names, so I'm fully with you if you argue for appropriate variable name lengths, but having any name at all (one character is typically not a name) can give some indication of what it is, even if abbreviated or otherwise terse. The variable 'n' sounds like a number, rather than an object that would have a .flag property. While not very confusing, it's also not the most intuitive thing to call it. I wouldn't flag this as "please fix" in a code review, but it's also not better than using any name at all.

Post reply on HN