r/ProgrammerHumor • • Sep 23 '22

Refactoring "Just working solution ".

Post image
54.8k Upvotes

355 comments sorted by

View all comments

3.1k

u/TerriblyCoded Sep 23 '22

Past me: “Hahaha! I’M A GENIUS!”

Present me: “OH NO!”

313

u/omgFWTbear Sep 23 '22

I learned .. who was it, Knuth who said, “Never write code as clever as you can, because debugging is harder than coding, so …”

As many others, I received a full scholarship in my degree on that from the University of Hard Knocks, the School of Blood, Sweat, and Tears; later on, I shared this wisdom to my now-ex who started her career after me, she thought it was ridiculous to ever not be your cleverest.

As they say, you can lead a horse to water …

142

u/brimston3- Sep 23 '22

If it took you 2 weeks to figure out complex system interactions at the height of your power, it’s probably going to take you 4 weeks now to figure out why the jank solution works and how to make a non-jank fix for it.

0

u/[deleted] Sep 23 '22

[deleted]

28

u/[deleted] Sep 23 '22

[removed] — view removed comment

5

u/peoplesen Sep 23 '22

Or you second guess if your tar switches are used correctly

→ More replies (1)

107

u/derefr Sep 23 '22 edited Sep 23 '22

Put all your cleverness to the task of simplifying.

The cleverest solution is one that automates everything and hits all the edge-cases correctly but which uses no code at all, and has nothing to maintain or understand.

E.g. "the system you're describing to me as wanting to have built, has exactly the semantics of an XMPP server, just put to the use of machine-to-machine communication. Just install ejabberd, and point your machines at it using XMPP client libraries."

The "cleverness" in such cases, usually comes from realizing that your problem domain is a clean isomorphism of some other seemingly-completely-unrelated problem domain. Like a mathematician realizing that an existing proof of X also already proves Y as a corollary.

80

u/omgFWTbear Sep 23 '22

task of simplifying

I realized I could solve this one problem with a REGEX, and now I have two problems…

26

u/akatherder Sep 23 '22

I need a regex to validate email address.

(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=
?^_`{|}~-]+)*|" (?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b
\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[a-z0-9]
(?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\
[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]
|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\x01-\
x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c
\x0e-\x7f])+)\])

Well that was intuitive.

18

u/git0ffmylawnm8 Sep 23 '22

... aight imma head out chief this ain't it

11

u/[deleted] Sep 23 '22

You do know that the only sure-fire way of validating an email address is to try sending mail to it, right?

→ More replies (1)

36

u/ThePyroEagle Sep 23 '22

Using regex is rarely a simplification.

26

u/omgFWTbear Sep 23 '22

9

u/[deleted] Sep 23 '22

I use this and think it is better than regex buddy.

https://regex101.com/

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

13

u/vadiks2003 Sep 23 '22

task of simplifying is kinda tough. i always misunderstand the simplifying part. it's not the "make the job very easy and clumsy" it's "make everything short and readable as hard as you can". there really should be an example of simplifiying a code, because withotu an example, stuff might be understood wrong

atleast that's what i think, i'm not really a programmer yet

15

u/burnalicious111 Sep 23 '22

Short does not mean readable. Common point of confusion.

You can shorten by simplifying, which does help with readability. But you can also shorten by making more implicit instead of explicit, and that hurts readability (which the writer often forgets in the moment, because in that moment the implicit is perfectly clear to them. For now.)

6

u/vadiks2003 Sep 23 '22

i know that moment haha, pythonists make the worst one liners ever to exist. i thought shortening stuff helps because the bigger the stuff looks, the harder it is to remember relations between things and the more it looks overwhelming, atleast for me

6

u/burnalicious111 Sep 23 '22

because the bigger the stuff looks, the harder it is to remember relations between things and the more it looks overwhelming, atleast for me

This tends to improve as you gain expertise, because your brain learns relevant patterns, which it can use to chunk and store the information more efficiently.

But even then, that's still about optimizing for your working memory to remember all the moving parts, and not about optimizing how easy it is to understand how it works. The former is only a part of the latter.

→ More replies (1)

7

u/theogskinnybrown Sep 23 '22

Clean Code by Bob Martin shows how code can be simplified to make maintenance much easier.

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

57

u/FVMAzalea Sep 23 '22

The actual quote is a lot nicer:

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.

And it was Brian Kernighan.

26

u/omgFWTbear Sep 23 '22

Sounds like a smart fellow who should write a book. If he did, I’d be sure to read it. And then misattribute quotes from his august peers to/from him.

5

u/BetterOffCamping Sep 23 '22

Since nobody else seems to have picked up on it, I'm impressed with the joke.

For those who didn't pick up on it and are clueless, kernigan is half of kernigan and Richie - of the people who invented C and Unix, and wrote the Bible on the subject.

18

u/[deleted] Sep 23 '22

Always write code as if the next person is a murderous psychopath who knows where you live.

Usually that next person is me and I hate myself for it.

Seriously though I'll take code that is more verbose and self documenting over "clever" code any day of the week. If you think you're being clever when you're writing something it's time to do your due diligence to make sure your solution is something that is maintainable long term esp if you aren't going to be the one on the team maintaining things.

13

u/cephles Sep 23 '22

I write code as though a complete idiot will be looking at it a few months from now (I'm the idiot).

→ More replies (1)