TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Ask HN: I prefer single letter variables

17 pointsby techsin101over 2 years ago
So it&#x27;s almost universally accepted variable names should always be descriptive, consistent, etc... but I&#x27;m sure there are scenarios where single variable names are preferable. I can&#x27;t quite formulate solid parameters of when that is, but here is a try:<p>- There are few variables in the whole code block &lt; 5.<p>- Amount of lines is &lt; 25.<p>- Code is very functional in nature. ie. no a.getObj()<p>Sometimes I appreciate good variable names and sometimes I hate descriptive variable names. Short var names might be better where extra effort required to parse long names is not worth in terms of utility gained.<p>One example: variables are not meant to be used for long term just exist for next two lines....<p>i.e. filteredData = arr.filter(...) , noDateFilteredData = filteredData.filter(...), noNameDateFiltered = noDateFilteredData.filter(..)....<p>this would be easier to read when it&#x27;s written like this<p>a = arr.filter(...), b = a.filter(...), filteredData = b.filter(...)<p>another situation...<p>data = arr.filter(n =&gt; n.flag === true);<p>&#x2F;&#x2F; it&#x27;s completely obvious what&#x27;s happening here, n is iterable. calling it obj|arr|users|etc... doesn&#x27;t add much value for this piece of code as is. you probably already know we are getting users. It&#x27;s like not using pronouns &quot;he&quot;&#x2F;&quot;she&quot; and just keep using first name: Mike wanted to come in because Mike had said that Mike needed to talk to Aniqa, Aniqa knows about the insurance stuff. Aniqa has worked in insurance....<p>There is inverse correlation between length of a variable name and the speed of understanding logic of the code. But smaller the name the more you need to remember what it is, and the longer it&#x27;s carried over the more difficult it&#x27;s to continue remembering it. So in situations where it doesn&#x27;t need to be carried for a long time I prefer smaller names.<p>I&#x27;m sure of it from my experiences. I prefer short names generally when is less data massaging and more algorithmic in nature, because sometimes it&#x27;s hard to name a var in a way that captures vague concept in algo, without making it downright misleading. i.e. i call something a sliding_window but then later don&#x27;t use it as one. Reader would be like um... wait what??<p>TLDR: I guess correct name matters more and more, the farther you are from its declaration.

23 comments

