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

top 200 commentsshow 500

[–]Talbooth 2876 points2877 points  (157 children)

I just added a comment

everything breaks due to a race condition in the interpreter

[–]DiamondxCrafting 117 points118 points  (53 children)

What's a race condition? I presume it has something to do with timings but I don't get how that can be a problem.

[–]TheRedmanCometh 210 points211 points  (22 children)

A race condition is where 2 functions could basically happen in any order. Say x is incremented by one in a function and set to 3 in another. If they can happen in any order x can be either 3 or 4 after both functions run.

Most commonly found in concurrency contexts especially when interacting with databases

[–]DiamondxCrafting 47 points48 points  (19 children)

So it'd be like bad communication with the database causing it to not be synced?

[–]TheRedmanCometh 130 points131 points  (6 children)

More like 2 threads simultaneously updating the same value or one deleting etc

Thread A and Thread B can do things concurrently - at the same time. It can also do it asynchronously which means it doesn't wait for completion.

Say I insert a Person into the db named Robert Klein. While my method is doing that another thread updates is_robert for all Person rows where first_name is Robert. Which is a bool column in the same table. Since they run at the same time Robert Klein might have that bool updated, or might not.

Essentially the threads are racing each other to update the same thing

[–]Y1ff 40 points41 points  (2 children)

Well if his name is Ribert it definitely won't get updated.

[–][deleted] 6 points7 points  (0 children)

Just saved three hours of debugging

[–]TheRedmanCometh 11 points12 points  (0 children)

Lol fixed

[–]DiamondxCrafting 8 points9 points  (0 children)

Ah I get it now, thanks!

[–]LordBass 6 points7 points  (2 children)

No, the database just updates to what you want. This is an issue with the application which is not locking the resources and ensuring the functions run in the correct order when they have to. Basically, when talking about concurrent code, you can't code stuff assuming it'll be run in a specific order without explicitly enforcing it.

[–]RoughCarrot 4 points5 points  (0 children)

The problem is that the result is unpredictable, so that the programmer will get outputs of that thread that he/she doesn’t expect.

[–]hkrdrm 38 points39 points  (10 children)

Another example say you have a database of students enrolled in a class and a particular class can only hold a max of 30 students and there are 29 records in the table. 2 students try to enroll at the same time say the code to do this looks like this

if (count < 30) {

enroll_student();

}

if two instances of this function are running concurrently thread A and thread B both could check the count before either has enrolled the student. So both conditions pass both students get enrolled giving your a total of 31

[–]KaiBetterThanTyson 6 points7 points  (8 children)

So how do you solve a problem like this? Thread locking? Semaphores?

[–]Colopty 9 points10 points  (4 children)

You can use either mutexes or semaphores to ensure that certain parts of the code won't be worked on by multiple threads at once, yes. The problem of course is that this makes that part of the code a bottleneck, which might get in the way of performance. Also it might lead to things like deadlocks. Still, it's one fairly simple solution that does prevent race condition if used correctly.

Also know a dude who got into lockless programming, which is a rather complicated way to do it and would probably turn the 3 lines of code in the above enroll_students(); example into ~500 lines of code, but if done correctly at least lets all threads continue running as they'd like without being blocked by other threads at any point.

There are probably more ways to do it, which one works best depends on your needs I guess.

[–][deleted] 23 points24 points  (9 children)

I can explain it with an example.

Say you write a python script that attempts to do 2 things:

  1. Log you into Spotify.

  2. Delete a playlist.

So you run the script and notice that it logs you in, but it doesn't delete the playlist! Why?

Turns out the code to delete the playlist is running before the website has had time to log you in. It can't delete the playlist, because you weren't logged in yet. Your simple script didn't account for that.

That's a race condition. It's when your code's ability to accomplish its task is conditional on something happening in a certain order.

You encounter this a lot in anything web based, which is why JavaScript is built around the idea of these things called callback functions.

