10 Halloween Horror Code Stories That Will Freak You Out

Tonight is Halloween!

The day of the dead, the day where people go around dressed up as ghosts, vampires and other zombies to celebrate and actually… have fun!

Yeah, Halloween is that too. I’ve looked it up a bit and found out that there are some customs of Halloween that don’t really relate to the dead. Like apple bobbing, for example. I’ve never bobbed an apple but man, that looks like fun.

But since we can’t exactly bob apples on a blog, let’s do another customary Halloween thing: telling horror stories! But not just any horror story. Horror code stories.

I’ve asked around and scoured the web searching for the ugliest, scariest pieces of code that programmers have laid eyes on. So we’re taking a day off from expressive code here, going quite to the opposite actually.

I’ve compiled my 10 favourite ones together here. I’ve tried to put as many as possible in C++, but I couldn’t resist including some pearls in other languages too. Don’t hesitate to add comments if you know other examples of scary and ugly horror code!

Even though some of them might send a chill down your back, like all things with Halloween, they’re a bit of fun too 🙂

Happy Halloween!

1) A multithreading tip

How can you make a thread communicate a piece of information to another thread? Nothing easier, the file system is waiting for you:

For a flag shared across threads and exposed via the filesystem: volatile enabledFlag;

Fs::Value::Register((uint16_t*)&enabledFlag); — Kev Kitchens (@kitchens_sync) October 25, 2017

Thanks Kev for sharing this brilliant one!

2) Algorithms on sets

We’ve already come across the importance knowing our algorithms.

How can we return the element of a set that has the higher number of occurrences? Here is a pretty brave attempt:

3) Checking if a point is inside a rectangle

We have two classes point and rect that represent a point and a rectangle respectively. Given a point and a rectangle, how to check whether the point is located inside the rectangle?

Here is a very, very meticulous method:

4) Odd numbers

Is a number odd? At least, this code is:

5) Expressive field name

This one is taken from the .NET framework. Consider the following field:

EnableJITcompileOptimizer 1 EnableJITcompileOptimizer

Looks like it enables some optimizations related to the JIT, right?

Well, here is what the documentation says:

Specifies that just-in-time (JIT) compiler optimization is disabled for the assembly. This is the exact opposite of the meaning that is suggested by the member name.

(bold font is mine).

6) Idiomatic C++

Thanks to Jens Weller for sending this one in:

This is C++. But not really in its most glorious and idiomatic form:

7) Is this a prime number?

Here is a pretty incredible function that checks whether a given number is prime. To illustrate, its prototype starts like this (I especially love the greeting comment):

bool primeCheckUgly(unsigned long long int n){//good luck ... 1 2 bool primeCheckUgly ( unsigned long long int n ) { //good luck . . .

Here is the complete code (click to enlarge):

Pretty amazing, huh?

8) Downright ugly

This one is plain ugly. If you’re a sensitive person, maybe you want to look away while performing one energetic scroll-down with your mouse to skip over it (click to enlarge):

9) Robust binary to decimal

Starting from a string representing a binary number, here is a maybe not so robust piece of code that converts it to a number in base 10:

…and it goes all the way down to 63, of course.

10) An unexpected end

Let’s close off this distressing Halloween series with an innocent piece of code that got violently severed to meet an unexpected end (notice that there are 745 lines of code below that return statement):

Oops. I know, it’s hard to sustain.

Bonus

Here is a little piece of innocuous-looking code, but that can cause horrible pain to debug. Courtesy of reader Tomek Jonarski who posted it in the comments section:

return "false"; 1 return "false" ;

Thanks Tomek!

Let’s go bob some apples

That was gory!

If you share this article with a friend or colleague, make sure they’re not afraid of looking at this kind of unsettling code. And if they are, bobbing a couple of apples could be just the thing to relax after this! And if you know other pieces of horrible code don’t hesitate to put them in comments.

Happy Halloween!

Share this post! Don't want to miss out ?