Ask HN: I prefer single letter variables
11–20 of 52 posts
Re: Ask HN: I prefer single letter variables
#12conversely, what is the case for single letter names, even in the situations you bring up? Why would someone chose to use single letter names?
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
#13Re: Ask HN: I prefer single letter variables
#14Re: Ask HN: I prefer single letter variables
#15Even 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.
Re: Ask HN: I prefer single letter variables
#16Re: Ask HN: I prefer single letter variables
#17Re: Ask HN: I prefer single letter variables
#18Re: Ask HN: I prefer single letter variables
#19That's worked well for me on a qualitative level