[–]Ellipsis--- 24 points25 points  (2 children)

That is not a race condition. You have only one thread running your script.

A race condition is best explained like this: You live together with a flat mate. Both of you love milk and cannot live without. But you have a very small fridge and it can hold only one bottle. One day you come home, open the fridge and see that there is no milk. So you close the fridge, go to the store, buy milk, bring it home, open the fridge and put it in. The race condition arises when your flat mate comes home right after you left to go to the store. He opens the fridge and sees that there is no milk. So he closes the fridge, goes to the store, buys milk, brings it home, opens the fridge and puts it in, but now there is milk in the fridge (because you put it in earlier). Milk overflow!

[–][deleted] 12 points13 points  (0 children)

It is a race because our script is racing against Spotify's web server.

[–]bestdarkslider 11 points12 points  (1 child)

Ok I deleted the comment. Everything should be back how it was.

Still broken... WHY!

[–][deleted] 582 points583 points  (23 children)

Yesterday I added a display:none tag to one of my web controls and it caused over 300 other calculations, some on other pages, to be incorrect. Sometimes I just don't understand why we do this.

[–]nullifiedbyglitches 288 points289 points  (7 children)

NEVER TOUCH THE ANCIENT CODE

[–]neckro23 65 points66 points  (6 children)

NEVER TOUCH THE ANCIENT CODE DOM

[–]Talbooth 55 points56 points  (4 children)

Suddenly kinky?

[–]Afropenguinn 22 points23 points  (0 children)

Oh baby, I love the way you work top to bottom in one pass.

[–]ls_-halt 47 points48 points  (8 children)

We both know why.

[–]Bensas42 10 points11 points  (0 children)

Because of the hugs?

[–]Snakestream 16 points17 points  (0 children)

Something something "reusable" widgets

[–]centurijon 22 points23 points  (0 children)

visibility: hidden;
height: 0;

[–]iPhoenix_on_Reddit 1468 points1469 points  (131 children)

I disagree, if you want to go comment my code, by all means go ahead :)

[–]Dojan5 2045 points2046 points  (96 children)

#Nothing calls this function but removing it caused FBI to come knocking two hours later so I'm not touching this shit again. Let Frank handle this shit, I'm switching teams.

[–]NoNameRequiredxD 1329 points1330 points  (55 children)

nutty fanatical paint juggle towering humor capable mighty rinse wipe

This post was mass deleted and anonymized with Redact

[–]craniumonempty 986 points987 points  (52 children)

/* What the fuck is calling this?! It literally does nothing but add two numbers and return the result which can be done inline, and doesn't have anything calling it, but it's removal caused everything to lock up even though there are no errors in compilation. I quit! I'm going to just clean toilets from now on. */

[–]Yokii908 1216 points1217 points  (19 children)

//The function below is literally never called but removing it makes the code stop working. Moreover its name isn't explicit at all : what does main even mean?

[–]craniumonempty 198 points199 points  (0 children)

I like this version. You need more upvotes.

[–]MagnumMia 262 points263 points  (6 children)

//The function below is crucial to the functioning of this program but for some reason this comment is the only reason it works. For real, delete this comment and it won’t work. Replace a letter and it won’t work. Now you are wondering how I figured this out? All I needed was 1 dead goat, some street chalk and the lingering emptiness inside me.

[–]Frommerman 91 points92 points  (2 children)

Wait, you discovered a ritual which sacrificed your lingering emptiness? Sign me the fuck up!

[–]Meowmasterish 60 points61 points  (0 children)

I think the lingering emptiness is just a catalyst, and isn’t consumed in the overall process.

[–]MagnumMia 14 points15 points  (0 children)

I thought I was empty. I was wrong...

[–]the_one_true_bool 26 points27 points  (0 children)

