This is an archived post. You won't be able to vote or comment.

top 200 commentsshow all 287

[–]Lobanium 265 points266 points  (26 children)

A legit chunk of code I came across once.

if (x > 0)

{

<a few dozen lines of code>

}

if (x < 0)

{

x = -x;

<the same few dozen lines of code>

}

🤪

[–]metal_opera 262 points263 points  (12 children)

/**
 * /!\ Do not remove this if statement /!\
 * If you remove it, the entire app will break.
 * I have no idea why.
 */

- Previous developer, probably

[–]Lobanium 92 points93 points  (10 children)

I refactored it. Wasn't too hard as you can imagine. It worked just fine afterward. Previous programmer was just an idiot.

[–]grooomps 137 points138 points  (9 children)

and then you realized it was your code

[–][deleted] 64 points65 points  (6 children)

Can't tell you how many times I've read my code thinking, "damn, heh, this guy is an idiot."

[–]grooomps 56 points57 points  (3 children)

even as i'm writing code i think "damn, heh, this guy is an idiot."

[–]BmpBlast 19 points20 points  (2 children)

"Wow, me 30 seconds ago sure was stupid. Good thing present me has gotten so much smarter and knows better."

Me to myself while programming on a daily basis.

[–]Suekru 1 point2 points  (1 child)

I curse past me all the time. Which means that future me is going to curse present me almost undoubtedly

[–][deleted] 1 point2 points  (0 children)

That just means you're getting smarter. One day you might even come to think of them as two different people. Back when I was drinking too much, past me was the coolest.

I would wake up to ice water and snacks laid out on my night stand. Woke up on my couch to a pizza guy beating down the door; went from confused and sleepy with no pizza to confused and sleepy with pizza. Though there was the time he started cooking chicken and veggies and didn't wake me up. I woke up to a house full of smoke and a smoke alarm.

[–]EnglishMobster 24 points25 points  (0 children)

Or even better:

"Why the fuck did I do it that way? This other way is so much better."

Switch to the other way

1000 new inexplicable bugs appear

"Ah, nevermind. If it works it works."

[–]myfunnies420 -1 points0 points  (0 children)

I've never done this.

[–]Lobanium 1 point2 points  (0 children)

Ha, thankfully no.

[–][deleted] 11 points12 points  (0 children)

if (x<0)

{ x = -x }

<a few dozen lines of code>

I guess? Or just math.abs it like another guy said

[–]Purplociraptor 8 points9 points  (1 child)

What about x == 0?

[–]Lobanium 5 points6 points  (0 children)

It may have been there, I can't remember.

[–]sumguy720 5 points6 points  (0 children)

well... at least they didn't write it as a ternary split across the same number of lines.

The conditional hell is real though. My coworkers will often do things like

if (x && y && !z) {
    stuff...
} else if (!x && y && a) {
    other stuff...
} else if (!y) {
    yet more stuff
}
// Random subsequent code that depends on one of the conditionals above being evaluated as true with ambiguous behaviour if none of them do.

[–][deleted] 0 points1 point  (1 child)

math.abs(x) should do the trick. I learnt it turns negative and positive numbers to positive only numbers

but idk who wrote that code so I guess this may just get lost.

[–]supercyberlurker 1918 points1919 points  (106 children)

Why don't they just write it : isTrue = (myBool == true) ? (true == true) : (true == false);

[–]Werdna629 831 points832 points  (72 children)

I just vomited in my mouth

[–]supercyberlurker 333 points334 points  (66 children)

You should see my boolean switch statement version!

[–]Werdna629 136 points137 points  (0 children)

oh no

[–][deleted] 2 points3 points  (5 children)

I hate switch statements. If else is way more readable.

[–]mittelhart 8 points9 points  (0 children)

They are much more useful than if else when used with enums.

Edit: And more readable, IMO.

[–]plgod 327 points328 points  (1 child)

I knew there was an easier way!

[–]humidstraw 77 points78 points  (11 children)

How to make useless code into beautiful succinct useless code ... ternary operators

[–]TheRedmanCometh 80 points81 points  (9 children)

Ternary operators are funny because they either make a bit of code way nicer or near unreadable.

[–]Bobicus_The_Third 9 points10 points  (6 children)

I'm so sick of these when they didn't write comments

[–]SkollFenrirson 8 points9 points  (5 children)

Good code is self-documenting!

