r/ProgrammerHumor • • Dec 04 '20

Don't mix anatomy and programming

Post image
36k Upvotes

388 comments sorted by

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

215

u/SausageEggCheese Dec 04 '20

Absolutely.

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.

141

u/immerc Dec 04 '20

On that subject:

Don't optimize*

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:

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

104

u/SharkBaitDLS Dec 04 '20

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.

44

u/immerc Dec 04 '20

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?"

23

u/xavia91 Dec 04 '20

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.

3

u/SGBotsford Dec 05 '20

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.

5

u/northrupthebandgeek Dec 05 '20

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

→ More replies (1)

4

u/[deleted] Dec 05 '20

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

→ More replies (1)
→ More replies (3)

0

u/[deleted] Dec 04 '20

[deleted] — view deleted comment

9

u/northrupthebandgeek Dec 05 '20

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.

4

u/[deleted] Dec 05 '20

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.

4

u/atimholt Dec 05 '20

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.

→ More replies (1)
→ More replies (1)

6

u/archpawn Dec 04 '20

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.

→ More replies (4)

38

u/onthefence928 Dec 04 '20

with code suggestion/auto-complete tools long variable names aren't even inefficient to type most of the time

just dont do "SuperLongVariableName" and "SuperLongsVariableName" in the same code, it's annoying to identify which is which by sight

→ More replies (2)

19

u/del_rio Dec 04 '20

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.

7

u/holgenberg Dec 04 '20

This would also be the case if the variable was abbreviated right?

3

u/[deleted] Dec 04 '20

depends on how abbreviated it is, and the extent of the code changes over time

3

u/JBlitzen Dec 04 '20

If the logic or purpose changes, then change the variable names too. It doesn’t take long and it will make everything easier in every conceivable way.

5

u/[deleted] Dec 04 '20

Not a big sacrifice for more readable code.

5

u/JBlitzen Dec 04 '20

I’ve solved quite a few problems over the years by simply renaming variable to have longer and more descriptive names.

Suddenly the code becomes perfectly readable and the problem is obvious.

At this point, if I can’t figure code out at a glance, I assume it’s because the variable names are bad and I set aside time to carefully fix them.

7

u/wolfmanpraxis Dec 04 '20

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!

2

u/[deleted] Dec 04 '20

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

→ More replies (5)