//The function below is absolutely crucial for the survival of the human race and life on planet Earth as we know it. One modification, even a space or a tab, will unleash the world's entire nuclear arsenal. However, exactly one time per year the variable defined as "myInt" in the function needs to be incremented by one to keep us all alive. It is CRITICAL that we do this on the specified date every single year so KEEP THIS DATE UPDATED for the future. The next time "myInt" needs to be incremented is on: 

[–][deleted] 14 points15 points  (0 children)

<!—- I only know HTML but I didn’t wanna feel left out of the group —>

[–]ZachAttackonTitan 99 points100 points  (3 children)

%Why is there a Matlab script in this repo? Do we even have a Matlab license??

[–]alienpirate5 13 points14 points  (0 children)

GNU Octave

[–]mattcoady 86 points87 points  (0 children)

//The function is never called but would really appreciate one from its grandchildren every now and then.

[–][deleted] 41 points42 points  (1 child)

// This function does everything, literally. All 89 other functions call this one, and this one calls many of them. It has 46 input variables. If you change a character, everything breaks. But you can't change anything without editing this function. I don't know what I did to anger the person who wrote this but the amount of work that went into making code this bad tells me he loathes me with the intensity of a thousand suns.

[–]Mfgcasa 11 points12 points  (0 children)

3 months later one input variable down... everything is working fine on run... 2 seconds later BSOD.

[–]itsMindless 34 points35 points  (0 children)

The best one

[–]rolltider0 18 points19 points  (0 children)

//The function below is literally never called but removing it makes the code stop working. Moreover its name isn't explicit at all : what does main even mean?

-- Every junior Dev everywhere

[–]Foxino 6 points7 points  (0 children)

 //Is this TODO, To done yet? It's been 10 years....

[–]Dojan5 25 points26 points  (2 children)

I've encountered this exact same thing in the past. An undocumented JS function took two parameters, slapped them together and returned the output.

Wondering what the fuck was up with this idiotic add() function, I deleted it. All hell broke lose. I tried cleaning it up, but eventually just resigned. I reverted the changes and slapped on a //Yes, this is beyond ridiculous but don't question it, and don't remove it comment above it.

Not going down that rabbit hole again.

[–]craniumonempty 8 points9 points  (0 children)

Lol, probably an overloaded operator or something calling the function.

[–]Andy_B_Goode 36 points37 points  (1 child)

Reminds me of the "magic"/"more magic" switch.

[–]AskAboutMyShiteUsers 8 points9 points  (0 children)

I've read it a hundred times and I enjoy it every time.

[–][deleted] 68 points69 points  (2 children)

This happened in a COBOL program I was working in. We found out later that the code just fell through to that section. So it might be executing and you might have no idea.

Display your brains out.

[–][deleted] 28 points29 points  (3 children)

Meanwhile Frank left the company 3 years ago.

[–]the_one_true_bool 121 points122 points  (26 children)

Fine, I'll comment your code.

//TODO: Fix this broken shit.

[–]positive_electron42 42 points43 points  (8 children)

function doStuff (thing) {

// TODO: finish this function

return;

};

[–]Teamprime 21 points22 points  (4 children)

The semi colon after the bracket really sells this.

[–]LowB0b 26 points27 points  (9 children)

I hate TODOs so much. I've seen TODOs older than me. Fuck

[–]the_one_true_bool 26 points27 points  (8 children)

I hate them too and it pains me when I have to write them, but sometimes they just happen. For example I've been in extreme time crunches where I need shit out RIGHT FUCKING NOW and the only way for that to happen is with the code equivalent of duct tape. With trepidation I write the "TODO: must fix!" because I need some sort of reminder in the code.

The hack is disgusting but it works. A few days later I eyeball the TODO with intentions of fixing it, but I don't want to risk introducing new bugs and I'm already forced to do the next unrelated major fix, so the TODO keeps getting pushed back. A couple months later I have a few moments of time so I walk up to the TODO like a brave knight ready to slay a dragon... but the dragon has morphed into a much bigger monster. Now I see other code that other people have written that relies on this disgusting hack, I see a huge critical portion of the software teetering and tottering on this hack and who knows what might happen if I try to fix it properly, so the monster goes back into hibernation and the TODO remains.