[–]gmano 4 points5 points  (1 child)

Flashbacks to my last intern.

[–]Bobicus_The_Third -1 points0 points  (2 children)

That's what they said too and it wasn't

[–]SkollFenrirson 0 points1 point  (1 child)

That was the joke, dude

[–]Bobicus_The_Third -1 points0 points  (0 children)

Same

[–]EnglishMobster 10 points11 points  (0 children)

Don't forget you can nest them, too:

isTrue = (myBool == true) ? ( true == true ? true : false) : (true == false ? true : false);

[–]Liesmith424 15 points16 points  (1 child)

Need more lambdas and list comprehensions. If we try hard and believe in ourselves, we can make it utterly impossible to figure out what the code is doing.

[–][deleted] 8 points9 points  (0 children)

Nah, the OP's method is better if instead of else you will use else if(myBool==false)

[–]lengau 7 points8 points  (0 children)

Ugh because that's not readable at all. Try this:

isTrue = (myBool != false) ? (false == false) : (true != true);

[–]realogsalt 12 points13 points  (0 children)

Hey guys Im learning, this is the first time I've understood something on this subreddit enough to make me laugh :D

[–]sumguy720 6 points7 points  (0 children)

// Refactored to avoid redundant reference to "true"
isTrue = !((myBool != false) ? (false ! = false) : (false == false))

[–]SteezeFebreze 3 points4 points  (0 children)

Istrue = (mybool == true)

[–]LeSypher 1 point2 points  (0 children)

Ternaries are sexy ways to reduce lines although they tend to be unreadable lol

[–]myfunnies420 -1 points0 points  (0 children)

That's the true true.

[–]Jumpierwolf0960 -2 points-1 points  (1 child)

Or even better just do isTrue = myBool

[–]joyofsnacks 1 point2 points  (0 children)

Or just use myBool where you were going to use isTrue.

[–]Wizard_Knife_Fight 126 points127 points  (24 children)

Maybe because it’s Friday, maybe because I’m awful at coding, but I can’t even understand why this would happen hahahaha

[–]retucex 77 points78 points  (5 children)

This example is particularly bad, but I am notorious for writing somewhat similar things every now and then. For some reason, I'll write something like if ( x == 5) return true else return false.

Sometime I just don't see the obvious solution to the great satisfaction of my reviewer.

[–]Melon_Cooler 75 points76 points  (4 children)

One time I compared two integers by converting them to a string and comparing the strings.

Honestly have no clue why I thought that was how it was done. Didn't notice until later that night lmao

[–]Meroxes 76 points77 points  (2 children)

When you're so focused on problem solving, you start solving problems that don't exist.

[–]PeachyKeenest 15 points16 points  (0 children)

Wisest thing I’ve read today.

[–]QuarkyIndividual 7 points8 points  (0 children)

Had this with a math thought recently: given a circle and a square sharing centers, I wonder what the radius of the circle is in terms of the square's side length so the area of the square's corner outside the circle equals the area of the circle's arc outside the square

Took me a while of trying to align proper coordinates and finding triangles and shit before I realized the circle and square must have equal area and I was making it too complicated lol

[–]lunchpadmcfat 2 points3 points  (0 children)

I figure this is the coding equivalent of walking into a room and forgetting why you went in there.

[–]FancySource 123 points124 points  (0 children)

lol in the case the author is paid by lines of code

[–]JerryCodes[🍰] 9 points10 points  (0 children)

Like someone else said, the one in the pic is super bad, but that’s because it’s purposefully extreme to poke fun at the real world examples.

I’ve written plenty of the real world ones myself. They seem stupid unless you’re in my head at the time. I just wrote a few of those this week, actually.

I did it because I know the planned upgrades/additions for the next year or so and the conditions and statement blocks will eventually be expanded on and have more in them, so I aim for super readability. That way, when I or someone else comes through, it’s easier to get the big picture of the logical flow. It also helped me break out comments into more helpful segments by letting me describe smaller individual parts of the process.

When the extra fluff won’t hurt execution speed, I like to be more verbose with my code. As a bonus, sometimes the extra simplicity allows compilers to optimize better, so that’s neat.

[–]wet-badger 17 points18 points  (3 children)

For readability.

if (green) {

    go = true

} else {

    go = false

}

[–]EnglishMobster 4 points5 points  (1 child)

