140

This has become a large frustration with the codebase I'm currently working in; many of our variable names are short and undescriptive. I'm the only developer left on the project, and there isn't documentation as to what most of them do, so I have to spend extra time tracking down what they represent.

For example, I was reading over some code that updates the definition of an optical surface. The variables set at the start were as follows:

double dR, dCV, dK, dDin, dDout, dRin, dRout
dR = Convert.ToDouble(_tblAsphere.Rows[0].ItemArray.GetValue(1));
dCV = convert.ToDouble(_tblAsphere.Rows[1].ItemArray.GetValue(1));
... and so on

Maybe it's just me, but it told me essentially nothing about what they represented, which made understanding the code further down difficult. All I knew was that it was a variable parsed out specific row from a specific table, somewhere. After some searching, I found out what they meant:

dR = radius
dCV = curvature
dK = conic constant
dDin = inner aperture
dDout = outer aperture
dRin = inner radius
dRout = outer radius

I renamed them to essentially what I have up there. It lengthens some lines, but I feel like that's a fair trade off. This kind of naming scheme is used throughout a lot of the code however. I'm not sure if it's an artifact from developers who learned by working with older systems, or if there's a deeper reason behind it. Is there a good reason to name variables this way, or am I justified in updating them to more descriptive names as I come across them?

Victor Engel
  • 173
  • 9
