Live data from Hacker News

Why I'll never abbreviate a variable as long as I live

beehollander.wordpress.com

21–30 of 111 posts

Re: Why I'll never abbreviate a variable as long as I live

#21

Pwd stands for: ________? Correct, you guessed it! Once an abbreviation or acronym becomes widely used it makes sense to use it as a variable name for as long as you live.

...I though you meant password, I google it and you probably mean "print working directory" right?

Re: Why I'll never abbreviate a variable as long as I live

#22
This is common in the sciences as well, since senior professors also had the 8-character limit (from fortran [0]). And functions are also named this way as well (see lapack/blas).

Some people also hate typing out slightly longer variable names and what not. I try to emphasize that a section of code will tend to be read more times that it is written, and therefore readability is more important. It's a frustrating battle sometimes, though.

[0] Exacerbating the problem of using the wrong variable is the fact that much existing code uses implicit types...

Re: Why I'll never abbreviate a variable as long as I live

#23
post #6

If the variables had been named timeOfTenPercentHeightOnTheFallingEdge and timeOfTenPercentHeightOnTheRisingEdge it probably would have still been hard to notice that they had been swapped in that one line.

Yes, but the likelihood of mistyping them would be far less. 'f' and 'r' are right next to each other. 'Falling' and 'Rising' are a bit harder to unnoticeably fat-finger.

Re: Why I'll never abbreviate a variable as long as I live

#25
post #5

I definitely prefer more verbose names to abbreviated ones, but I'm not sure that never abbreviating a variable name is the right way to go either. Surely there's a middle ground between `Ttpfe` and `timeOfTenPercentHeightOnTheFallingEdge`?

tl;dr: use newtypes!

I like using static types to avoid these sorts of problems. Modern languages like Swift, Rust and Haskell let you make zero-overhead type wrappers around other types.

So here they could have defined `newtype RisingEdge(Float)` and `newtype FallingEdge(Float)`, and then use those types in the function parameters as appropriate.

Helps shorten function names to boot!

Re: Why I'll never abbreviate a variable as long as I live

#26
post #6

If the variables had been named timeOfTenPercentHeightOnTheFallingEdge and timeOfTenPercentHeightOnTheRisingEdge it probably would have still been hard to notice that they had been swapped in that one line.

I think the catch is that the 'f' and 'r' keys are right next to each-other, so if you accidentally type one instead of the other then you'd get the other variable by mistake.

That said, you raise a fair point - we don't know how this bug got there. If it was a simple typo like above then the verbose names would have prevented it. If it was a logic error by the programmer (For whatever reason), then you're right that the name wouldn't matter because they typed the one they intended, it just wasn't the right one to use.

Re: Why I'll never abbreviate a variable as long as I live

#27
post #10

Anyone else feel a bit uncomfortable reading the level of detail in this post?

Hope those systems are now declassified for his career's sake.

I would never publish anything that shouldn't be published. Pretty much everything here could be found in a few hours on wikipedia, except for the story aspect of it.

Re: Why I'll never abbreviate a variable as long as I live

#28

Pwd stands for: ________? Correct, you guessed it! Once an abbreviation or acronym becomes widely used it makes sense to use it as a variable name for as long as you live.

Print Working Directory? Present Working Directory? Password?

A probably worse trap is "temp". Is it temporary or temperature?

Re: Why I'll never abbreviate a variable as long as I live

#30
I was recently implementing a geometry algorithm which I looked up on quora. It was described using typical vector notation, using r,s . t,u. Since I referenced the algorithm in the comments, I decided to use these same variable names in my code.

I think this is the right choice, but my code reviewer didn't. But he didn't click on the quora link.

Why is okay for mathemeticians to abbreviate things but programmers? Is it because they deal in more abstract entities where the name is irrelevant?

Post reply on HN