r/badcode • • Apr 02 '21

js Found this on stack overflow. My. Head. Hurts.

Post image
1.1k Upvotes

68 comments sorted by

66

u/ApoorvWatsky Apr 02 '21

My personal threshold is 3 if else statements. If it gets over that, I switch to maps/dictionaries.

77

u/paraflaxd Apr 02 '21

Yeah, I just kill myself

25

u/thexavier666 Apr 02 '21

I would have reported this comment for self harm, but the code justifies it.

15

u/da_BAT Apr 02 '21

Don’t be shy. Post your better solution.

15

u/tsteinholz Apr 02 '21

iterate through the keys of the object, if they’re null then raise the invalidEntry, otherwise setValue.

9

u/flyflagger Apr 02 '21

Yes and then setValueAndStyle with max and min

1

u/MrFluffyThing Apr 03 '21

I just did that check myself and realized that you already commented. Exactly what the entire second half of this code is trying. What happens if these values are equal though? There's no error checking for matching values.

2

u/tsteinholz Apr 03 '21

matching values act as an identity function so they do not change the output of the application one way or the other. in other words, it’s not really an error

1

u/MrFluffyThing Apr 03 '21 edited Apr 03 '21

It depends on the intended behavior though doesn't it? Yes, the language course corrects by using the else statements instead but how much do you want to bet this wasn't factored into this person's code? Changing the code by optimizing to a sorted list breaks the code technically. They only expect 3 values that are always different but what if they aren't and we sort the object values instead? If they were intended to default to one value when both items are the same value then we break it by using sort potentially.

Edit: im dumb. The values are fucking the same. It's not referencing the object that has the value but the value itself. In this case you're right. I shouldn't do anything with code after Balmer peak

1

u/paraflaxd Jan 14 '22

Yeah im a little late to the game but i would not even end up in that situation. Its not something you can just ”fix”; there is obviously a fundamental issue, not just with the code, but with the whole structure of the project.

274

u/RyanNerd shameless Apr 02 '21

I love that this has been tagged as NSFW.

125

u/ThatsAHumanPerson2 Apr 02 '21

Good trick to force people to rewrite code and not just paste it.

102

u/MurdoMaclachlan public boolean isInt(int i) { return true; } Apr 02 '21

Image Transcription: Code


    a5.style.opacity=1;
}
if(q5Value1.value==""){
    invalidEntry(q5Value1);
}
else if(q5Value2.value==""){
    invalidEntry(q5Value2);
}
else if(q5Value3.value==""){
    invalidEntry(q5Value3);
}
else if (q5Value1.value > q5Value2.value){
         if(q5Value2.value > q5Value3.value){
            setValueAndStyle(q5Value1.value , q5Value3.value);
        }
        else {
            setValueAndStyle(q5Value1.value , q5Value2.value)
        }
}
else if (q5Value2.value > q5Value1.value){
         if(q5Value1.value > q5Value3.value){
            setValueAndStyle(q5Value2.value , q5Value3.value);
        }
        else {
            setValueAndStyle(q5Value2.value , q5Value1.value)
        }
}
else if (q5Value3.value > q5Value2.value){
         if(q5Value1.value > q5Value2.value){
            setValueAndStyle(q5Value3.value , q5Value2.value);
        }
        else {
            setValueAndStyle(q5Value3.value , q5Value1.value)
        }
}

I'm a human volunteer content transcriber for Reddit and you could be too! If you'd like more information on what we do and why we do it, click here!

54

u/Franks2000inchTV Apr 02 '21

I don’t know how you guys retype this terrible code without reflexively refactoring it.

32

u/MurdoMaclachlan public boolean isInt(int i) { return true; } Apr 02 '21

I try not to think of it as code; just patterned (or sometimes not patterned) characters on a screen.

10

u/Franks2000inchTV Apr 02 '21

Excellent way to keep the madness at bay!

1

u/Huhngut Apr 02 '21

Is OCR not working good enough?

8

u/MurdoMaclachlan public boolean isInt(int i) { return true; } Apr 02 '21

OCR.

Sometimes it does pretty well with most of the characters. Formatting & indentation, however, are never preserved; partially because the OCR, while a very advanced one, is not necessarily tailored to Reddit's formatting, but also partially because OCR simply isn't yet at the point where it's reliable enough to make proper transcriptions.

39

u/Flawlesscode Apr 02 '21

good hooman

3

u/KomaedaEatsBagels Apr 02 '21

good and brave human. this code is cursed

3

u/MurdoMaclachlan public boolean isInt(int i) { return true; } Apr 02 '21

Haha, thanks! It's certainly awful.

2

u/kurbsdude Apr 03 '21

Genuine question, why do you do what you do?

6

u/MurdoMaclachlan public boolean isInt(int i) { return true; } Apr 03 '21

Primarily to help blind/otherwise visually impaired people who rely on screen readers to browse Reddit, since that technology can't read text out of images (and while OSCR can, it's very unreliable). There are also some dyslexic people who I believe rely on screen readers.

It also helps people who can't load the image due to slow internet, third-party clients, text-based browsers, etc., as well as anyone who wants to copypaste the content of the post for any reason.

Hope that helps explain!

3

u/kurbsdude Apr 06 '21

That’s so awesome!

39

u/jarfil Apr 02 '21 edited Jul 17 '23

CENSORED

3

u/Gorrlaamiii Apr 02 '21

How do you run them all at once

13