orzigover 2 years ago
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.<p>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, including my own experience, to back that up. But if you can prove you will re-factor when the time is right, it becomes a lot easier to make your case for brevity.
评论 #34144785 未加载
评论 #34143916 未加载
lucb1eover 2 years ago
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&#x27;t quite sound consistent. The title and submission text also contain no concrete question, so I&#x27;m not really sure what you&#x27;re looking for here.<p>Even in your example cases, though, compare these two lines:<p><pre><code> a = arr.filter(n =&gt; n.flag === true); a = arr.filter(user =&gt; user.flag === true); </code></pre> Which one is more clear here?<p>I hate ${JavaStyleVariableName}s that are so long that it takes extra mental effort to parse the names, so I&#x27;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 &#x27;n&#x27; sounds like a number, rather than an object that would have a .flag property. While not very confusing, it&#x27;s also not the most intuitive thing to call it. I wouldn&#x27;t flag this as &quot;please fix&quot; in a code review, but it&#x27;s also not better than using any name at all.
评论 #34144831 未加载
评论 #34147060 未加载
评论 #34145375 未加载
moloch-haiover 2 years ago
This is a ridiculous thing to have to discuss. Probably it is a product of software development&#x27;s version of the &quot;Homeowner&#x27;s Association&quot;, Code Review, where petty rule enforcement is allowed to overrule good sense.<p>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.<p>Where there is only one thing, its name doesn&#x27;t matter: only language syntax demands it have a name at all. Shortest is best. In some languages, &quot;_&quot; is that name.<p>Where there are only two, you need two names visibly distinct. The easiest and cleanest way is two one-letter names.<p>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&#x27; attention. It is worst if it is similar to another name.<p>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.
评论 #34145429 未加载
评论 #34166658 未加载
mudrockbestgirlover 2 years ago
I generally agree with you, but it depends on how obvious the semantics of the code are.<p>In your example, <i>noNameDateFiltered</i> is most likely superfluous. If the filter function is short it&#x27;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 <i>arr</i> or <i>a</i>.<p>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&#x27;s what in such a case becomes difficult, and you want something more descriptive.<p>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:<p>- 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.<p>- The code expression may cause confusion - use a more descriptive variable name, but don&#x27;t go overboard<p>- It&#x27;s difficult to describe the result succinctly even with a variable name - use comments
评论 #34144598 未加载
pjdkochover 2 years ago
You&#x27;re not alone. 3 to 5 letters works best for me.<p>Obligatory reference, given the aesthetic: <a href="https:&#x2F;&#x2F;archive.vn&#x2F;mIwG0" rel="nofollow">https:&#x2F;&#x2F;archive.vn&#x2F;mIwG0</a><p>My rule of thumb is to avoid variable names that are only used once. Instead, use something like pipeline operators, flow(), etc.<p>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 &quot;braided together&quot; than having perfect names, and your editor prolly highlights all the uses of the variable where you have your cursor.<p>But, as always, prefer consistency with the rest of the team, even if you feel the rule is dumb. Don&#x27;t proselytize. If the style really gets in your way to understanding the code, rewrite it and throw it away. There&#x27;s something elucidating (and satisfying) in seeing a non-obvious piece of code in your native style.
评论 #34144274 未加载
评论 #34144888 未加载
readthenotes1over 2 years ago
Clean Code advocates that the length of a variable name should be proportional to it&#x27;s scope.<p>That&#x27;s worked well for me on a qualitative level
akerl_over 2 years ago
Do you type your variable names using vim or emacs?<p>“When should you use single letter var names” feels like one of those style&#x2F;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”
评论 #34144228 未加载
borplkover 2 years ago
I agree with you and let me make another argument for it.<p>Consider the fact that a sensible longer variable name can not be THAT MUCH longer.<p>This creates a problem where you have to manufacture a 10-20-30 character name that somewhat describes what the variable is.<p>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&#x27;t write an essay as the variable name.<p>If you accept the short single letter variable name instead you avoid this losing battle to manufacture a name for an abstract thing.<p>Instead you will have the ability to maybe describe what &quot;a&quot; is in a more elaborate code comment.
评论 #34155127 未加载
saeranvover 2 years ago
Yeap, me too. Especially when dealing with math&#x2F;physics. Full named variables for math and physics is incredibly difficult to grok, once you&#x27;re used to reading it in traditional math form.
pestatijeover 2 years ago
filteredData, noDateFilteredData, etc. gives me more info than a, b, etc.<p>Let&#x27;s say you have 2 lines of code where the variable a is used: a = expr, b = a.filter(...)<p>Now I have to do the mental effort to see what expr is doing, instead of going directly to the second line and see filteredDateData = filteredData.filter(...)
评论 #34143891 未加载
cirrus3over 2 years ago
If nothing else, do it for the increased search scope... you can never predict when someone later is going to be desperately searching any reference to something and when digging into an issue with &quot;foo&quot; then the var name of &quot;fooOutputDirectory&quot; is going to save time vs if you named it &quot;out&quot; or &quot;o&quot;.<p>There is no excuse about &quot;well maybe the controller&#x2F;service&#x2F;repository&#x2F;whatever should have been named foo* in that case, I was just holding this temp var for 2 lines of code in helper function and it shouldn&#x27;t matter&quot;.... just don&#x27;t. Don&#x27;t be that person. No one thinks you are smarter or cooler for that.<p>Also consider that if you are really using tiny-scoped vars so often, maybe you are doing something else wrong.<p>If you have &quot;data = arr.filter(n =&gt; n.flag === true);&quot; and it is so tightly scoped, why do you even need &quot;data&quot;? Maybe just return it... if you need to do some other operation on &quot;data&quot;, maybe chain the call? If you need to do literally anything else (even just logging it), name it it something useful.<p>&quot;n&quot; is fine here. &quot;arr&quot; is not... wtf is contained in &quot;arr&quot;, what are these types that have &quot;flag&quot;?<p>Sounds like you got slammed in a code review by someone who has had to debug this type of stuff more than you and you are looking for validation. Just stop being this way sooner than later.
评论 #34145360 未加载
Gualdrapoover 2 years ago
The only thing I can say is that I remember having quite a hard time when my buddy used all single letter variables and reading his code, back when we were learning C at uni. Not sure if after a while one can get used to it and have some &quot;buffer memory&quot; (sort of speak) in the brain for allocating single letter variables while reading or working with code (and if I ever had such thing for the time being), but I do remember writing in paper which variable was which when I got lost after 50 lines or so while reading - and helped me to &quot;debug&quot; because, a couple of times, things didn&#x27;t work as expected because it seemed it was <i>he</i> who mixed vars when writing the code
igtztorreroover 2 years ago
In GoLang we use single letter variables names in selectors and local variables in small functions
justsomeuserover 2 years ago
I agree. For public interfaces&#x2F;types or global variables descriptive names are important.<p>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&#x27;ll use a comment.<p>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).
D-Coderover 2 years ago
For me, at least, typing is a lot easier than thinking.
sublinearover 2 years ago
I can only think of a few scenarios: math functions, string formatting, iterators, and generic code where there is no meaningful name.
AlphaWeaverover 2 years ago
I&#x27;ve encountered this in Erlang, but when using Elixir, pipes solve this problem quite nicely.
bityardover 2 years ago
Single-letter variable names are fine for very short loops in some cases and that&#x27;s about it. Nearly all other uses unnecessarily obfuscate the code for those who have to maintain it later. (Including Future You.)
heavyset_goover 2 years ago
The only time I feel comfortable using them is in short anonymous functions.
KptMarchewaover 2 years ago
In lambdas, sure, but in longer functions there&#x27;s usually better way.
decrementalover 2 years ago
I prefer variables of the same length so that code is symmetrical. I&#x27;ll sometimes use synonyms to achieve this.
评论 #34144498 未加载
sys_64738over 2 years ago
Single char bars are terrible when you’re trying to maintain code. Searching becomes really hard with cscope.
Tagbertover 2 years ago
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?
评论 #34144068 未加载