Live data from Hacker News

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

beehollander.wordpress.com

11–20 of 111 posts

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

#11

The problem was not that the variable was abbreviated. The problem was that the abbreviated variable was so similar to another abbreviated variable that was used for a similar purpose.

I would add - there's a bit of a difference between an abbreviation that just keeps something from being ungodly verbose (20 letters instead of 50), and an abbreviation that shortens something so much that the original meaning is completely lost ("Ttpfe"). This is especially true for things in context - "time of ten percent height on the falling edge" is needlessly verbose, but in context "ten_percent_falling_edge" would probably be perfectly fine. And indeed, that's really just an expanded version of "Ttpfe", which is what was used anyway.

If you shorten everything to five-letter names, it's not really that surprising that it becomes an issue - And I mean, what's the real point of abbreviations when they are so short that it makes it harder to read the code?

Edit: It's worth pointing out though - if this is really old C code it may be justifiable. Back in the olden days of C (Older then C89 at least), only the first 8 characters of a symbol actually mattered. "blahblahone" and "blahblahtwo" would resolve to the same symbol. So shortening in this way could be necessary.

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

#12
When hiring we ask for sample code from developers written specifically for the job application. We place a really high premium on readability. Occasionally we'll get code from devs that is very concise - not just abbreviated variable names, but complex long statements using ternary etc.

I'll usually ask them to resubmit code and really focus on readability and it turns out fine. But I think there might be a misconception among devs where the thought is that really compact code shows talent.

Our ideal coder is someone who cares deeply about performance and where the code is ridiculously easy to read and trace through. I mention performance, because in some situations making things more verbose can affect that.

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

#13

"Never abbreviate a variable" is a very strong statement, surely inspiring religious wars. And maybe there are edge cases: i as a loop counter, id for an integer primary key, whatever. But this example is something else entirely; "ttpfe" is honestly the worst variable name I have every seen.

Ah, good ol' TransactionTemplatePayloadFactoryExtension.

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

#18
post #7
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`?

Use context. If you can't, refactor so you can. If you can't, well, you're SOL and stuck with timeOfTenPercentHeightOnTheFallingEdge. Edit: e.g. if all of your variables are "timeOfTenPercentHeight...", cut that part out of the name. Full names can be just as bad as abbreviations - e.g. if they're all "timeOfTenPercentHeight..." then they all start to blend together.

> refactor

Yes exactly. If you factor out a class that deals exclusively with timeOfTenPercentHeight, then the variable names can be OnTheFallingEdge and OnTheOtherEdge or whatever...

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

#20

"Never abbreviate a variable" is a very strong statement, surely inspiring religious wars. And maybe there are edge cases: i as a loop counter, id for an integer primary key, whatever. But this example is something else entirely; "ttpfe" is honestly the worst variable name I have every seen.

Historically, i wasn't even an abbreviation: It was the first variable name which would be assumed to be integer by FORTRAN compilers which implicitly assigned types to variables based on name. The choice was probably further influenced by longstanding mathematical tradition, which uses i and j as indices.

(You could declare types and the compiler would respect it, leading to the old truism "GOD is REAL, unless declared INTEGER".)

(If you think that's the weirdest thing old FORTRAN did, look up the arithmetic IF statement sometime. Then, look up assigned GOTO.)

Post reply on HN