12 ms·
Why I'll never abbreviate a variable as long as I live
- twblalock 10y agoThe problem was not that the variable was abbreviated. The problem was that the abbreviated variable was so similar to another abbreviated variable that was used for a similar purpose.
- reactor 10y agothen there are two problems.
- bryanrasmussen 10y agosure but if the variable was not abbreviated the chance of collision would have been less.
- brandonbloom 10y agoEven with a 200 character variable name, the chance of collision is 100% if it was auto-completed or copy-pasted.
- philosoft 10y agoAt a minimum, readable variables would have simplified identifying the issue after it was introduced.
- twblalock 10y agoMy point was that the abbreviation is not a problem per se. If there was not another variable with a similar name, it would not have mattered at all. This is an issue of names being similar. No matter how long a variable name is, if another variable name in the same namespace is superficially similar, this kind of thing can happen. On the other hand, if there are no other variables with similar names, then you can abbreviate all you want and your chance of collisions will not increase.
- dgcoffman 10y agoYeah, the problem isn't that the airplane is on fire, the problem is that it crashed into another burning airplane as both tumbled uncontrolled through the sky, hurdling toward the earth.
- DSMan195276 10y agoI would add - there's a bit of a difference between an abbreviation that just keeps something from being ungodly verbose (20 letters instead of 50), and an abbreviation that shortens something so much that the original meaning is completely lost ("Ttpfe"). This is especially true for things in context - "time of ten percent height on the falling edge" is needlessly verbose, but in context "ten_percent_falling_edge" would probably be perfectly fine. And indeed, that's really just an expanded version of "Ttpfe", which is what was used anyway. If you shorten everything to five-letter names, it's not really that surprising that it becomes an issue - And I mean, what's the real point of abbreviations when they are so short that it makes it harder to read the code? Edit: It's worth pointing out though - if this is really old C code it may be justifiable. Back in the olden days of C (Older then C89 at least), only the first 8 characters of a symbol actually mattered. "blahblahone" and "blahblahtwo" would resolve to the same symbol. So shortening in this way could be necessary.
- bhollan 10y agoThe line was adding about 5 or 6 terms, and I think it was actually an 8-character acronym that he had. It might have been like "time from ninety percent to ten percent falling edge" or "falling edge fall time ten percent" or something. Either way, it was awesome to find it.
- saghm 10y agoI definitely prefer more verbose names to abbreviated ones, but I'm not sure that never abbreviating a variable name is the right way to go either. Surely there's a middle ground between `Ttpfe` and `timeOfTenPercentHeightOnTheFallingEdge`?
- sbov 10y agoUse context. If you can't, refactor so you can. If you can't, well, you're SOL and stuck with timeOfTenPercentHeightOnTheFallingEdge. Edit: e.g. if all of your variables are "timeOfTenPercentHeight...", cut that part out of the name. Full names can be just as bad as abbreviations - e.g. if they're all "timeOfTenPercentHeight..." then they all start to blend together.
- galori 10y ago> refactor Yes exactly. If you factor out a class that deals exclusively with timeOfTenPercentHeight, then the variable names can be OnTheFallingEdge and OnTheOtherEdge or whatever...
- Roboprog 10y agoExactly. Make a "times" record/object/tuple (whatever the local custom you have for an aggregate type is), perhaps with a "tenth" nested such item (10% = 1/10), with "fall" and "rise" members. times.tenth.fall times.tenth.rise Hopefully, they aren't using some ancient version of FORTRAN. My first paid work in the 80s was in a language with 8 or 10 character long names, depending on use, with only scalar values outside of ISAM tables. One compiler variant extended that with arrays (but not nested arrays). You had no choice but to make some weird names. That said, when I started doing ANSI C with 32 char names, I felt like I had failed when I needed a variable that had to be that long to explain what it did. Either it had too broad a scope, or should have been bundled in something else (record/struct).
- pshc 10y agotl;dr: use newtypes! I like using static types to avoid these sorts of problems. Modern languages like Swift, Rust and Haskell let you make zero-overhead type wrappers around other types. So here they could have defined `newtype RisingEdge(Float)` and `newtype FallingEdge(Float)`, and then use those types in the function parameters as appropriate. Helps shorten function names to boot!
- guelo 10y agoIf the variables had been named timeOfTenPercentHeightOnTheFallingEdge and timeOfTenPercentHeightOnTheRisingEdge it probably would have still been hard to notice that they had been swapped in that one line.
- niccaluim 10y agoYes, but the likelihood of mistyping them would be far less. 'f' and 'r' are right next to each other. 'Falling' and 'Rising' are a bit harder to unnoticeably fat-finger.
- Dylan16807 10y agoIt was probably a mental mistake. It's not particularly easy to fat-finger in that specific way.
- DSMan195276 10y agoI think the catch is that the 'f' and 'r' keys are right next to each-other, so if you accidentally type one instead of the other then you'd get the other variable by mistake. That said, you raise a fair point - we don't know how this bug got there. If it was a simple typo like above then the verbose names would have prevented it. If it was a logic error by the programmer (For whatever reason), then you're right that the name wouldn't matter because they typed the one they intended, it just wasn't the right one to use.
- adrianratnapala 10y agoFat-finger is only one kind of error -- and one far less common than the good-ole brain-spasm which can easily replace "Falling" with "Rising". The ability to read the code an find see the error is more important. And one that score even though ttpfe is a terrilbe name "timeOfTenPercentHeightOnTheFallingEdge", is even worse.
- benkuykendall 10y ago"Never abbreviate a variable" is a very strong statement, surely inspiring religious wars. And maybe there are edge cases: i as a loop counter, id for an integer primary key, whatever. But this example is something else entirely; "ttpfe" is honestly the worst variable name I have every seen.
- dgcoffman 10y agoAh, good ol' TransactionTemplatePayloadFactoryExtension.
- cbd1984 10y agoHistorically, i wasn't even an abbreviation: It was the first variable name which would be assumed to be integer by FORTRAN compilers which implicitly assigned types to variables based on name. The choice was probably further influenced by longstanding mathematical tradition, which uses i and j as indices. (You could declare types and the compiler would respect it, leading to the old truism "GOD is REAL, unless declared INTEGER".) (If you think that's the weirdest thing old FORTRAN did, look up the arithmetic IF statement sometime. Then, look up assigned GOTO.)
- ScottBurson 10y agoI think the weirdest thing old Fortran did was to let you pass a constant by reference to a subroutine, which could therefore change its value.
- foota 10y agoAnyone else feel a bit uncomfortable reading the level of detail in this post?
- mmaunder 10y agoHope those systems are now declassified for his career's sake.
- bhollan 10y agoI would never publish anything that shouldn't be published. Pretty much everything here could be found in a few hours on wikipedia, except for the story aspect of it.
- bhollan 10y agoI tried to keep it as high-level as I could. What would you change?
- foota 10y agohonestly I don't have any experience working with anything classified, so your judgement is likely oodles better than mine. Great story :)
- bhollan 10y agoNothing in this post is even remotely classified.
- mmaunder 10y agoWhen hiring we ask for sample code from developers written specifically for the job application. We place a really high premium on readability. Occasionally we'll get code from devs that is very concise - not just abbreviated variable names, but complex long statements using ternary etc. I'll usually ask them to resubmit code and really focus on readability and it turns out fine. But I think there might be a misconception among devs where the thought is that really compact code shows talent. Our ideal coder is someone who cares deeply about performance and where the code is ridiculously easy to read and trace through. I mention performance, because in some situations making things more verbose can affect that.
- component 10y agoWhenever we interview a candidate, the one thing we give high premium on is structure, the rest come next. bad variable names, long complex code...can easily be adjusted. If the candidate doesn't have a good understanding of how to structure a code (what goes where, for what purpose, separation on concern...), it'll take much more effort to teach that candidate than to teach the bad variable namer. Properly structured code naturally leads to a better performing software, even if/when a bug arises it'll be easier to spot / test. All that being said, if a coder names a variables, 'a', 'aa', 'aaa', 'aaaa' that's a serious red flag.
- Roboprog 10y ago"I ran the code through minify, but it's bigger now?!?"
- component 10y agoI don't understand. Why would one submit a minified code for a review? A friend of mine used to work at a place where one of the coder actually used 'a', 'aa', 'aaa'...when naming variables.
- Roboprog 10y agoI was joking. The example sort of looks like something that would fall out of minify -- although 'a' - 'z' would have to be used/visible in a scope before 'aa' would be generated as a name.
- ams6110 10y agoHeh. When I first learned to code as a kid, a variable was max two characters (the first of which had to be a letter A-Z and the other could be a letter or number).
- bhollan 10y agoThat same engineer would frequently nest for loops inside for loops inside for loops with indeces like ii, jj, and kk. Drove me nuts sometimes.
- stevep98 10y agoHa, you're right. Commodore BASIC, probably? (written by Microsoft, I think.) I do remember at some point having access to a language with less restrictions on variable naming, and thinking, "wow, that's so amazing"
- Roboprog 10y agoOr Apple "integer" BASIC. Probably a few other dialects, as well.
- iblaine 10y agoTL:DR Abbreviated variables are not always intuitive to others. I tend to agree. If you're going to use a pattern or abbreviation then be as non-creative as possible.
- bikamonki 10y agoPwd stands for: ________? Correct, you guessed it! Once an abbreviation or acronym becomes widely used it makes sense to use it as a variable name for as long as you live.
- crikli 10y agoPrint working directory?
- ivanca 10y ago...I though you meant password, I google it and you probably mean "print working directory" right?
- LukeShu 10y agoPrint Working Directory? Present Working Directory? Password? A probably worse trap is "temp". Is it temporary or temperature?
- bhollan 10y agoI think pwd, dir, cd and the like are obviously standard abbreviations, but that's pretty obvious that's not what I'm talking about.
- foota 10y agoThat example was comically poorly chosen.
- hoodoof 10y agoThis comment really deeply gets to the heart of the issue. Programmer X thinks "I'll abbreviate because MAN IT IS SO OBVIOUS what the abbreviation is, what sort of idiot would think anything else?". You have nothing to lose by typing: let password = '' as opposed to let pwd = ''
- Roboprog 10y agoIt depends on how many characters are significant to the compiler/interpreter. This was a real issue in the 80s, as laughable as it sounds now.
- sseagull 10y agoThis is common in the sciences as well, since senior professors also had the 8-character limit (from fortran [0]). And functions are also named this way as well (see lapack/blas). Some people also hate typing out slightly longer variable names and what not. I try to emphasize that a section of code will tend to be read more times that it is written, and therefore readability is more important. It's a frustrating battle sometimes, though. [0] Exacerbating the problem of using the wrong variable is the fact that much existing code uses implicit types...
- hartror 10y agoIs the issue that they were abbreviations or that they were named similarly?
- molteanu 10y agoSimilarly. The term was supposed to be “Ttpfe”, but he had mistakenly called it “Ttpre”
- stevep98 10y agoI was recently implementing a geometry algorithm which I looked up on quora. It was described using typical vector notation, using r,s . t,u. Since I referenced the algorithm in the comments, I decided to use these same variable names in my code. I think this is the right choice, but my code reviewer didn't. But he didn't click on the quora link. Why is okay for mathemeticians to abbreviate things but programmers? Is it because they deal in more abstract entities where the name is irrelevant?
- jordanlev 10y agoProbably has to do with decades (centuries?) of knowledge that is passed on in a somewhat consistent and rigorous way. Programming is a relatively new field, with tons of self-taught practitioners.
- potatolicious 10y ago> "Is it because they deal in more abstract entities where the name is irrelevant?" Partially, but also because math equations don't really have strong maintainability needs. Another mathematician isn't going to walk in 6 months later, get confused, and blow up the universe. Though I'd argue that in fact math equations should be better named. They're rather abstractly named as a matter of convention, but they would be more easily understandable in many cases if variables were more carefully named. Another risk of "porting" geometry algorithms, especially complex ones, directly from their mathematical expressions, is that you don't gain insight into why they work. This makes debugging later difficult, since nobody who wrote the code actually understood what the algorithm was doing. Forcing yourself to rename variables into something sensible will also force you to understand the mechanics of what's happening.
- hugozap 10y ago>they would be more easily understandable in many cases if variables were more carefully named A mathematician already knows the notation, so a a long mathematical expression with descriptive names will not make it more easy to understand (if you know the notation). I would argue that it can even make things worse and add cognitive load.
- cbd1984 10y agoI think the usual rule is that the size of a variable's name should be a function of the size of the scope in which it's visible: If it's a global, it should have a long, descriptive name, perhaps with_underscores or CamelCase or similar. If it's a class member, abbreviate it some. Function locals get even shorter names, and loop indices or temporary variables only used in one part of the function can be single-character.
- buzzybee 10y agoI self-tested variable name lengths on my own code. Three letters is enough to avoid most collisions. Words do not make sense yet. At four letters most words become decipherable given an appropriate encoding. At five letters a two word phrase may make sense. I make a rough decision based on variable scope - shorter lifetime means shorter variable name, but I rarely go with just one letter as it reduces uniqueness. If I need to use a really long phrase frequently I take a mathematician's approach and alias it to an abstract and highly unique symbol. The phrase may still exist in the addressed data structure, I just avoid it within the algorithm. Mathy code also has a tendency to encourage numbered variables, e.g. "x0, x1, x2".
- rileymat2 10y agoDid you self test on code from years ago or was it all fresh in your mind?
- buzzybee 10y agoI had approximately a month between when I wrote it and reread it. While it was serviceable in short-term memory, comprehension suffered noticeably in the long term with four-letter or less names. At the time I did this I was pursuing Arthur Whitney's extremely terse style to see what advantages and disadvantages it brings. My main modification was to add a full description comment inlined into the declaration of every variable, a practice which I have mostly kept up with even after the experiment ended, e.g.: var tz : String; /* time zone */ This meant that I was not testing on whether I could reconstruct the entire meaning in my head, just whether it made code flow better to use very short names. My discovery was that it does help, up to a point, because you can always create original short names that differentiate better than long abbreviations: result = ttpfe + ttpre; vs. result = q + k; Any time you copy-paste-modify, you create a risk of error. Including the whole phrase slows down your reading and brings diminishing returns on comprehension as it puts friction on actually working with the variable. With the highly differentiated "q" and "k", error has a lower likelihood of slipping in than the abbreviation because you've minimized the amount of noise in the data - it "chunks" better and you can read the whole algorithm more fluidly. The only problem with using such short names is the issue of reconstruction, which is why I opt for aliasing a long name if I need to work with it frequently, even if it means extra boilerplate to make a local variable. Abbreviation is the worst of both worlds and so I try not to do so much of it. Given the original 8-character constraint of OP I might have tried something along the lines of: "edge_t0" "fall0" "ef" Because there is no way to convey the entire phrase, I see it as preferable to focus on a key aspect, expand that word, and push everything else into more symbolic content, especially numbers. Programmers are already trained to look carefully for uses of 0 vs. 1 in our code. That doesn't mean it's my go-to for an abstract symbol, but it works better than a bad abbreviation.
- SimeVidas 10y agoThat’s what linters are for :-P
- bhollan 10y agoHow would a linter catch the presence of a variable that exists?
- hoodoof 10y agolet thisIsCertainlyAnApproachThatIAdvocate = true
- foota 10y agoI have the... pleasure of working with a legacy oracle database where table names are restricted by convention to 8 names.
- gavinpc 10y agoGreat writeup, full of sharp details. In the 90's I worked on a FoxPro application, also for DoD. My boss, a retired Navy Captain, used very terse variable names, partly because he was a hunt-and-peck typist, and partly because earlier languages allowed only two-character names. But FoxPro allowed variable names of any length. However, it only recognized the first ten characters. To my boss's chagrin, I often used names longer than ten characters, risking collisions with other long names. That never happened. But ten years after leaving, I was hired to port the product to Visual FoxPro, which does recognize the whole name. Some of the early commits were "Fixed inconsistently-used long variable name..." Of course, in those days we weren't using linters, or tests, or source control, or reproducible builds... and yet still had a business. No wonder Alan Kay calls computing "not quite a field."
- foota 10y agoHearing that absolutely terrifies me like?? Silently allowing names to collide in that way just sounds ridiculous to me.
- Roboprog 10y agoMany early 90s C compilers did much the same thing, though the limit was usually around 32 chars, rather than merely 10.
- Roboprog 10y agoI remember in college in the mid 80s trying to get something to work on one of the Apple IIs in the library in BASIC, only to discover that my earlier experience with the disk based interpreter supported 6 char names, but the ROM based interpreter only recognized the first 2 chars, but would accept longer names it would not differentiate. Hilarity ensues... And, yeah, I also did a lot of XBase in the late 80s, though usually "Clipper", rather than "Fox". Batch files made a decent build process, and you could be disciplined with regular arc/zip files for source - not too unlike "make clean" and svn. We certainly had a lot of cruddy manual processes otherwise back then, though.
- tehchromic 10y agoIt's the quality of engaging posts like this one that draws me here on a Friday night.
- bhollan 10y agoWhy thank you, sir.
- D-Coder 10y agoTyping is easy. Thinking is hard!
- TheDong 10y agoMay I recommend checking out Rob Pike's document for a counter-point http://doc.cat-v.org/bell_labs/pikestyle http://doc.cat-v.org/bell_labs/pikestyle I think that his variable names section is utterly ridiculous, but it's a relevant read from a relatively prolific person, so worth sharing.
- Roboprog 10y agoThank you for that. That was a good little read. While many of the Bell Labs guys didn't much like Pascal, Turbo Pascal managed to address almost all of the complaints I've ever seen, while preserving the good parts of Pascal (or Modula???). Java must look irredeemable to the Bell Labs folks, though. Perhaps it is: UglyNames; limited structured constant literals; still too clunky lambdas for callbacks.
- ericssmith 10y agoI would suggest that the need for Englishy variable names is due to a weakness in programming languages and possibly the programming model itself. Why should a set of legitimate values for a computation benefit from how you refer to that set? Can that variable take on undesired values? Do you rely on that name and its comprehensibility to distinguish good from bad values? I sometimes find it hard to believe we still program this way.
- correnos 10y agoWe don't have to still program this way - you can write code with very strict types, with machine-checked proofs that it works correctly, etc, etc. We don't do this very often because it turns out this level of rigor is incredibly time-intensive.
- mchahn 10y ago> The term was supposed to be “Ttpfe”, but he had mistakenly called it “Ttpre”, The same cognitive mistake could have happened with it spelled out. Two concepts closely related can easily be confused.
- adrianratnapala 10y agoOne thing I often see elided in these naming wars is scope: am I the only person who gives longer names to globally visible things than to locals? For example: size_t *find_foo(const char *source_text) { const char *src = source_text; for(; src != 'f'; src++) ... do stuff ... do more stuff return src - source_text; } Now the meaning of "source_text" would not be evident, except for the name. But just glancing at the usage shows that "src" is clearly a working cursor into the source text. But if I called it "working_cursor" would that really explain anything to the reader? If anything, giving a detailed name risks misleading readers in much the same way as stale comments can mislead.
- syphilis2 10y agoI say this tongue in cheek: With current IDEs having such great autocompletion, has anyone experimented with coding far outside of the ASCII character set? Programming language restrictions aside, I recognize the obvious troubles this would cause. And yet I do a lot of work on simulations where math formulas are converted to code, which means lots of compounded Greek names like "omegaSquared" or "epsilonMinus". Naming decisions becomes more challenging as subscripts and superscripts are added, yet alone matrix indices. At some point perhaps the symbolic name should be replaced with the descriptive name, such as "first_eccentricity_flat_to_fourth". But it sure would be nice to have access to something with such brevity.
- new_hackers 10y agoI see some new emojis on the horizon!