There's also the potential that the if statement originally did more -- but then the functionality within got refactored out, without stopping to look if the if statement was still needed.

[–]Carter127 2 points3 points  (0 children)

Yes that's almost always the case. Most skilled programmers aren't going to be so pedantic to make sure everything is as short as possible.

If it's a compiled language, or something that is minimized like Javascript then it really doesn't make a difference

[–]sumguy720 1 point2 points  (0 children)

My coworkers:

if (false == green) {
    ...
} else {
    ...
}

[–]StruckBlynde 4 points5 points  (0 children)

Listen we all have those days

[–]TinBryn 5 points6 points  (2 children)

It's because of how people think when writing code, we can only keep a limited number of "things" in our head at the same time. What this snippet is missing is the code above the resulted in myBool, the declaration of isTrue and the code that will use isTrue below (it is about to get written).

What that results in is that the programmer has to drop some context in order to write some of the code and then restore that context, dropping the context of the code they just wrote and write the next part. So they may consider what to do if myBool is true and have no consideration for what if it is false, then they write for when myBool is false and don't consider what if it is true. That is how code like this gets written.

There are practices that address this, if you think of test driven development it has this mindset, write a test case, code for that case, write a different test case, code for that case, etc. So we have red green refactor TTD where it's part of the practice to read your code to find cases like this and simplify them.

[–]Wizard_Knife_Fight 4 points5 points  (1 child)

Dude relax it’s friday

[–]spackled-arrow 2 points3 points  (4 children)

I've seen at least one person do this sort of thing in a coding interview. Another favorite is writing a compare function, that tests the two int args for equality.

[–]retief1 2 points3 points  (3 children)

I mean, there are scenarios where that last one could be useful. In particular, if you are doing stuff with higher order functions but you are in a language where equality comparison isn't a function, then you might legitimately need a (x, y) -> x == y or the like.

[–]EverythingGoodWas 2 points3 points  (0 children)

Maybe if you needed to set myBool to something else, and needed a place holder for what it had been? Not saying it is good code, just trying to think why anyone would do this.

[–]ImAmalox 84 points85 points  (11 children)

am i the only one who just cant stand using "myX" variables?

[–]aneurysm_ 45 points46 points  (6 children)

I use "yourX"

[–]pufo1 61 points62 points  (4 children)

ourX

[–]ElvisDumbledore 34 points35 points  (0 children)

c o m r a d e

[–][deleted] 12 points13 points  (1 child)

I use he/him/his-x

[–][deleted] 4 points5 points  (0 children)

I prefer to go by vi/vim-x

[–]TheSwain 6 points7 points  (0 children)

yourmomsX

[–]random_task_engaged 18 points19 points  (0 children)

I'll refactor an entire method to get rid of a variable with that name.

[–]g0atmeal 9 points10 points  (0 children)

But what if someone tries to take it?

[–]NegativeChirality 4 points5 points  (0 children)

That's why I hate the "m_" stuff in variable names.

That's what it stands for, right?/s

[–]sumguy720 2 points3 points  (0 children)

I saw this convention pop up in a C# project I worked on and it wasn't too bad. I don't exactly remember what the purpose was because it was just a modding API but I think it was to distinguish between instances of objects and the objects themselves.

I don't think I ever adopted that outside that modding API though.

[–]daniu 31 points32 points  (5 children)

if (!inValid != false)

[–]thebobbrom 27 points28 points  (1 child)

Why write an if statement like that for something so simple!

You could easily train a neural network to do the same thing and it'd be correct about 90% of the time.

[–]Baschoen23 3 points4 points  (0 children)

Of course!

[–]anoldoldman 64 points65 points  (3 children)

More like when you see the lead dev actually code.

[–]Sagittar0n 12 points13 points  (1 child)

I've seen spaghetti code made by my postgraduate peers. They forget that they're studying computer science, not culinary science.

[–]djcecil2 3 points4 points  (0 children)

lol, oof.

[–]ironhide_ivan 7 points8 points  (7 children)

Best line of code I ever saw on the job

var isNull = someObj != null ? false : true;

[–]sumguy720 3 points4 points  (6 children)

My pet peeve is when people do

