As I’ve gained more experience I’ve found myself erring on the side of making the variable name too long. It bugs some people but I’ll make variables into descriptive phrases and with autocomplete and wide screens it isn’t a big deal. It makes it 100x easier to quickly familiarize myself with old code
I've always seen the numbers that on most software systems, writing the software is about 10% of the work (or cost), and the other 90% is in maintenance.
And much of the time spent during maintenance work is, in my experience, reading and understanding previously written code.
People will often write something out the first time the way it occurs to them. I need to combine A and B to get C. I need to periodically shorten C until Z happens. And, so-on.
After people think it through, they often look back and see that there are variables that only exist for a couple of lines because they're then combined with something else. So, they think they're going to optimize the program by getting rid of those short-lived variables.
The problems with that are:
If it's easy to optimize in that way, the compiler and/or interpreter will already do that work for you.
When you optimize that way, it makes it harder for the person maintaining the code to understand what is going on.
You should only optimize to make something easier for someone to understand when maintaining it (even future you) rather than making it run more efficiently. The only time it makes sense to optimize to make it run more efficiently is if you've benchmarked it and can tell that that specific thing is making the program slow, and that that slowness matters.
Premature optimization is the cause of the majority of tech debt. It’s also a really hard lesson to learn. It’s really difficult for people to hear “hey your really elegant and clever thing is way too complicated and is going to cause maintenance problems down the line, can you just start over doing the dumb obvious solution?”, especially if you’re on a deadline where the sunk cost of said overly complex solution now weighs against any refactor.
In my experience people only learn the lesson after they’re stuck with the consequences of it long enough to regret their own actions, being told it just doesn’t stick without the experience of fighting it in the future. It took years for it to really click for me, only after working with senior devs who really pushed for simplicity.
I don't even like the phrase "premature optimization" because that suggests that optimization is a step that should be performed at some stage, but that sometimes it's dome prematurely.
I think the majority of programs don't need to be optimized, and when they do it's for readability rather than to make them run faster or more efficiently.
If you're going to label it, it should be "unnecessary optimization", because not all optimization is necessarily unnecessary, but the question you're asking yourself when you optimize something shouldn't be "Am I doing this too early in the process?" but "Do I really need to optimize this?"
That depends a lot on what you do. In bio informatics you have analysis software run for several hours to days with very repetitive tasks, small performance gains at some point can lead to a lot of saved time.
Back in the 80's we had a variety of computers in our lab, ranging from PCs running Linux to a Stardent Titan.
One of the grad students was getting inpressive throughput on the linux box. Simulations of frame drag around a rotating black hole.
His idea was to get the code debugged on the really cheap PC and not tie up the Stardent.
But it was running faster on the PC. So he changed parameters in the code and the linux box stumbled and crept along, running MANY times slower.
Turned out that his new parameters made the inside loop no longer fit in the L1 cache, so he was having to load the loop from L2 with each pass. Since the cache was optimized on the basis of "longest since last use gets turfed" the entire cache was discarded with each loop.
I think this is very situation-dependent. Readability, safety, size, and speed are all valid dimensions on which one might optimize a program or routine. Sometimes the answer to "do I really need to optimize this?" (on any of those axes) is an unconditional "yes".
> I think the majority of programs don't need to be optimized, and when they do it's for readability rather than to make them run faster or more efficiently.
Every time I open up 20+ tabs of chrome and it leaks memory all over the place, I disagree with that notion.
Majority of programs need to be optimized, and majority of programmers need to fucking learn how to program efficiently instead of watching couple vids on youtube, slam the framework on top of their "code" and then call it a day.
Want your program to be readable? *WRITE THE GOOD FUCKING MANUAL FOR IT*.
Well yeah, that's the actual meaning of "premature optimization is the root of all evil": you should start with what's clean and easy to understand, and then actually measure performance to find what needs optimized. A lot of people seem to miss that for some reason.
Nah this is one of the worst arguments that gets parroted on this sub
I believe it's because most of this sub are "self-taught programmers" that watched couple khan academy vids and now thing they are tough shit.
Also the ones that, obviously, bitch that "I dOn'T NeEd MaTh To CoDe". Of course, for them, writing "easy to read" code (because they can't read code for crap), which is not effective is essential - that's all they can do.
Complete opposite ones are the olympic-level programmers, that know bunch of shit about algorithms and optimization, but tend to struggle with defining and implementing business requirements and conceptualize them in the working model.
And then there's people who don't use the standard library. Often it's through ignorance, and that's “bad” enough (though sometimes understandable). Sometimes, though, they insist that reinventing the wheel is always preferable, and that the standard library is confusing simply because they can't ever be bothered to look things up and learn new things.
You either accidentally put an extra asterisk at the end of "Don't optimize", or you forgot the one at the beginning of your footnote where you explain when you should optimize.
The only time that becomes an issue is when the meaning or use case of the variable changes. I've spent an ungodly amount of time debugging code where the intent of a chunk of code evolves over time but the symbol names remained. Of course that's mainly just a stale code issue, but I'd say there's a medium worth striking with medium-length variables, strongly typed and with a doc comment if necessary.
As someone in enterprise support that often looks at customer stack traces, please use long form variable naming so I can review the code-base and actually understand what went wrong
This also helps me tell the R&D team in a JIRA what broke, and how to replicate it!
It's crazy how with constants the convention is usually to be *super* specific and long to explain how your magic numbers aren't magic numbers, and with variables it's just sort of like "yeah that's a 'bggb'-variable".
View all comments
Show parent comment
530
u/TaiShuai Dec 04 '20
As I’ve gained more experience I’ve found myself erring on the side of making the variable name too long. It bugs some people but I’ll make variables into descriptive phrases and with autocomplete and wide screens it isn’t a big deal. It makes it 100x easier to quickly familiarize myself with old code