Live data from Hacker News

Ask HN: I prefer single letter variables

news.ycombinator.com

41–50 of 52 posts

Re: Ask HN: I prefer single letter variables

#41

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…

At first I thought you were arguing against the point I made, but after reading this fully I totally agree. The worst offenders that are hell bent on naming variables as sentences are java developers. For some reason the boiler plate required by Java is no long enough for them. Lol. Joking aside, I think anywhere where the code is an API for external functions or anything like that. Having a very descriptive name and consistent convention is good, also if the variable is being carried over in a nested code or is being passed around or is being used 50 lines down somewhere then it is useful to name it in a way that is recognizable

Ultimately, I think even the choice of naming something and using a single letter communicates your intentions. If you make something a single letter then you're telling the reader that do not even worry about this variable it is just passing through, but if you need something your communicating your intentions of using this variable in the future it is more permanent in a sense

Re: Ask HN: I prefer single letter variables

#42
I agree with you and let me make another argument for it.

Consider the fact that a sensible longer variable name can not be THAT MUCH longer.

This creates a problem where you have to manufacture a 10-20-30 character name that somewhat describes what the variable is.

Inevitably you end up creating a somewhat misleading name that badly describes what that variable is supposed to be. Probably leaving something out to avoid the name getting too long. You can't write an essay as the variable name.

If you accept the short single letter variable name instead you avoid this losing battle to manufacture a name for an abstract thing.

Instead you will have the ability to maybe describe what "a" is in a more elaborate code comment.

Re: Ask HN: I prefer single letter variables

#43

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…

At first I thought you were arguing against the point I made, but after reading this fully I totally agree. The worst offenders that are hell bent on naming variables as sentences are java developers. For some reason the boiler plate required by Java is no long enough for them. Lol. Joking aside, I think anywhere where the code is an API for external functions or anything like that. Having a very descriptive name and…

Right! Anywhere you are offered a choice about what to write, you can use that choice to communicate something. Best is when the message comes through without need to read text.

Whitespace is such a choice. Used judiciously, whitespace and its absence can call attention to organizational choices. Used injudiciously, it loses all useful meaning. People who splatter whitespace everywhere -- a blank line above and below every line of code, every parenthesis with a space before and after -- communicate only their contempt for the reader.

Re: Ask HN: I prefer single letter variables

#44
post #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 =…

At the very least make "n" "u", so that it's easier to remember it's a user!

Re: Ask HN: I prefer single letter variables

#45
post #28
post #23

Earlier quoted context omitted.

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 :)

No, it's more than I'm just dumb.

Re: Ask HN: I prefer single letter variables

#46
I agree. For public interfaces/types or global variables descriptive names are important.

But for temp vars in short functions or closures, I also use single letter vars because I want to see what is being done to the variables rather than the description. If a description is needed I'll use a comment.

Another pattern I find myself using is trying not to use `else`, instead using a function return (either returning early with an if, or a default return at the bottom of the function).

Re: Ask HN: I prefer single letter variables

#47
post #42

I agree with you and let me make another argument for it. Consider the fact that a sensible longer variable name can not be THAT MUCH longer. This creates a problem where you have to manufacture a 10-20-30 character name that somewhat describes what the variable is. Inevitably you end up creating a somewhat misleading name that badly describes what that variable is supposed to be. Probably leaving something out to av…

yes this as well... wrong info is worse than no info. Short var almost is like writing _list "_" indicates it's an internal property ignore it

Re: Ask HN: I prefer single letter variables

#48

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…

> ... pay more attention to names that are too similar than to short names.

Ah this is exactly why people dislike the short names.

If you ever have more than a few of them and they aren't even real words, common abbreviations, or hijack well known unreserved names I would immediately reject that during code review.

I think most people are fine with plain English 1 to 3 word names concatenated in whatever style of the language or other conventions.

i.e. "result" instead of "r", "re", or "res"

Why? Because "r" is familiar as a read permission, "re" is familiar as a regular expression, and "res" is familiar as a response object.

This is the bad kind of familiarity that drives people up a wall and wastes a ton of time.

Re: Ask HN: I prefer single letter variables

#49
post #34
post #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 =…

Using "n" here instead of "user" just screams lazy... I wouldn't flag it either, and it will probably never be a problem is this case, but I would momentarily think of the dev that wrote "n" as lazy and worry a tiny bit more about what other code they are writing lazily as well. Don't be that person.

I completely agree.

One letter names are a code smell I associate with the nastiest bugs I've experienced especially when the git blame shows multiple authors on it.

Re: Ask HN: I prefer single letter variables

#50
post #33
post #6

I am with you on this, though I am in a minority at work. An underappreciated benefit is that you are signaling to the reader, when they first see the variable, that it will only be used briefly. That helps them ration their attention. The strongest counter argument is that developers cannot be trusted to avoid building a house of cards if you let them use, even a single shortcut. And there is plenty of evidence, inc…

> though I am in a minority at work This should tell you something. Taking the time to name it nicely now costs almost nothing vs wasting your time trying to prove it can be refactored later and then doing that refactor later. Why? What are you getting out this? Taking 1 second to name it nicely vs the time it costs for everyone to listen to you "make a case for brevity", just please don't be that person. No one like…

The main argument I see for short variable names is that it's easier to read, and interpret (yes, ironically the exact same case the other side makes). In certain contexts (for small functions, math) code is a lot easier to read when using simple, or single variable names. Every now and then I see someone writing out math formulas with long names, and it's so difficult to grok the equation. Just use single variable names so we can grasp the whole thing in a single glance!
Post reply on HN