Live data from Hacker News

Is there an excuse for short variable names?

programmers.stackexchange.com

1–10 of 38 posts

Re: Is there an excuse for short variable names?

#3

//for every for(int i = 0; i

    di[ctrl+space] = Math.Sq[ctrl+space](xD+[ctrl+space] 
...

    robo[ctrl+space][index].ohG[ctrl+space]
Every editor I've used in about 10 years has had an excellent autocomplete feature. I've never had to type out such long variable names.

Re: Is there an excuse for short variable names?

#4
post #3

//for every for(int i = 0; i

di[ctrl+space] = Math.Sq[ctrl+space](xD+[ctrl+space] ... robo[ctrl+space][index].ohG[ctrl+space] Every editor I've used in about 10 years has had an excellent autocomplete feature. I've never had to type out such long variable names.

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

Re: Is there an excuse for short variable names?

#5
post #3

//for every for(int i = 0; i

di[ctrl+space] = Math.Sq[ctrl+space](xD+[ctrl+space] ... robo[ctrl+space][index].ohG[ctrl+space] Every editor I've used in about 10 years has had an excellent autocomplete feature. I've never had to type out such long variable names.

Physicist here. I never have to read such long variable names, which would make it much more difficult to spot bugs. I do undertand that long variable names have their place but, when it comes to programming numerical code which is a translation of actual equations, shorter names make it much easier to read and spot mistakes. I do wish that I could use a programming editor that could look like a TeX output on the screen though ...

Re: Is there an excuse for short variable names?

#7
post #3

Earlier quoted context omitted.

di[ctrl+space] = Math.Sq[ctrl+space](xD+[ctrl+space] ... robo[ctrl+space][index].ohG[ctrl+space] Every editor I've used in about 10 years has had an excellent autocomplete feature. I've never had to type out such long variable names.

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.

Re: Is there an excuse for short variable names?

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

Re: Is there an excuse for short variable names?

#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 memory of how I had struggled with it, then I drew the picture, and I was amazed at how easy it was.

The rule is not that you need long or short variables. You need meaningful ones. A short variable name is inherently ambiguous, which can lead to confusion and mistakes. Thinking through anything with a long-variable name abuses your working memory, limiting how complex your thoughts can be.

It is a trade-off. Use the right one for your code. For me, index variables are short, and so are any variables that refer to math concepts that I understand well. Otherwise I use (concise if possible) descriptions without any abbreviation, separated by underscores.

Re: Is there an excuse for short variable names?

#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 math, though, if you had to learn it!"

Post reply on HN