u/Biaboctocat Apr 02 '21

As in, run each check at a time, but don’t return an error until you’ve run all of them and can tell the user all the errors at the same time

5

u/Gorrlaamiii Apr 02 '21

You can just loop on them and spit out if anything errors out right

2

u/Biaboctocat Apr 02 '21

I guess it depends on the wider context, if “showing an error” actually pops up a unique dialog box every time, you may not want the user to have to close each one before they can continue.

1

u/Mfgcasa Apr 03 '21

Then you build an error message out of the list of errors that covers everything.

2

u/Biaboctocat Apr 03 '21

Exactly, that was my first suggestion ;)

49

u/Cristichi wicked Apr 02 '21

A teacher of us used to say that things like that should always be revised because solutions to most problems tend to be elegant

15

u/curious_pinguino Apr 02 '21

My teacher said:

"Juniors will write simple code, because it's all they know."

"Mid-level developers will write complex, "elegant" code, because they know how to."

"Seniors will write code as simple as the Juniors, so that if something comes up later, it's somebody else's problem".

7

u/[deleted] Apr 02 '21

The difference between a junior and a senior is documentation and comments.

31

u/Poutrator Apr 02 '21

That's a pretty dangerous adage to live by.

Mine would be "trusted solutions looks boring"

I used to insert nifty and cleverness in code. That's great at super small scale, super small time window (so perfect for school project).

Elengant solutions are for college maths mainly IMHO, it looks like most modern maths are getting incredibly huge proof (except the one that popped up last 2 years I think)

9

u/lstyls Apr 02 '21

This is the kind of thing professors say to seem smart but upon closer inspection is utterly inane.

The thing is, “elegance” is a completely subjective quality. To the college professor it probably means a solution that uses a minimal amount of code and probably leverages some trick of logic that isn’t immediately obvious, or perhaps implements an abstraction with impeccable purity.

In my opinion “elegant” code is code that is robust, extensible, and can be understood by people other than the author. In other words code that functions elegantly in production over time, rather than code which looks elegant on paper.

2

u/Cristichi wicked Apr 02 '21

Well, that's why he never told us to always reject all "inelegant" solutions, just to double check

9

u/Moldybot9411 Apr 02 '21

This is some real yanderedev shit

7

u/NakeleKantoo wicked Apr 02 '21

I was planning on sleeping tonight, but now if I ever sleep again I'll have this nightmare, thanks.

4

u/TdiParadiso Apr 02 '21

It's a question or an answer?

3

u/Glori4n Apr 02 '21

Someone teach this man &&

2

u/veedant Apr 02 '21

this guy earned himself a long and painful eternity

2

u/TechcraftHD Apr 02 '21

Meh, I've seen worse, if you don't know about min or max functions (it has logic errors of course so I hope this was a question not an answer)

2

u/KingTuxWH Apr 02 '21

Is this the question or answer?

2

u/Bubbacub Apr 02 '21

What the fuck were you searching for, to get that nonsense?

2

u/LeanderT Apr 02 '21

Those variable names really help to explain what this code is supposed to achieve. /s

1

u/undieablecat Apr 02 '21

Not even the processor can understand this (when complied down to machine code

1

u/valschermjager Apr 02 '21

paid by the line?

1

u/6138 Apr 03 '21

Could be intentional code obfuscation? To prevent reverse engineering? Just a thought.

0

u/[deleted] Apr 02 '21

[deleted]

60

u/RootOfTheGiantOak Apr 02 '21

Why not just use variables a, b, c, d ... it's no better

24

u/pier4r Apr 02 '21

How? The code would just generate debt in case if changes

65

u/Sqeaky Apr 02 '21 edited Apr 02 '21

The idiotic theory behind that the notion that kind of shit produces job security is that no one else can touch the the code. The reality is that this bullshit slows down a whole dev team and the whole team gets replaced.

25

u/prisonbird Apr 02 '21

that got me scared, maybe i should stop naming my variables something other than city names

10

u/GloGangOblock Apr 02 '21

Lmao just switch it to country names

6

u/[deleted] Apr 02 '21

WTF? The only reason why you should use city names as variables is if you're trying to store some hash with data on a specific city.

And even then, use a common hash with the city name as a key.

5

u/Mfgcasa Apr 03 '21

Almost definitely r/whoosh

3

u/someGuyyya Apr 03 '21

I thought he was being serious too :(

3

u/[deleted] Apr 02 '21

My team just got a feature working in a module reputed to be like that

Turns out to be a beautiful bit of code, well designed, well commented; the rumours were fud created by the person who used to own it under waterfall

3

u/Sqeaky Apr 03 '21

Absolute shit code is either absolute shit or pure genius written by someone better.

0

u/[deleted] Apr 02 '21

[deleted]

5

u/[deleted] Apr 02 '21

W H A T

6

u/KCGD_r Apr 02 '21

Why not just minify the code and keep the original private?

5

u/Help____________me Apr 02 '21

You can unminify pretty quickly

3

u/KCGD_r Apr 03 '21

You could also compile it with closure compiler or something else

4

u/[deleted] Apr 02 '21 edited Apr 02 '21

I would fire this person on the spot for incompetence. If some bullshit about "job security" comes up, I would designate somebody to recreate the functionality and throw all of this old tech debt bullshit away.

After I fired the idiot that wrote it.

Also, we would have never hired this idiot in the first place, because they wouldn't have even gotten past the first set of code tests. How's that for "job security"?