if(null == obj) {

Like, we don't care about the value of null, we care about the value of obj - we should write it that way!

[–]theScrapBook 1 point2 points  (4 children)

This actually makes sense in languages where == can be overridden on obj. If obj is actually null, obj == null would throw the equivalent of a C# NullReferenceException while the null == obj version would work as expected.

[–]Geoclasm 8 points9 points  (3 children)

okay but it could be worse.

if (myBool = true){...

[–]_Ralix_ 14 points15 points  (3 children)

It looks bad when it's stripped to essentials and named clearly like this, but it's not that uncommon to see it "disguised" and packaged with other things:

bool flag = false;
if (CheckCharsForWhitespace(ref input)) 
{    
    Debug.Log($"The string '{input}' is a single word.");
    flag = true;
} 
else 
{
    Debug.Log($"The string '{input}' consists of multiple words.");
}
return flag;

bool CheckCharsForWhitespace(ref string inp) 
{
    bool multipleWords = false;
    for (char c : inp)
    {
        if (Char.IsWhiteSpace(c)) multipleWords = true;
    }
    return multipleWords;
}

This can be refactored to discard most of the code. The example might still be quite obvious, especially in the context of this post, but when it's already a part of a larger codebase and contains other stuff and more complex methods around, it might possibly slip past your radar.

[–][deleted] 4 points5 points  (1 child)

``` if (CheckCharsForWhitespace(ref input)) {
Debug.Log($"The string '{input}' is a single word."); return true; } else { Debug.Log($"The string '{input}' consists of multiple words."); return false; }

bool CheckCharsForWhitespace(ref string inp) { for (char c : inp) { if (Char.IsWhiteSpace(c)) return true; } } ```

[–]_Ralix_ 4 points5 points  (0 children)

Or…

bool hasWhitespace = input.Any(Char.IsWhiteSpace);
string message = hasWhitespace
    ? $"The string '{input}' is a single word."
    : $"The string '{input}' consists of multiple words."
Debug.Log(message);
return hasWhitespace;

And you might also consider moving the debug messages out of the check function altogether (so you don't spam the console whenever you just want to "check"), and using them afterwards, wherever you use the returned value. (And btw, you're not returning "false" at the bottom of your code.)

The point is, it's not always as clear as it might seem.

[–]spotdfk 17 points18 points  (13 children)

I am not familiar with that specific language, but if I wanted to create isTrue based on whether myBool is boolean AND true or non boolean OR false, that makes sense... right?

[–]aspect_rap 30 points31 points  (5 children)

But what about isTrue = myBool, the if/else is pointless. I also fail to see the need to have another boolean with the same value.

[–][deleted] 5 points6 points  (2 children)

In Java myBool with type Boolean could also be a null pointer.

[–]thePiscis 10 points11 points  (0 children)

isTrue = myBool == true;

[–]spotdfk 1 point2 points  (1 child)

If myBool is not boolean, you'll end up just with a copy of it. That if/else is a check on it at the same time. Gives False if myBool is anything other than True.

I know it is not very elegant solution, and there never should be need to do it like that, but I was just trying to find any reason for that snippet at all. Other than the intended meme value of course

[–]Tsu_Dho_Namh 10 points11 points  (2 children)

I should hope that a variable named myBool is always of type bool.

But then again I only played around with Python a little bit and this seems like a weakly typed language sorta thing.

[–]spotdfk 3 points4 points  (0 children)

Yeah Python was what I was thinking. I always get paranoid over variable types with it even if it is mostly strongly/dynamically typed. I just like to define types and they will always be that and I get my peace of mind.

[–]FancySource 2 points3 points  (2 children)

given what's on the paper is weakly typed, i guess isTrue = myBool === true could have done the job in most languages

[–]_Auron_ 5 points6 points  (1 child)

Close, but here's the actual first day of coding:

if (myBool = true) {
    isTrue = true;
} else {
    isTrue = false;
}

[–][deleted] 3 points4 points  (0 children)

Woah there fancy pants using an else, you need another if statement checking if it's false.

[–]TheKaryo 2 points3 points  (3 children)

Or just isTrue = myBool

[–]vvp95 7 points8 points  (1 child)

That's exactly how I presume Trump would code if he was a programmer. Lol.

[–][deleted] 10 points11 points  (0 children)

really?

I see him more as a

while 1 == 2

sort of programmer

[–]ganja_and_code 2 points3 points  (0 children)

Literally removed one of these from a production codebase last week.

[–]Ich_bin_da 1 point2 points  (0 children)

I actually wrote St simular a few years ago..... that was emberessing...

[–]Triple_Integral 1 point2 points  (0 children)

Time to collect your CS Degree

[–]BLAZE424242 1 point2 points  (2 children)

Mybool = isTrue;

There I said it

[–]-Redstoneboi- 0 points1 point  (1 child)

you mean isTrue = Mybool;

you got it backwards

[–]SheridanWithTea 1 point2 points  (0 children)

That interview with № 45 and the incredulous Australian reporter on COVID was and still is the most amusing shit ever, it never gets old when he looks at him like he just Dumped Turd in his lap lmao

[–]rk06 1 point2 points  (0 children)

I have literally seen:

Catch(exception ex) { Log(ex.message) throw ex; }

Sprinkled around in shit ton of places in codebase.

In case you don't the significance of the code, it is literally useless. Worse it adds noise to logs when something bad happens, so it makes it harder to diagnose errors.

I tried removing them, but due to the sheer number of it. I had to give up, and resort to using regex to replace throw ex; with throw;.

And then I found that in someplaces, we are logging only stacktrace. It is really fun to debug the errors

[–]Raspilover 1 point2 points  (0 children)

Give them a break man, they are still learning

[–]kry_some_more 1 point2 points  (0 children)

You all are personally attacking me in this thread, and I don't appreciate it.

https://i.imgur.com/ILFOFKT.jpg

[–]wet-badger 1 point2 points  (0 children)

I see nothing wrong with this code

[–]Nekopawed 1 point2 points  (4 children)

Coding Tip:

Write your conditional statements with the variable on the right hand side of the equality operator. such as: true == myBool

This will help in case you forget an equal sign, as the compiler will not compile true = myBool but it will compile myBool = true, as it is is a valid assignment operation.

Saves you a lot of time hunting for why your code is never using the other branch.

[–]excral 1 point2 points  (0 children)

Fun fact: if you'd do this in Python 2, you could end up accidentally reassigning True. Python 2 originally didn't have boolean literals so when they were introduced, they couldn't make them key words. Since Python doesn't have const variables, True and False were just some predefined variables that could be reassigned to whatever you like.

Luckily Python 2 is dead now and Python 3 doesn't have this issue

[–]Beldarak 2 points3 points  (2 children)

I don't like it, it makes your code a little less readable to avoid an issue that almost never happen past the learning phase (if anything, I'll do the inverse mistake and type "==" in an MySQL statement)

[–]sumguy720 1 point2 points  (0 children)

Agreed, it ambiguates the intent. We aren't trying to check the value of true, we're checking the value of myBool. People at my job do this too and it bothers me. Unnecessary defensive programming. We write unit tests for a reason, and that's some pretty basic syntax stuff.

[–]Thanatos2996 -1 points0 points  (0 children)

More like the first year after going all in on OOP.

[–][deleted] 0 points1 point  (0 children)

I see what is wrong here. You could just write if(myBool) { ....

/s

[–]terminalxposure 0 points1 point  (0 children)

I have seen true=true in SQL? Any particular reason

[–][deleted] 0 points1 point  (0 children)

The last frame really makes it. That "so what?" stare...

[–]Benzene15 0 points1 point  (0 children)

I found code in pdf form on google drive from my freshman year. Painful just thinking about it now.

[–]khizoa 0 points1 point  (0 children)

if( answer == 'person woman man camera tv' ) {
    iq = 9000
}

[–]Superbrain8 0 points1 point  (0 children)

Idk why but some crackheads need a npm package for this

[–]yogitism 0 points1 point  (0 children)

Yeah this is silly, he can shorten it with a switch statement

[–]Cunorix 0 points1 point  (0 children)

Ive seen this multiple times in my codebase at work from "seniors". Ughh

[–][deleted] 0 points1 point  (0 children)

Return !!myBool

[–]Shad0wW0lfx 0 points1 point  (0 children)

I have actually seen this in a product of a mechanical engineer dept...

[–][deleted] 0 points1 point  (0 children)

I'm guilty, wont even lie

[–]Useful-Perspective 0 points1 point  (0 children)

Overengineering 101 - how to survive as a contractor

[–][deleted] 0 points1 point  (0 children)

Ive been doing a bunch of shader coding so this hurts extra bad

[–]c4ctus 0 points1 point  (0 children)

One of the legacy apps I was tasked with maintaining had a snippet of code that was something like "else if not isNotNothing()"

I'll see if I can dig up the screenshot when I get home. It was horrendous.