Live data from Hacker News

Ask HN: I prefer single letter variables

news.ycombinator.com

21–30 of 52 posts

Re: Ask HN: I prefer single letter variables

#21
post #2

Do you type your variable names using vim or emacs? “When should you use single letter var names” feels like one of those style/preference calls that doesn’t ever have solid rules, other than “if you’re working on a collaborative project, pick a rule and enforce it, even if it’s not perfect”

I use vim exclusively but my names are still descriptive. To give an example from a file I have open right now:

    list($maxTrophies, $positions, $winStreak, $lossStreak, $lastfetch) = $result->fetch_row();
(This is my own hobby code on a solo project; not for an employer where I would be more careful to make it clear for others.)

Some are context-specific, like $positions being an array of how often you finished in which position (e.g.: [1,2,3]=[1 finish in 1st position, 2 finishes in 2nd position, 3 finishes as DNF]). Not all names will make sense if you're not familiar with the context, but given how often it's used, I think that's better than having to use onlineRaceFinishPositionArray. Maybe I could have used finishPositions, though. Not sure, it wasn't a choice I thought about very long, but I definitely am not using "p" as a "name".

For long names there is Ctrl+n, for example maxtr will autocomplete to maxTrophies. Or for another example, "goldTime_ms": there isn't anything else starting with 'gold' than the gold medal time, and the underscore is annoying to type so this is nice to autocomplete.

Re: Ask HN: I prefer single letter variables

#22
You're not alone. 3 to 5 letters works best for me.

Obligatory reference, given the aesthetic: https://archive.vn/mIwG0

My rule of thumb is to avoid variable names that are only used once. Instead, use something like pipeline operators, flow(), etc.

Another preference I have is to use initialisms. Might feel dumb at first, but eventually you realize (or I did, at least) that it matters more how things are "braided together" than having perfect names, and your editor prolly highlights all the uses of the variable where you have your cursor.

But, as always, prefer consistency with the rest of the team, even if you feel the rule is dumb. Don't proselytize. If the style really gets in your way to understanding the code, rewrite it and throw it away. There's something elucidating (and satisfying) in seeing a non-obvious piece of code in your native style.

Re: Ask HN: I prefer single letter variables

#23
post #22

You're not alone. 3 to 5 letters works best for me. Obligatory reference, given the aesthetic: https://archive.vn/mIwG0 My rule of thumb is to avoid variable names that are only used once. Instead, use something like pipeline operators, flow(), etc. Another preference I have is to use initialisms. Might feel dumb at first, but eventually you realize (or I did, at least) that it matters more how things are "braided to…

OP: "I prefer single letter variables"

Reply: "You're not alone. 3 to 5 letters works best for me."

I mostly agree with your comment, also the "prefer consistency with the rest of the team" part, but you are not proposing the same as what OP described.

Re: Ask HN: I prefer single letter variables

#24
I generally agree with you, but it depends on how obvious the semantics of the code are.

In your example, noNameDateFiltered is most likely superfluous. If the filter function is short it's self-descriptive what it does. Adding long variables names makes the code harder to read and adds cognitive overhead. In this case, I would strongly prefer just re-using arr or a.

On the other hand, I have seen long complex function that use single-letter variable names and end up with 20 variables that mean different things. Keeping track of what's what in such a case becomes difficult, and you want something more descriptive.

Taking a step back, the goal of variable names is to make the code easily comprehensible by someone else or you at a later time. You have a few choices to do this, in order of preference:

- The code is self-explanatory, like a simpler filter function - use a short simple variable names. This is always the preferred method. Why make it harder than it needs to be? Reading over-the-top verbose code is just as bad as the opposite.

- The code expression may cause confusion - use a more descriptive variable name, but don't go overboard

- It's difficult to describe the result succinctly even with a variable name - use comments

Re: Ask HN: I prefer single letter variables

#25
This is a ridiculous thing to have to discuss. Probably it is a product of software development's version of the "Homeowner's Association", Code Review, where petty rule enforcement is allowed to overrule good sense.

The essence of a name is twofold: it identifies a thing, and distinguishes it from every other thing. Anything that achieves these two ends is a good name.

Where there is only one thing, its name doesn't matter: only language syntax demands it have a name at all. Shortest is best. In some languages, "_" is that name.

Where there are only two, you need two names visibly distinct. The easiest and cleanest way is two one-letter names.

Using longer names lets you provide documentation without adding a comment which could later become incorrect. That is the only value in a longer name. A longer name has a cost that has nothing to do with how long it takes to type: the reader has to read it to determine it is this thing and not that. It squanders readers' attention. It is worst if it is similar to another name.

If you want to use talking about names in code review to make code better, pay more attention to names that are too similar than to short names.

Re: Ask HN: I prefer single letter variables

#28
post #23
post #22

You're not alone. 3 to 5 letters works best for me. Obligatory reference, given the aesthetic: https://archive.vn/mIwG0 My rule of thumb is to avoid variable names that are only used once. Instead, use something like pipeline operators, flow(), etc. Another preference I have is to use initialisms. Might feel dumb at first, but eventually you realize (or I did, at least) that it matters more how things are "braided to…

OP: "I prefer single letter variables" Reply: "You're not alone. 3 to 5 letters works best for me." I mostly agree with your comment, also the "prefer consistency with the rest of the team" part, but you are not proposing the same as what OP described.

Apparently they're not a fan of consistency, either :)

Re: Ask HN: I prefer single letter variables

#29

I prefer variables of the same length so that code is symmetrical. I'll sometimes use synonyms to achieve this.

On a similar note, I like variable names that are in the same style, e.g. fooBar or bazBat. That means if I ever see a variable named simply foo or bar, there's likely to be a problem.

Re: Ask HN: I prefer single letter variables

#30
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.

All these are all math terms and standards which are defined outside of code.

I think there's something to that - if it's a common enough convention they use it. But if not, don't make it up.

Post reply on HN