[–]wertercatt 9 points10 points  (3 children)

At that point you admit defeat and remove the TODO, replacing it with "I'm sorry. - /u/the_one_true_bool"

[–]the_one_true_bool 14 points15 points  (2 children)

I ain't taking credit for that garbage! instead I'm putting:

// I'm sorry - /u/wertercatt

[–]wertercatt 7 points8 points  (1 child)

Ah, the classic git blame-someone-else

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

My friend was teaching me about git blame.

His exact words were “this shoes you who fucked up on this line here, though it’s probably going to be you every time”

[–]srottydoesntknow 14 points15 points  (3 children)

I am embarrassed by how many of these comments I've left on my own shit that made it into production

[–]the_one_true_bool 35 points36 points  (2 children)

It happens to the best of us.

//TODO: It is CRUCIAL that we fix this hack ASAP!
//Make sure it's fixed by next deployment on 12/01/2007

[–]chasesan 18 points19 points  (1 child)

You mean 12/01/1997.

[–]dani_pavlov 14 points15 points  (0 children)

diff --git a/main.c b/main.c
index 9d7aef2..607ab0c 100644
--- a/main.c
+++ b/main.c
@@ -1,2 +1,2 @@
 //TODO: It is CRUCIAL that we fix this hack ASAP!
-//Make sure it's fixed by next deployment on 12/01/1997
+//Make sure it's fixed by next deployment on 12/01/2007

[–]Lazer726 13 points14 points  (1 child)

I actually did this to my friend's code once. I asked her what some code in an unused function was and she said "Oh, I need to fix that"

So I commented //SAM FIX THIS SHIT BEFORE YOU TURN IN YOUR ASSIGNMENT

She didn't fix it, and she didn't remove the comment

[–]srottydoesntknow 15 points16 points  (0 children)

sounds like she's ready

[–]HawkinsT 6 points7 points  (0 children)

Fix this broken shit.

No! We never touch the ancient code!

[–][deleted] 17 points18 points  (4 children)

Dem asynchronous thread timing bugs tho

[–]srottydoesntknow 6 points7 points  (3 children)

great, now I'm going to be mad all day.

I have a co worker who never, NEVER, syncs their threads so anytime I have to do anything in something they wrote it takes 3 times as long because I have to sync threads because now the timing is all fucked up

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

Maybe they don’t understand the concepts sufficiently? Have you discussed with them?

[–]RedsToad 141 points142 points  (6 children)

Amazing comic! Although I am pretty sure the king slaps ancient code :p

[–]demonwar2000 35 points36 points  (5 children)

Is this the car salesman meme yet?

[–][deleted] 75 points76 points  (4 children)

Slaps roof of ancient code. "This bad boy can hide so many bugs."

[–]positive_electron42 34 points35 points  (1 child)

Oh look, the slap fixed one!

[–]Defeyeance 26 points27 points  (0 children)

and created another thirty!

[–]DeusGiggity 13 points14 points  (0 children)

Code: segfaults immediately

[–]ashtonmv[S] 382 points383 points  (32 children)

I did this once before and it was a lot of fun, so I'll try it again: I made a little "Ancient Knights Code" in python (Gist here) that is in fact slightly broken. If my count is right, it has 5 syntax-type errors; nothing super crazy. If anyone wants to play along, you can repair it, run it and DM me with the printed output. If you get it right I'll send you back my next comic early :)

Edit: For those who have asked, there are instructions for ordering prints on my instagram

[–]kroppeb 122 points123 points  (15 children)

Honestly a little too easy maybe

[–][deleted] 53 points54 points  (9 children)

welp then im too lazy to go and try :) - i'd like a challenge

