Live data from Hacker News

Is there an excuse for short variable names?

programmers.stackexchange.com

11–20 of 38 posts

Re: Is there an excuse for short variable names?

#11

Earlier quoted context omitted.

Reading is arguably more important than typing. Being able to fit more code on the screen has its advantages, too.

Even in the age of 1080p I agree with you. Side by side code samples, or having documentation up next to code, is very useful. However, if super verbosity pushes your code width to 120 characters, the screen real-estate needed is too much.

Something like thing.horizontalPositionInTransformedAndScaledUnits may be slightly clearer the first time but becomes counterproductive when there are 20 of them on the same screen, especially if there are 20 thing.verticalPositionInTransformedAndScaledUnits mixed in.

Your eyes just start to glaze over (or at least mine do).

If you're only ever using one coordinate system, just use thing.x and thing.y or thing.h and thing.v, maybe with a comment that explains that these are in transformed and scaled units. Your eyes will thank you. :-)

Re: Is there an excuse for short variable names?

#12
Ugh. He's complaining about commonly known physics abbreviations. Short variable names are fine as long as they are descriptive. The key thing to keep in mind is descriptive to you may not be descriptive to someone else (or future you for that matter.)

You can even scrape by with naming the full version in declaration. nothing worse than a cryptic name you cant figure out at first glance like in this instance.

Re: Is there an excuse for short variable names?

#14
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.

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 really is no logical reason to do this, other than established practice.

I've found that even scientists and mathematicians can be closeminded enough to get angry when you suggest that they're being inefficient and wasteful, and their conventions are crap.

Don't even get me started on the mathematician way of explaining things, where instead of explaining the concept and filling in the gaps, you take a 20 variable equation and start by describing all the variables one by one. By the time you reach the end, you can't remember what the beginning did, so you have to go back and forth all the time.

Re: Is there an excuse for short variable names?

#15
post #10
post #9

I am reminded of a complicated piece of indexing that I had to figure out. I kept on getting confused, reversing things, etc. Eventually I put a big comment at the top that to understand the code you needed to draw the picture, and named my variables x and y. About a page of code later, I was done. And when I has to tinker with it a couple of years later my first reaction was to wonder WTF I was thinking, wince in me…

A short variable name is not ambiguous when the code is based on some math or physics that has a more or less standardized notation. In this case, what's needed is to connect the variable names to the mathematics, as Rex Kerr pointed out to the OP: "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 ma…

A short variable name is not ambiguous when the code is based on some math or physics that has a more or less standardized notation.

The problem is that the same notation is often used in different parts of math in different ways. You sometimes need to clarify. (Except in the case of differential geometry - there the notation is so horrible that you should find a different job! I only partly kid...)

Conversely even if you aren't using a standard math notation, meaningless names and a simple labeled diagram can often make something unambiguous in a way that no amount of verbiage in variable names can hope to do.

Re: Is there an excuse for short variable names?

#16
You might be able to make the case that short variable names are reasonable as long as they are common and used consistently.

I am not completely sold on this, but for example LLVM has a lot of 1 character variables where 'I' is always the instruction you're working on, 'F' is always the function you're compiling, 'B' is the current basic block you're processing, etc.

Having worked on several compilers over the year, it's certainly not much worse than other practices I have seen like using 't1' and 't2' to refer to the two instruction temporaries you just created, or 'insn' for the instruction you're currently examining.

Re: Is there an excuse for short variable names?

#17
Variable length should be comparable to the length they're in scope and their importance. So short indices for loops is fine. S for a string in a loop over a list of strings is fine (so long as the loop isn't too long).

Assuming everyone knows your equation and what your variables mean is a terrible idea. Unless your method is less than 10 lines and you explain all the variables in a comment... just write stuff out. Text is free. Don't go crazy, but 4-8 characters won't kill anyone and may well prevent bugs.

Re: Is there an excuse for short variable names?

#18
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.

You mean a comment header like http://futureboy.us/frinkdata/units.txt? No matter how clear and detailed, there comes a point when nobody is likely to remember much of it. (And that would be an exemplary example of clarity. Read it if you don't believe me.)

There is an upper limit to how much state we can stuff into our head. We can memorize things for longer. But unless we will amortize that state over many, many uses, the effort is not worthwhile.

That said, if we have taken the effort to memorize it, there is no reason not to leverage the already spent effort.

Re: Is there an excuse for short variable names?

#19
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.

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 precise instructions for a variety of different problems. For you, constant signposts are an assistance. For a domain expert in their domain of expertise, they are overhead.

Re: Is there an excuse for short variable names?

#20
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.

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…

The comment by btilly is ok as the mathematicians are trying to communicate in a highly efficient manner which is not the same as communicating with us mere mortals.
Post reply on HN