KChaloux
  • 5,773
  • 4
  • 35
  • 34
  • 98
    It appears to me that in that specific case, those are variable names copied directly from a longhand math formula. – Dave Nay Nov 20 '12 at 20:36
  • 1
    the only excuse I would accept would be **["My peer told it's OK after reviewing the code"](http://programmers.stackexchange.com/a/141010/31260 "paraphrased from another answer")** – gnat Nov 21 '12 at 15:37
  • 35
    If you find yourself unable to understand mathematical code because of short variable names, be aware that it may be because you don't understand the mathematics, not because the names are too short. Altering mathematical expressions you don't understand is not a high-reliability process. Once you understand the math, the length of the variable names is irrelevant. Do others a favor and leave a citation (in a comment) to some relevant description of the math, though, if you had to learn it! – Rex Kerr Nov 21 '12 at 17:02
  • 1
    Not an answer because I quite can't understand it yet, but in Haskell (and FP in general, I guess) the consensus seems to be to write 1 or 2-character variable names. (this is unrelated to common Physics or Maths abreviations). I'm still not used to it, but it seems to be related to the extreme generality FP programmers strive for. So lists are usually named "xs", with a particular element named "x"; longer names seem to be frowned upon. And yes, industrial strength software IS written with Haskell/FP. – Andres F. Nov 22 '12 at 16:40
  • 1
    My guess is that longer names make sense in OOP because _meaning_ is derived from them, whereas in FP, where generality and abstraction are much higher, succinctness is more valued. – Andres F. Nov 22 '12 at 16:45
  • possible duplicate of [Are short identifiers bad?](http://programmers.stackexchange.com/questions/24077/are-short-identifiers-bad) – dietbuddha Nov 24 '12 at 20:57
  • You started writing code in the 70/80s and haven't learned any better? (you didn't say it had to be a good excuse). – Evan Plaice Dec 14 '12 at 01:09
  • 3
    Any identifier that is named after Donkey Kong is approved: `dK = conic constant`. – Thomas Eding Dec 14 '12 at 01:38
  • 8
    Conversely, one could ask if ignorance of the domain field justifies ignoring its conventions. In the end, it depends on the context. – Daniel C. Sobral Feb 02 '13 at 17:49
  • related: [Why are cryptic short identifiers still so common in low-level programming?](http://programmers.stackexchange.com/questions/162698/why-are-cryptic-short-identifiers-still-so-common-in-low-level-programming) (possibly a duplicate, as [top answer about Zipf's Law](http://programmers.stackexchange.com/a/183578/31260) applies here) – gnat Sep 16 '14 at 19:05
  • I always strive to make them as short as possible while clearly indicating meaning, this can make them quite long sometimes though ) but usually they are just fine since I have - an IDE... occasionally for "temporary" variables that are used for a few lines, I go very short like first letters of each word. Basically I guess there are 3 main variable naming techniques I use.. all full words, initial letters then a word (or two) (e.g. tfOrder) the prefix letters being short forms of the full word they represent, or just initial letters.. e.g. `tf` for short-lived variables sometimes – ycomp Sep 11 '17 at 00:33
  • Excuse is a pejorative term. IF a name is both accurate and well understood, THEN the name's length is irrelevant. (i in for loops control variables, n for list/sequence index, implied variables in some scripting languages for example). However, there is no excuse for bad names. – Kristian H Aug 12 '18 at 17:38

18 Answers18

234

It appears that these variable names are based on the abbreviations you'd expect to find in a physics textbook working various optics problems. This is one of the situations where short variable names are often preferable to longer variable names. If you have physicists (or people that are accustomed to working the equations out by hand) that are accustomed to using common abbreviations like Rin, Rout, etc. the code will be much clearer with those abbreviations than it would be with longer variable names. It also makes it much easier to compare formulas from papers and textbooks with code to make sure that the code is actually doing the computations properly.

Anyone that is familiar with optics will immediately recognize something like Rin as the inner radius (in a physics paper, the in would be rendered as a subscript), Rout as the outer radius, etc. Although they would almost certainly be able to mentally translate something like innerRadius to the more familiar nomenclature, doing so would make the code less clear to that person. It would make it more difficult to spot cases where a familiar formula had been coded incorrectly and it would make it more difficult to translate equations in code to and from the equations they would find in a paper or a textbook.

If you are the only person that ever looks at this code, you never need to translate between the code and a standard optics equation, and it is unlikely that a physicist is ever going to need to look at the code in the future perhaps it does make sense to refactor because the benefit of the abbreviations no longer outweighs the cost. If this was new development, however, it would almost certainly make sense to use the same abbreviations in the code that you would find in the literature.

Justin Cave
  • 12,691
  • 3
  • 44
  • 53
  • 17
    And if there are no physicists or mathematicians on staff? The code was essentially left to me. It's largely undocumented. Would it be *more* difficult to read the descriptive names? – KChaloux Nov 20 '12 at 20:40
  • 4
    I think many of the downsides of the abbreviation can be eliminated by properly commenting the code. – Joe Nov 20 '12 at 20:47
  • 2
    I'll give this the answer, because it provided both a legitimate consideration for using the shortened notation, and legitimate reasons to prefer a longer naming convention. – KChaloux Nov 20 '12 at 20:48
  • 127
    +1 It's not unreasonable to expect a programmer to understand the domain he's working in. On the other hand, in mathematical works, the variables are usually defined in a convenient place. In code like this I would expect a prominent comment showing the long form. – Karl Bielefeldt Nov 20 '12 at 20:52
  • 15
    +1: What you're basically saying is that a programmer who understands the domain that they're working in will understand the variable names. This is going to be the same in many projects even with longer variable names. eg. a variable named `column` in a military strategy game very likely means something different than it would in charting software. – Steven Evers Nov 20 '12 at 20:57
  • 1
    The OP states the code is undocumented, however it appears in this case there probably is, in physics text books. I would just about put money on it - if he looks around some book shelves he will find the book the code was developed from. – mattnz Nov 20 '12 at 21:04
  • 1
    if you do use the abbreviations like that and expect both types to need to debug it, then have a comment somewhere nearby to clarify what it means for the uninitiated – ratchet freak Nov 20 '12 at 21:05
  • 23
    In medical programming you'll often find terms that persist into the programming sphere. For example with ophthalmology you'll see OU, OD, and OS. These denote which eye, for example ouSphere may reference the 'sphere' component of a eyeglass prescription for the right eye. You'll be a better programmer if you understand the business domain you are programming for. – Bill Nov 20 '12 at 23:57
  • 1
    You might find it interesting that, after searching through other parts of the code, I found that they were generally referred to by their longer names. The abbreviations appear to only have been used in that one function. Our Asphere class actually defines properties for `Curvature`, `ConicConstant`, `InnerRadius`, etc. – KChaloux Nov 21 '12 at 00:14
  • 11
    +1 for noting that short names that match those in equations & formulas from papers and texts. When I do that, I include the comments on where to find the original reference material . – Jay Elston Nov 21 '12 at 01:08
  • 2
    This is a case where Intellisense can make a real difference, providing easy access to the variable's alternate description. – Hand-E-Food Nov 21 '12 at 01:17
  • 6
    If the variable names really are taken from a known forumla as others suggest, I would avoid refactoring them and simply add a comment above their declaration referencing the forumla. It's the best of both worlds. – Chris Nov 21 '12 at 01:59
  • 2
    The reason may be right, but it's still a bad idea. – Ross Patterson Nov 21 '12 at 02:26
  • 2
    Short variable names are a problem that I frequently come across. In my experience, the problem is not confined to the domain of programming:- the academic culture present in many mathematical sciences is competitive rather than collaborative. As a result of this, many forms of technical communication have unnecessarily high barriers to entry. (papers, textbooks as well as software source files). Brevity and concision are touted as benefits, but the level of deliberate obfuscation goes beyond what is necessary, to a point where perverse pride is taken in elitist possession of arcane knowledge. – William Payne Nov 21 '12 at 11:56
  • Nobody has mentioned a pretty reasonable answer that I've run across. I know many old C compilers didn't support variables > 8 characters. So people fell back into common abbreviations. – Doug T. Nov 21 '12 at 15:30
  • These variables are probably used in mathematical formulae in the code. Mathematical formulae are *much* easier to read with short variable names. I use long variable names in most of my code, but maths is better with short variable names. Random example: consider how long [this](http://upload.wikimedia.org/math/3/8/e/38e4e5d25cb29a13b63e9f6e6bb099ef.png) would be with long variable names. – MarkJ Nov 21 '12 at 16:17
  • 1
    First, pseudo-Hungarian notation is I think mistake. Second, one usually denote subscript with `_`. Therefore I think more natural would be to use e.g. `R_in` instead of `dRin`. And of course there should be a comment with variable description and an expression that uses them. – Jakub Narębski Feb 12 '13 at 22:29
  • @JakubNarębski Yes, subscripts like `R_in` are recognizable by most researchers because they are the standard way of indicating subscripts in LaTeX, etc. Also some studies monitoring eye movements suggest camelCase is less readable than snake_case anyways. – JoseOrtiz3 Jun 28 '18 at 07:40
89

Variables with short lifetimes should be named shortly. As an example, you don't write for(int arrayCounter = 0; arrayCounter < 10; arrayCounter++) { .... Instead, you use for(int i ....

In general rule of thumb it could be said that the shorter the variable scope the shorter the name should be. Loop counters are often only single letters, say i, j and k. Local variables are something like base or from and to. Global variables are then somewhat more elaborate, for example EntityTablePointer.

Perhaps a rule like this isn't being followed with the codebase you work with. It's a good reason for doing some refactoring though!

zxcdw
  • 5,075
  • 2
  • 29
  • 31
  • I'm going to give this a +1, for bringing up a distinction I didn't make about temporary variables and especially iterators. Since there's a common, short convention, I'd agree, and it's worth mentioning (even though it wasn't really what I was concerned about). – KChaloux Nov 20 '12 at 20:43
  • 14
    i, j and k for array indices represent a tradition going back at least to Fortran. Any self-respecting programmer will be familiar with this convention. – Dominic Cronin Nov 20 '12 at 21:24
  • 7
    I usually don't write `i`, `j`, `k`, I write something like `personPos` because then I won't forget what I'm iterating on and what the index represents. – Malcolm Nov 21 '12 at 04:56
  • @DominicCronin: a lot further than that. At least back to 1901: http://jeff560.tripod.com/matrices.html – naught101 Nov 21 '12 at 06:54
  • @Malcolm - yes, if that adds value - agreed. And of course, in situations where you are using `i`, you probably need a foreach instead, but still, there are places where what you want to signal is simply `the current item I'm on`, in which case, the i,j,k convention works well. – Dominic Cronin Nov 21 '12 at 09:19
  • 18
    -1, I do use long names for array index iterations, but I give them meaningful name instead of obvious and **meaningless** `arrayCounter`. Makes code easier to read and at very least saves you from stomping all over outer counter when you copy/paste another loop inside this one. – Oleg V. Volkov Nov 21 '12 at 11:10
  • There is a smell in this code, don't make a variable description of an action. Variables do not execute actions, while function execute actions. The variable should describe what the data represent, and "counter" is an action. While "ArrayIndex", is a better way to describe it, while there is another smell in that, where you don't smurf naming, or better yet, Hungarian nutation, unless you REALLY HAVE TO, so Index, would be better. of Position. do you get it? – Display Name Nov 21 '12 at 11:33
  • 3
    counter is a noun or an adjective. Count is a noun or a verb. Of course, I still wouldn't call something `arrayCounter`, I'd use `personIndex` or `row` or whatever described what I'm looking at. – Wayne Werner Nov 21 '12 at 13:45
  • There's another exception. The variables in the question are probably used in mathematical formulae in the code. Mathematical formulae are *much* easier to read with short variable names. I use long variable names in most of my code, but maths is better with short variable names. Random example: consider how long [this](http://upload.wikimedia.org/math/3/8/e/38e4e5d25cb29a13b63e9f6e6bb099ef.png) would be with long variable names. – MarkJ Nov 21 '12 at 16:19
  • 6
    When I'm doing a single loop, I use `i`. But as soon as I transition to a nested loop, I drop the `i` nomenclature. The reason for this is because I really want to track which loop variable is being worked with, and `i` vs `j` can be quite deceiving in dense code. Making it more readable, and adding more whitespace, is inherently necessary to me. – jcolebrand Nov 21 '12 at 18:07
  • +1, I think your first sentence is a beautiful guideline to go by. This also aligns with [Spartan Programming](http://ssdl-wiki.cs.technion.ac.il/wiki/index.php/Spartan_programming) (also [here](http://www.codinghorror.com/blog/2008/07/spartan-programming.html)), a style I try to strive to (though it's a bit on the extreme side). – Oak Nov 22 '12 at 08:27
  • -1,"the shorter the variable scope the shorter the name" Why? What's the benefit of having short names? You can't argue that short names is good because it's possible to have bad long names. All non-trivial variables (and possibly they too) should have good descriptive names. Scope is irrelevant. – AndSoYouCode Nov 22 '12 at 16:12
  • 1
    @AndSoYouCode The original insight for this came from Rob Pike's and Brian Kernighan's book called The Practice of Programming. "Programmers are often encouraged to use long variable names regardless of context. That is a mistake: *Clarity is often achieved through brevity*." In a local scope a variable named `n` might suffice, `npoints` would perhaps be most suited and `number_of_points` is overkill, even more so when the scope is as short as a single trivial few-line for-loop. Just as there can be excess comments, there can be excess burden in variable names which serves no purpose. – zxcdw Nov 22 '12 at 17:14
  • Make the trivial trivial and the non-trivial will stand out. – JeffV Dec 17 '12 at 17:08
  • @zxcdw: Another thing is that when using variables to hold results from temporary expressions, the *expression with which the variable is written* will be a clearer and more accurate explanation of what it contains than would a verbose name. I wish languages had a combined variable declaration/assignment syntax which was optimized for that--specifying that anyplace where the variable was read it would be guaranteed to hold the value written by the previous such statement in the source code, and allowing the name to be used only by such statements, and only when the guarantee would be upheld. – supercat Jan 18 '14 at 16:57
48

The problem with the code is not the short names, but rather the lack of a comment which would explain the abbreviations, or point to some helpful materials about the formulas from which the variables are derived.

The code simply assumes the problem-domain familiarity.

That is fine, since problem-domain familiarity is probably required to understand and maintain the code, especially in the role of someone who "owns" it, so it behooves you to acquire the familiarity rather than to go around lengthening names.

But it would be nice if the code provided some hints to serve as springboards. Even a domain expert could forget that dK is a conic constant. Adding a little "cheat sheet" in a comment block wouldn't hurt.

Kaz
  • 3,572
  • 1
  • 19
  • 30
  • Ultimately ended up going with something like this. – KChaloux Nov 21 '12 at 13:42
  • 5
    This is wrong. There is no reason to make your code hard to read in order to force people to spend more time "learning" it. You're not teaching, you're just slowing people down. Also, there is virtually never a single owner of code forever. You're being short sighted and selfish. – xaxxon Feb 03 '13 at 03:57
  • 7
    It is your interpretation that is wrong, not the answer. The code implements some math which exists outside of that code and independently of it, and which came before the code. Keeping the same naming is helpful. Someone who maintains the code will probably have to study that outside math, and not having to map between two naming schemes will help that person. – Kaz Feb 03 '13 at 04:09
19

For certain variables which are well-known in the problem domain -- like the case you have here -- terse variable names are reasonable. If I'm working on a game, I want my game entities to have position variables x and y, not horizontalPosition and verticalPosition. Likewise loop counters that don't have any semantics beyond indexing, I expect to see i, j, k.

  • I'd argue that cv, din, and dout aren't immediately obvious (though apparently they are established in specific fields). I wouldn't argue about x and y, seeing as Cartesian coordinates are applicable for any number of fields, and are taught to everyone in middle and high school. – KChaloux Nov 21 '12 at 01:01
  • 1
    And `x` and `y`, while common, don't carry implicit important aspects like where the origin in. – Ross Patterson Nov 21 '12 at 02:28
  • 3
    @RossPatterson: however, there are many contexts where that simply is not important. For example, consider a loop such as `for (x,y) in list_of_coordinates: print x,y`: The origin doesn't particularly matter when trying to understand the code. – Bryan Oakley Nov 21 '12 at 15:00
16

Acording to "Clean Code":

Variable names should:

  • Be intention revealing
  • Avoid disinformation
  • Make meaningful distinctions
  • Be pronounceable
  • Be searchable

Exceptions are the proverbial i,j,k,m,n used in for loops.

The variable names you, rightfully, complain about do nothing of the above. Those names are bad names.

Also, as every method must be short, using prefixes to indicate either scope or type is no longer in use.

This names are better:

radius
curvature
conicConstant
innerAperture
outerAperture
innerRadius
outerRadius

A commenter says that this would be too complex with long variable names:

enter image description here

Short variable names don't make it simple either:

fnZR = (r^2/fnR(1+Math.sqrt((1+k) * (r^2/R^2)))) + a[1]*r^2 + a[1]*r^4 + a[1]*r^6 ...;

The answer is long names and intermediate results until you get this at the end:

thisNiceThing =  ( thisOKThing / thisGreatThing ) + thisAwsomeThing;
Tulains Córdova
  • 39,201
  • 12
  • 97
  • 154
  • 26
    These variables are probably used in mathematical formulae in the code. Mathematical formulae are *much* easier to read with short variable names. I use long variable names in most of my code, but maths is better with short variable names. Random example: consider how long [this](http://upload.wikimedia.org/math/3/8/e/38e4e5d25cb29a13b63e9f6e6bb099ef.png) would be with long variable names. – MarkJ Nov 21 '12 at 16:19
  • @MarkJ You are right. – Tulains Córdova Nov 21 '12 at 20:58
  • 1
    Intermediate results have the added benefit of reducing and isolating programming errors. Our brains can only process so many pieces of information at a time, and it's tough to spot the error in something like `fnZR = (r^2/fnR(1+Math.sqrt((1+k)) * (r^2/R^2))) + a[1]*r^2 + a[1]*r^4 + a[1]*r^6 ...;` – eyelidlessness Feb 02 '13 at 17:55
  • 1
    It's not that tough if that's your bread and butter. – Radu Feb 02 '13 at 18:30
  • 1
    The point is not to write a one-liner. But the problem is that if you need to change something and use another formula now you have a problem where it takes a long time to figure out which `long_name` the `R` in the textbook refers to. Use intermediate results, but keep your complex maths as close as possible to the problem domain so that you can actually maintain it if you need to add a feature. – slebetman Sep 17 '14 at 06:25
8

There are two good reasons not to rename variables in legacy code.

(1) unless you're using an automated refactoring tool, the possibility of introducing bugs is high. Hence, "if it's not broken, don't fix it"

(2) you will make comparing current versions with past versions, in order to see what changed, impossible. This will make future maintenance of the code more difficult.

gnat
  • 21,442
  • 29
  • 112
  • 288
ddyer
  • 4,060
  • 15
  • 18
5

Is there a good reason to name variables this way, or am I justified in updating them to more descriptive names as I come across them?

The reason to use smaller names is if the original programmer finds them easier to work with. Presumably they have the right to find that to be the case, and the right not to have the same personal preferences that you have. Personally, I'd find...

dDin better than dDinnerAperture
dDout better than dDouterAperture

...if I was using them in long, complex calculations. The smaller the math expression, often the easier it is to see the whole thing at once. Though if that was the case, they might be better as dIn and dOut, so there wasnt a repetitive D that could lead to an easy typo.

On the other hand, if you find it harder to work with, then knock yourself out and rename then to their longer form. Especially if you are responsible for that code.

GrandmasterB
  • 37,990
  • 7
  • 78
  • 131
  • 7
    At the very least I'm certainly getting rid of the Systems Hungarian notation... – KChaloux Nov 20 '12 at 21:03
  • 4
    The leading `d` is Hungarian? I thought it was calculus as in in `dx^2/dx = 2x`. – Shannon Severance Nov 20 '12 at 23:20
  • @ShannonSeverence I'm fairly sure he's using it to denote "double" here. It's done throughout the codebase. The same with "i" for integers. – KChaloux Nov 21 '12 at 00:05
  • 7
    If I saw `dDinnerAperture` by itself, I'd read it as "Dinner Aperture", and wonder if it's just a funny way of saying "your mouth". Never been a fan of the "capital letter followed by lower-case word) style. Very confusing sometimes. – Darrel Hoffman Nov 21 '12 at 03:35
  • 2
    +1 this is the answer. Long maths formulae are *much* easier to read with short variable names. These variables are probably used in mathematical formulae in the code. Mathematical formulae are *much* easier to read with short variable names. I use long variable names in most of my code, but maths is better with short variable names. Random example: consider how long [this](http://upload.wikimedia.org/math/3/8/e/38e4e5d25cb29a13b63e9f6e6bb099ef.png) would be with long variable names. – MarkJ Nov 21 '12 at 16:20
  • 1
    @ShannonSeverance I agree with you. Coming from an engineering background, d should definitely be reserved for calculus when using variable names in the mathematical sense (as it is often mentioned here). – CodeMonkey Mar 01 '17 at 09:28
4

Generally, I believe that the rule for this, should be that you can use extremely short variable names where you know that people who are "skilled in the art" of your particular code will immediately understand the reference of that variable name. (You always have comments for the exception of this case anyway), and that the localized usage of the variables can easily be discerned based on the context of how they are used.

To expand on this, it means that you shouldn't go out of your way to obfuscate your variable names, but, you can use abbreviations for your variables names, where you know that only people who understand the underlying concept of your code are likely to read it anyway.

To use a real world example, recently, I was creating a Javascript class that would take a latitude, and tell you the amount of sunlight that you would expect on a given date.

To create this Sundial class, I referred to perhaps half a dozen resources, the Astronomy Almanac, and snippets from other languages, (PHP, Java, C etc).

In almost all of these, they used similar identical abbreviations, which upon the face of it mean absolute nothing.

K, T, EPS, deltaPsi, eot, LM, RA

However, if you have knowledge of physics you can understand what they were. I wouldn't expect anyone else to be touching this code, so why use verbose variable names?

julianTime, nutationOfEclipticalLongitudeExpressedInDegrees, equationOfTime, longitudeMean, rightAscension.

Additionally, a lot of the time, when variable names are transitory, that is to say, they are only used to temporarily allocate some value, then it frequently doesn't make sense to use a verbose version, especially when the context of the variable explains it's purpose.

Laykes
  • 401
  • 2
  • 6
1

There absolutely is; often times a short variable name is all that is necessary.

In my case, I am doing waypoint navigation in my senior Robotics class, and we program our robots in KISS-C. We need variables for current and destination (x, y) co-ordinates, (x, y) distances, current and destination headings, as well as turn angles.

Especially in the case of x and y co-ordinates, a long variable name is completely unnecessary, and names such as xC (current x), yD (destination y), and pD (destination phi), suffice and are the easiest to understand in this case.

You might argue that these aren't 'descriptive variable names' as programmer protocol would dictate, but since the names are based on a simple code (d = destination, c = current), a very simple comment at the outset is all the description they require.

0

Is there a good reason to name variables this way, or am I justified in updating them to more descriptive names as I come across them?

Usually, complex algorithms are implemented in matlab (or similar language). What I have seen is people just taking over the variables name. That way, it is simple to compare implementations.

All other answer are almost correct. These abbreviations can be found in math and physics, except they do not begin with d (as in your example). Variables beginning with d are usually named to represent differentiation.

All normal coding guides are telling not to name variables with the first letter representing type (as in your case), because it is so easy to browse the code in all modern IDEs.

BЈовић
  • 13,981
  • 8
  • 61
  • 81
  • Right, that element was going away regardless of what people ended up saying. There's a sort of half-Hungarian half-not schizophrenia going on in the codebase that I'm updating as I find it. – KChaloux Nov 21 '12 at 20:20
0

I can think of a reason for variable names to be reasonably short.

The short names are easy to read using shorter eye-span, hence a short attention span.

For example, once I get used to the fact that svdb means "save to the database", the rate of scanning the source code gets better as I only have to quickly scan 4 characters instead of when reading SaveToDatabase (14 characters, things get worse for more complex operation names). I say "scanning" not "reading", because that takes a major part of analyzing source code.

When scanning through large amount of source code, this can provide good performance gains.

Also, it just helps the lazy programmer to type out these short names when writing code.

Of course, all these "shorthands" is expected to be listed in some standard location in the source code.

Amol
  • 143
  • 7
  • 1
    We don't read words as a set of characters, we read them as shapes and resolve details conditionally when at-a-glance comprehension fails. The length a word must be to take longer to read is far greater than 4 characters, and we're able to mentally process camelCase and other means of breaking up variable name words as word boundaries. I doubt a reading test would bear out a claim that shorter names improve reading comprehension speed; instead, by removing recognizable words, there will be a speed decrease until the new words are assimilated (which you yourself point out). – eyelidlessness Feb 02 '13 at 18:07
0

To frame what @zxcdw said in a slightly different way, and elaborate on it in terms of approach:

Functions should be pure, brief and a perfect encapsulation of some functionality: Black box logic, regardless of whether it has been sitting untouched for 40 years, it will continue to do the job it was designed for, because its interface (in and out) is sound, even as you know nothing of its internals.

This is the kind of code you want to write: this is code that lasts, and is simple to port.

Where necessary, compose functions out of other (inline) function calls, to keep code fine-grained.

Now, with a suitably descriptive function name (verbose if necessary!), we minimise any chance of misinterpretation of those shortened variable names, since the scope is so small.

Engineer
  • 767
  • 5
  • 16
-1

Variable names should be as descriptive as possible to help the readability of the program. You experienced it yourself: you had a lot of trouble identifying what the program did because of the poor naming.

There is no good reason not to use descriptive name. It will help you, and everyone else that works/will work on the project. Well actually there is a single valid use for short names: loop counters.

marco-fiset
  • 8,721
  • 9
  • 35
  • 46
  • 6
    Note that `int dummy_variable_for_for_loops;` is as descriptive as possible. – Kaz Nov 20 '12 at 23:50
  • 4
    -1 because this answer is confusing. First it says there are no good reasons, then finishes by saying that actually, there are. The answer would be better if it was rephrased not to contradict itself. – Bryan Oakley Nov 21 '12 at 00:50
  • I'd change that to read "as descriptive as *necessary*". If a short name conveys the variable's purpose, then *it's OK to use a short name*. – Maximus Minimus Nov 22 '12 at 09:02
-1

Surely this is what // comments are for?

If the assignments have comments which are descriptive you get the best of both worlds: a description of the variable and any equations are easily comparable with their text book counterpart.

Richie
  • 11
-1

For interfaces (e.g., method signatures, function signatures) I tend to solve this by annotating the parameter declarations. For C/C++ this decorates the .h file as well as the code of the implementation.

I do the same for variable declarations where knowing the usage of the variable is not obvious in context and in the naming. (This applies in languages that don't have strong typing also.)

There are many things that we don't want to clog the variable name. Is the angle in radians or degrees, is there some tolerance on precision or range, etc. The information can provide valuable assertions about characteristics that must be dealt with correctly.

I am not religious about it. I am simply interested in clarity and in making sure that I now what it is the next time my forgetful self visits the code. And anyone looking over my shoulder has what they need to know to see where something is off, what is essential (the last critical for proper maintenance).

orcmid
  • 111
  • 3
-1

Is there an excuse for excessively short variable names?

First: Naming a variable for energy e while calculating formulas such as E=MC2 is NOT excessively short naming. Using symbols as an argument for short names is not valid

This question was rather interesting to me, and I can only think of one excuse and that is money.

Say for instance you're wiring javascript for a client which knows that the file is to be downloaded many times a second. It would be cheaper and make the experience of the users better if the file (in number of bytes) is as small as possible.

(Just to keep the example 'realistic', you were not allowed to use a minifier tool, why? Security issues, no external tools allowed to touch the code base.)

AlexanderBrevig
  • 525
  • 2
  • 6
-1

I notice that the other answers don't mention the use of Hungarian Notation. This is orthogonal to the length debate, but relevant to naming schemes in general.

double dR, dCV, dK, dDin, dDout, dRin, dRout

The "d" at the beginning of all these variables is meant to indicate that they're doubles; but the language is enforcing this anyway. Despite the terseness of these names, they're up to 50% redundant!

If we're going to use a naming convention to reduce errors, we're much better off encoding information which isn't being checked by the language. For example, no language will complain about dK + dR in the above code, even though it's meaningless to adding a dimensionless number to a length.

A good way to prevent such errors is to use stronger types; however, if we're going to use doubles then a more appropriate naming scheme might be:

// Dimensions:
// l  = length
// rl = reciprocal length
double lR, rlCV, K, lDin, lDout, lRin, lRout

The language will still allow us to write K + lR, but now the names give us a hint that this might be incorrect.

This is the difference between Systems Hungarian (generally bad) and Apps Hungarian (possibly good)

http://en.wikipedia.org/wiki/Hungarian_notation

Warbo
  • 1,205
  • 7
  • 11
  • 1
    Actually, C++ can complain about `K + lR` if you properly declare your units. With SI units it can do dimensionality checks and unit conversion. Adding `3_feet + 2_meter` should be no problem, while `2_meter+1_second` should be a compile-time error. – MSalters Sep 17 '14 at 11:46
  • @MSalters That's pretty cool; a template/macro approach hadn't occurred to me. Still, in a "language-agnostic" question like this I'd group all compile-time unit checks under the "stronger types" umbrella, regardless of whether the language calls them "types" or not ;) – Warbo Sep 17 '14 at 16:01
  • Template, necessarily. You can't do the necessary type calculus in macro's. – MSalters Sep 18 '14 at 15:20
-2

The only case where illegibly short variable names are acceptable in modern software engineering is when they exist in a script and the throughput of that script (over a network generally) is important. Even then, keep the script with long names in source control, and minify the script in production.

Telastyn
  • 108,850
  • 29
  • 239
  • 365
  • This is in C#, so I can't say we even have that excuse... – KChaloux Nov 20 '12 at 20:36
  • 9
    What about when performing mathematical operations where the terms are considered common knowledge in the domain? Example: using M instead of "mass" in e=mc^2 – Andy Hunt Nov 20 '12 at 20:38
  • 2
    @andyBursh - I'd be careful there. Programmers are not often experts (or even competent) in their problem domain. That said, this sort of thing can be okay, especially if there's the appropriate comment link to the formula in question; I had forgotten about this scenario. – Telastyn Nov 20 '12 at 20:44
  • 8
    @Telastyn - If you assume that the programmer is not an expert, though, that often leads to taking abbreviations that have a very well defined meaning and turning them into longer names that are at least somewhat ambiguous (i.e. someone decides to name a local variable `radius` when it stores the inner radius not understanding that is much more ambiguous than `Rin`). And then the non-expert developer has to translate between his unique nomenclature and the nomenclature that the business understands every time there is a discussion involving equations. – Justin Cave Nov 20 '12 at 21:07
  • 2
    What about "i" for a loop counter? Or "x" and "y" when dealing with a coordinate? Or a variable whose scope is only two lines of code? – Bryan Oakley Nov 21 '12 at 00:51
  • Also highly generic code will often use short abstract names as there is nothing useful you can name the things anyway. e.g. flip f x y = f y x – jk. Nov 21 '12 at 10:10
  • 4
    Disagree with this answer. A programmer working on a program containing *complex* mathematical expressions had better be able to at least *read* the formulae in the spec, otherwise they're not competent. That's not problem domain knowledge, that's an essential skill - some basic maths competency before working on a mathematical program. If the programmer doesn't have access to the spec formulae, that's another problem - and it can't be solved by comments alone. – MarkJ Nov 21 '12 at 16:22