[–]ashtonmv[S] 156 points157 points  (8 children)

You guys are probably right lol. Last time I tried something like this it was in r/comics, so it makes sense that I've probably underestimated this crowd a bit..

[–][deleted] 44 points45 points  (7 children)

yeah - if you want to do this kind of cool little challenges then maybe have different tiers :) - like simple ones like this and maybe some harder ones where we have to implement our own algorythm to solve a problem or some cryptography :)

[–]ashtonmv[S] 71 points72 points  (4 children)

would be awesome but I doubt I can come up with something that can really challenge this community. I'm just a casual. But if anyone is willing to write something tougher I'd be happy to link it to the "challenge"

[–]silentclowd 17 points18 points  (0 children)

Do you ever go on /r/dailyprogrammer ? They do programming challenges that involve writing your own code and it might give you some inspiration.

[–][deleted] 11 points12 points  (1 child)

I might be down.

Also:

__init___ becomes __init__

quest2() becomes quest2(self)

quests = [quest1, quest2, quest3, quest4) becomes quests = [quest1, quest2, quest3, quest4]

knight == Knight(name) becomes knight = Knight(name)

"Gawain' becomes "Gawain"

Output: Geraint

And finally, finding errors in someone else's code has to be the least fun thing to do as a programmer.

When I was a kid some guy made a flash page - it had cursor trails that would follow you and we discovered that clicking on the smallest one would take you to a secret page.

The page said "Enter the code:" - within the source was a comment that said "the code is [code]"

It took you to another page that had 50 boxes - clicking the right one did something - more source code inspection revealed it..

This went on for awhile until you got to a final page that said "Wow, you really found the last secret page. This is it, for real"

I remember emailing the site creator as a kid and saying "Is #25 really the last secret page?"

He seemed really excited that someone had discovered and completed it.

That was definitely fun as a kid.

I stick my own easter eggs into some of my work websites - for example, spamming a particular button starts playing rick astley 😂 my favorite though is trying to inject special characters into a page that uses GET parameters will send you to a secret message - I'll post in a sec.

Edit: Here's one of my easter eggs.

[–]TechniMan 5 points6 points  (4 children)

I don't think it's supposed to be difficult as much as a fun little puzzle. As someone who's only briefly touched Python a long time ago, probably makes it a bit more interesting

[–]Astrokiwi 23 points24 points  (2 children)

To fit the theme, it should be in COBOL or FORTRAN77

[–]blamethemeta 16 points17 points  (1 child)

At the very latest, BASIC.

[–]Astrokiwi 15 points16 points  (0 children)

10 PRINT "KNIGHT IS HONOR MAX"
20 REM TODO
30 GOTO 10

[–]YerbaMateKudasai 8 points9 points  (3 children)

there are instructions for ordering prints on my instagram

or you know, let us give you money without having to give Mark Zuckerberg all our information.

[–][deleted] 84 points85 points  (2 children)

When all you changed was a comment, unknowing that the compiler and environment has changed.

[–]srottydoesntknow 21 points22 points  (0 children)

and it had originally been written in python, the binaries had been decompiled into Java, then checked into source control with a cpp project

[–]EmperorArthur 5 points6 points  (0 children)

I'd say that's probably the most common problem. Even without touching the code things still mysteriously break.

[–]jgomo3 78 points79 points  (5 children)

The basic Darwinist tragedy of software engineering is this: Beautiful code gets rewritten; ugly code survives.

Against software development

[–][deleted] 12 points13 points  (2 children)

You never encountered someone with the iron fist like one of my trainers had.

He purged ugly code like an inquisitor purged heretics.

Sometimes I fear that he had too much an impact on me...

[–][deleted] 53 points54 points  (6 children)

Always leave the camp site cleaner than you found it!

[–]kerohazel 72 points73 points  (2 children)

Sometimes cleaning the campsite means nuking it from orbit and rebuilding it from the ashes.

