Live data from Hacker News

Is there an excuse for short variable names?

programmers.stackexchange.com

31–38 of 38 posts

Re: Is there an excuse for short variable names?

#31
I'm surprised Hacker News readers are even having this conversation. :-) Kernighan and Pike's excellent _The Practice of Programming_ addresses it early on; chapter one _Style_, section one _Names_. It includes suggesting length should be inversely proportional to scope and that “clarity is often achieved through brevity”. http://amazon.com/exec/obidos/ASIN/020161586X/mqq-20 http://cm.bell-labs.com/cm/cs/tpop/

Re: Is there an excuse for short variable names?

#32
post #19

Earlier quoted context omitted.

This actually bothered the hell out of me. For anything more complicated that highschool physics, this is terrible. You start to run out of letters and start using capitals and noncapitals, and even worse is that there are some "globals" that are not really variables but well-known constants. So you're trying to distinguish between uppercase / lowercase / variable / constant and keep all that in your head. There real…

Most people tend to get unhappy when you start dismissing their knowledge as crap . Also in this case you are wrong. With a compact notation you can think more complicated thoughts than you can with a verbose one. The downside is that a compact notation requires more from the person reading it. The tradeoff is correct, and the domain expert is not necessarily wrong. Your domain expertise is maintaining a lot of very…

This is a wonderfully lucid and subtle comment. I think any earnest programmer who wants to get better would do well to study your contributions to this thread until they are sure they understand them.

For a domain expert in their domain of expertise, [long explicit names] are overhead.

Once you pass a certain threshold of complexity and/or time-investment, a well-designed program becomes its own domain of expertise. At that point there is great leverage to be had in finding a compact notation suitable for the recurring concepts of the system. In some ways, finding such a notation is how one ensures that the system has a good design and will keep it. As you point out, it (critically) is what enables us to keep more complex things in our heads. It also becomes an intimate part of the creative process – a good notation suggests new concepts and hints at how the system should grow.

Long explicit names are exactly what you don't want when it comes to the core concepts of a well-designed system. They are useful for things that aren't familiar and so need to be spelled out. But in a well-designed system, the most important concepts are familiar and it is a poor use of our limited cognitive capacity to constantly spell them out, for much the same reason that we prefer to say "gas" instead of "liquid hydrocarbons". Since cognitive capacity is our principal bottleneck in software, this is a big deal.

Re: Is there an excuse for short variable names?

#33
post #30

An argument that usually lets programmers understand why physicsists do this: to a physicist variables like x (position), v (velocity), p (momentum), R (radius) are as clear as i,j,k,l to a programmer. Using the longhand would not improve readability for them, just like using arrayIndex instead of i would not improve readability for a programmer. In fact for a physicist it is much easier to read the short variable na…

> If you update an equation like T = dE/dS to temperature = infinitesimal_change_in_energy / infinitesimal_change_in_entropy

and in the case the changes in energy or entropy are not so infinitesimal, the semantics of the variable name can cause all sorts of troubles

Re: Is there an excuse for short variable names?

#34
post #8

Simple solution: in a program source, create a very clear comment header that won't be missed, that explains the meanings of the variables. Also, even though physicists can use any names or lengths they like in their papers, they seem to prefer concision: f = G M1 M2 / r^2 If the reader understands the math, short variable names aid comprehension, not the reverse.

it sounds very programming 101, but I've found getting into the habit of boring old comments at the top of all my methods/functions explaining what the function does, what the input should be, what the output should be, and then simply putting some comments near the top talking about what all the variables are, what they do, why I have them etc. (usually only 4-10 words)...it sounds dumb and time consuming...but this little bit of polish has pulled my ass out of the fire more times than I care to count, and actually helped me write saner code more times than I care to admit (since sometimes writing out the semantics of this stuff helps clarify thoughts that get muddied and confused in the details of code).

you don't have to do this while you're writing the code, often-times just hammering the code out gets you something running, but then doing this after the fact really helps.

Re: Is there an excuse for short variable names?

#35
post #19

Earlier quoted context omitted.

This actually bothered the hell out of me. For anything more complicated that highschool physics, this is terrible. You start to run out of letters and start using capitals and noncapitals, and even worse is that there are some "globals" that are not really variables but well-known constants. So you're trying to distinguish between uppercase / lowercase / variable / constant and keep all that in your head. There real…

Most people tend to get unhappy when you start dismissing their knowledge as crap . Also in this case you are wrong. With a compact notation you can think more complicated thoughts than you can with a verbose one. The downside is that a compact notation requires more from the person reading it. The tradeoff is correct, and the domain expert is not necessarily wrong. Your domain expertise is maintaining a lot of very…

Well, I put it more politely than that, but it's a longstanding nagging issue for me, so I tend to get worked up when talking about it.

I think your point is a very valid one. I guess when I try to read a paper that's "way over my head" academically, it's fair that the authors don't care about me.

But I don't think they know its tradeoff. The way I know this is by reading the majority of intro / mid level textbooks I've had to deal with as a freshman. At that point you're not nearly a domain expert, but the texts are written in the same way.

Re: Is there an excuse for short variable names?

#36
Uh, the example in the article is from a numerical computation, and following conventions (typically, from a book, or a research paper, etc) is often a good idea. Just as long as they are not confusing. An example would be using g for the gravitational constant. If they start becoming confusing, can take them out to a namespace (if the language allows it): just like Math/e is a better practice than e, so Physics/g could be better than g. As a previous comment (http://news.ycombinator.com/item?id=5157849) indicates, when used in a short piece of code, quick synonyms can be used, thus g instead of Physics/g (or Physics/Some_Longer_Name).

As mentioned elsewhere in this thread, short-lived names can often be short, too. In some cases they are little more than placeholders. An example is an index for a for-loop that is in fact a "for-each" loop when the language lacks the latter and the index does not have much of a meaning of its own.

Re: Is there an excuse for short variable names?

#38
After reading many of the responses, I am wondering if there is a difference between the 'business programmers' and the 'scientific/mathematical programmers'. Most of the examples below show code that could have come from physics or maths code. I have noticed that my code (scientific) tends to have much shorter variable names than the code of some of my "not so mathematical" counterparts. Also, 'business' languages like Java and COBOL seem to encourage longer names more than C and FORTRAN.
Post reply on HN