Hacker News new | ask | show | jobs
by joesmo 3707 days ago
When was the last time you read a math formula that was two million lines long? I don't see why short variable names would be better. With a proper editor you don't even save on typing and I've never seen code with short names that was easier to read than the same code with variable names of proper length that reflect their semantics (not necessarily long).
3 comments

When are you long variable name fanatics going to understand that it is NOT about typing ;-) It is in fact about READING! People who think longer is better simple do not have a insight into how the human brain process information.

Read up on good user interface design. One of the key points is that texts should be short and the shape of words easily distinguishable. index1, index2, index3 might be more descriptive than i, j, k but the latter have more unique overall shape which makes it easier to identify for a reader. Likewise CoordinateX, CoordinateY, CoordinateZ is harder to read than x, y, z.

How do you like reading the line below: divideBy(multiply(rocketmass, multiply(rocketvelocity, rocketvelocity), 2)

compared to: (mv^2)/2

Sure the former describes what the individual variables are, but immediately getting an overview or sense of what is being calculated is harder. But using short variable names doesn't mean you can only use short names. You can mix and match to optimize understanding and clarity.

    rocketKineticEnergy = (m*v^2)/2
I follow Rob Pike's advice and use long names for global and seldom used variables and functions while I use short names for locally defined variables and functions. I might also use short names for key concepts frequently used. If your key domain is geomtry then nobody will have problems understanding in context what: x, y, w, h, dx, dy etc means. You don't have to write XCoordinate, YCoordinate, Width, Height, DeltaX, DeltaY.
Maybe if you read my post, you'd see that I specifically mention "variable names of proper length that reflect their semantics (not necessarily long)," so I don't really appreciate being called a "long variable fanatic." I'm asking what the advantage is outside of loop variables which is everyone's favorite example because they can't find any better. Also, outside of geometry or other mathematics fields as most apps are not in that field. I find variable names that are actual, non-abbreviated words easier to read. What are you going to tell me next, that I don't know what's easier for me to read? Oh wait, you just did. Thank you for your condescending manner.
> I follow Rob Pike's advice

... of course you do.

What code are you reading that variables have scope covering millions of lines of code? That's appalling.

As long as variable scopes are kept tightly controlled, length of the overall code-base is irrelevant. Avoid "spooky action at a distance" and you never have to care that there's a variable named s instead of string_for_truncation_html_aware 500,000 lines away from the function in front of you.

When did you see a local context two millions lines long?

I never seen a code with long local variable names that was not awful.

And it is not about typing. Long names harm reading in the first place.