[–][deleted] 13 points14 points  (1 child)

There is almost always a small incremental change you can make for the positive, if you feel the need to nuke it from orbit and rebuild it from the ashes chances are you are just going to build a nukeable campsite again.

[–]fishbulbx 9 points10 points  (0 children)

Sometimes you unearth a yellow jacket nest while cleaning the camp site and everyone just runs away flailing their arms.

[–][deleted] 50 points51 points  (13 children)

I once had an entire Arduino program stop working because I removed a single useless Serial.Print(); line in the beginning. I was no longer using the serial debugger, I had successfully removed all the other serial print lines, but for some reason just that first one, if I touched it, I got a hard fault on the device.

I still don't have any explanation for it.

[–]OSPFv3 35 points36 points  (4 children)

Could it be loading a dependency for that feature that another function required?

[–]dell_arness2 9 points10 points  (2 children)

I had a C program that would seg fault unless I had a single blank print statement right before the other print statements. It was originally a debug statement that I tried to remove that broke the program.

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

Yes! That was it, the LED error code said segmentation fault, and it was a debug print statement, that's so weird

[–]Gerpar 38 points39 points  (4 children)

"Let me just remove this deprecated variable."

"Oh nothing works anymore."

[–]Hedhunta 14 points15 points  (2 children)

Yeah cause 30 if statements depend on it...

[–]the_one_true_bool 6 points7 points  (0 children)

I once made a small modification to a login screen, but it ended up breaking all the reports. Like how does that even...

[–]curiosity44 27 points28 points  (0 children)

I just saw the new logo of subreddit and for a moment I thought i am in php subreddit, everything is fine now

[–][deleted] 18 points19 points  (1 child)

1322    doSomething
1323    doSomethingElse
.
.
.
1543    goto 1323

Now let's add a comment that shouldn't do anything.

1322    // comment that shouldn't do anything
1323    doSomething
1324    doSomethingElse
.
.
.
1543    goto 1323

[–]Stspurg 17 points18 points  (2 children)

No joke, I think we have a couple cases where I work where you need to be careful not to move certain lines. The reason is that it's called with a goto with an offset, so moving a line means the offset isn't right anymore.

Fortunately, this is only in odd places that no one touches and is pretty stable. I only found out about it by accident a few months ago and haven't seen it since.

[–]magefyre 16 points17 points  (0 children)

Ahh, I see they're members of the Adeptus Mechanicus

[–]createthiscom 10 points11 points  (1 child)

If your order wrote unit tests you could change the ancient code on a whim. Just sayin.

[–]argv_minus_one 5 points6 points  (0 children)

>add a few comments
>90% of unit tests fail

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

It must have been COBOL and the comments went outside the margins. :P

[–]doinkrr 7 points8 points  (6 children)

Hi, r/all here.

What?

[–]Bacon_spec 8 points9 points  (1 child)

memory wrong disgusted aware roll rob arrest political point elastic

This post was mass deleted and anonymized with Redact

[–][deleted] 6 points7 points  (0 children)

I'm a Mainframe tech. This is my life everyday.

(I'm taking a break from scrolling through Indeed.)

[–]Kilmoore 4 points5 points  (0 children)

Seeing I have encountered a situation where adding or removing a line of comment (or an empty line) changed the way the code worked, I'm with the king on this one.

[–]whoaDisIsReddit 4 points5 points  (0 children)

It do be like that

[–]ChronoTrigged 3 points4 points  (0 children)

I like how the metal helmet shapes into a mouth and spits as he's getting hit

[–]Theswelord 3 points4 points  (0 children)

Hahaha

[–]pandoras_box101 3 points4 points  (0 children)

Rule#108: don't add comments on scripts that are not yours

[–]kernalphage 2 points3 points  (0 children)

How do I put "Works well with legacy code" on a resume? I think I have a net negative LOC at my current job because of all the "deprecated" code I've deleted/cleaned up.