DEV Community

What's the Worst Code You've Ever Seen or Written?

Jeremy Grifski on April 29, 2019

Earlier today, I published an article about the struggles of working in teams (it's a little personal, so apologies for the paywall). In that artic...
Collapse
 
xteesy profile image
Will

This requires a bit of backstory, but it's worth it.

Dev worked on private company project. Dev was only "dev" that actually did anything valuable (there was 1 UX guy who was a consultant and a designer with extremely limited css skills).

Dev was owed little over $20-30k in commissions from a project.

Dev didn't get paid, instead he got fired (company's owner was a next level greedy asshole).

Mistake #1 by company: Not owning their repo

Mistake #2 by company: Pissing off a dev who owns the repo

Dev proceeds to delete all his work from server, deleted the repo, and then uploaded his own copy of everything back to the company (he didn't want to get sued).

There was only 1 small thing he changed. Every single class, ID, function name, you name it was now obfuscated. I don't really know how he did it, but everything was also connected to a script on HIS server. That script changed each class name, ID, and function every 24 hours or so?

Needless to say the company thought "well fine, we'll just rename it all and go from where he left off", and then everything renamed itself. These aren't devs so they are basically freaking out, and it turned into quite a hilarious event.

TL;DR: Dev got fired, dev was owed around $20-30k, dev proceeded to rename everything to look like "na89s7fn7234fna8h12sba" as class names on nearly 65,000 lines of code. Company finally paid up and got their original code back after realizing attempting to fix it themselves was pointless.

Collapse
 
renegadecoder94 profile image
Jeremy Grifski

This is great. Haha this takes the “villain” definition of worst.

Collapse
 
pbouillon profile image
Pierre Bouillon • Edited

I once heard about a project where the business logic would depend of the front.
Something like:

if (tile.style.backgroundColor == 'red') {
    // do stuff as 'warning' or else
}

I don't know if this is considered as bad code, but it sure is awful.

Collapse
 
renegadecoder94 profile image
Jeremy Grifski

This looks like the kind of code I'd write when I'm working with D3. haha I struggle so much with web dev stuff.

Collapse
 
laurieontech profile image
Laurie

I don't like the idea of categorizing "worst" code based on arbitrary metrics like lines of code, O(n) efficiency, etc.

However, I'd say bad code is difficult to understand and read. Code will always be interpreted by machines, but it should be comprehensible to humans.

But if I'm being honest the "worst" code I see is code with horrible security. I.e. plain text passwords, or hardcoded credentials.

Collapse
 
renegadecoder94 profile image
Jeremy Grifski

I agree with all this! I do quite a bit of teaching, and I stress readability, correctness, and testing over metrics like efficiency and whatnot.

Collapse
 
laurieontech profile image
Laurie

Inefficient code is more often than not hard to read! There is a reason people try to get rid of "pyramid code".

Thread Thread
 
renegadecoder94 profile image
Jeremy Grifski

For sure! I was just referencing your mention of big O as a criteria for bad code.

Thread Thread
 
laurieontech profile image
Laurie

Oh absolutely. We're in total agreement!

Collapse
 
niorad profile image
Antonio Radovcic

Code that works is never bad, but this is probably the most hacky one from my last project:

.flatpickr-prev-month[style="display: none;"],
.flatpickr-next-month[style="display: none;"] {
    display: block !important;
}
Collapse
 
renegadecoder94 profile image
Jeremy Grifski

What does this do?

Collapse
 
niorad profile image
Antonio Radovcic

It displays an element which has been set invisible by a JavaScript-Library that I didn‘t want to hack. There was no option in the API to do it.

Thread Thread
 
d1p profile image
Debashis Dip

Cleaver code is not bad code xD

Collapse
 
sleepyfran profile image
Fran González

Uh, definitely the code that I had to put up with when I was doing a scholarship in one of the biggest company of my country. We were making an Angular application that had a couple of pages, one of which included a map, a table and a lot of buttons with actions that will change the state of the map and the table. So well, the guy who wrote all that decided it was a good idea to have it all in one single component with more than 10k lines of code across several methods named almost the same with a huge list of component variables handling the whole state, with incredible names like "points", "pointss", "pointsss"; basically he added another letter every time he couldn't think of a name.

Also in that project: Angular forced him to use TypeScript but he didn't want to write any interface because what's the point, right? So he annotated everything with any.

Oh, man, I do not miss being there.

Collapse
 
renegadecoder94 profile image
Jeremy Grifski

Oh no... I can see it now:

function zoom() { ... }

function zoomMore() { ... }

function zooming() { ... }

function zoomz() { ... }
Collapse
 
sleepyfran profile image
Fran González

That just brought very very bad memories hahaha.

Collapse
 
itr13 profile image
Mikael Klages

Was pointsss a list of lists of lists of points?

Collapse
 
sleepyfran profile image
Fran González • Edited

Of course not, that'd have made a little tiny bit of sense!

Collapse
 
itr13 profile image
Mikael Klages

A bit off-topic, but parts of your site confused me a little when I visited the link in your post, so I thought you might like hearing about it.

The "Members" tab shows both "Account" and "Login" no matter if you're logged in or not (And no "log out"). And if you can't see a page it just shows "You are unauthorized to view this page." in small text where the content would be, without telling you if it's because you're not logged in, or don't have a subscription.

There's also no easy way of seeing if an blog post is free or not. If I go Members.Account > Subscriptions > Basic it sends me to /tag/free which lets me filter the free ones, but I don't see a way of getting there from the blog itself. Nor are the tags visible in the blog post list.

Collapse
 
renegadecoder94 profile image
Jeremy Grifski • Edited

All great critiques! Unfortunately, I haven’t found any nice ways to work around them. I’m not a web dev, so I’ve been rocking Wordpress for about 2.5 years now. Solutions to these problems aren’t always clear cut, and I’d prefer not to edit the theme files if I don’t have to.

EDIT: Okay, I fixed at least one of the problems! The unauthorized messaged you get is different if you're logged in or not now. I'm using MemberPress, and they have a shortcode which I've setup like this:

[mepr-show if="loggedin"]Looks like you're logged in, but you don't have a premium membership. If you're interested in getting one, head over to the memberships page and select a tier.[/mepr-show][mepr-show if="loggedout"]Looks like you're not logged in to view this content. If you have an account, feel free to log in below. Otherwise, head over to the memberships page and sign up for a tier. [/mepr-show]

As for the other problems, I'll have to sit and think about them for a bit.

Collapse
 
renegadecoder94 profile image
Jeremy Grifski • Edited

Alright, I just removed the login link altogether. All your account related info should be in the sidebar anyway.

I'll look into the last issue. I'm thinking I might have to add a watermark or something to the images. Otherwise, I'll have to add some custom CSS or something to make free/paid articles clear.

Thread Thread
 
renegadecoder94 profile image
Jeremy Grifski • Edited

At last, I'm adding borders to the images, so it should be a bit more obvious when something is free (silver border) and when it is not (gold border). Public articles will continue to have no border at all.

Also, I added the free and premium tag links to the top of the blog archive. You should be able to get there in two clicks from the homepage now.

Thread Thread
 
itr13 profile image
Mikael Klages

Seems like you've gotten a lot done, looks way better now ^
Even easy to use on mobile.

Collapse
 
rachelsoderberg profile image
Rachel Soderberg • Edited

I was hired on at my current job to replace the developer before me who was part of the way through building an application that replaces what was at the time done in three very old applications. The opening method, called by pressing the start button, was named "DoStuff()". There were several other versions of "DoStuff" throughout the code and it made the job of learning what had already been done and needed to be done even harder than it needed to be. Thanks guy before me.

Collapse
 
renegadecoder94 profile image
Jeremy Grifski

Haha I always love stories like this. It seems readability is always an afterthought.

Collapse
 
rachelsoderberg profile image
Rachel Soderberg • Edited

Sure seems like! The first time we were debugging a soap request together on code I was able to conceptualize and write from the ground up, my manager was blown away at the OOP style of error output from small, single-job methods with descriptive names: "oh, it's getting hung up in the binding portion of the login method. Wow that's helpful!"

No more DoStuff() on my watch, dang it!

Collapse
 
renegadecoder94 profile image
Jeremy Grifski • Edited

I teach a Java class, and students often gravitate toward strings because of their functionality. Why try to get the third digit from a number using math (i.e. (5092 / 10) % 10) when you can convert it to a string and call the index method on it (i.e. Integer.toString(5092).charAt(3))?

Not making an excuse for this code! It's bad, but I can see why people do it. haha In this case, it really makes no sense.

Collapse
 
buphmin profile image
buphmin

For me it was a 2000 plus line function in PHP that had several include files with variables declared and used across a variety of files, then multiple 1 character variables reused for completely different things and the only comments were leftovers from some other time so they were inaccurate and misleading.

Collapse
 
renegadecoder94 profile image
Jeremy Grifski

This sounds like an obfuscation challenge. haha

Collapse
 
circuitburn profile image
Patrick Ryan • Edited

I wrote this gem yesterday:

if (!$doNotSendActivationEmail)
{
    //...
}

The client does most of the front-end development and the whole logic for this was implemented ass-backwards; the checkbox's checked state disabled an email from being send. I chose to run with that logic on the back-end so we would be less likely to mix up the intention of the checkbox.

Collapse
 
keptoman profile image
mlaj

I've seen a db where passwords were encoded but the password tries were logged and linked with the user. It was pretty easy figuring out their passwords.

I've also worked on a site where programmers for the pas 10 years never bothered to change previous CSS and just added their modifications on top of the rest. You could see the previous refractors load one on top of the other. They also had a different version of jquery for various sections of the site, depending on who did it. I had to change various functionalities but I had to code them in different versions of the same library. The site was an absolute beast and I could only add stuff, not refactor (boss said so). It was horrible but I learned a lot since it was my first job out of school.

Collapse
 
renegadecoder94 profile image
Jeremy Grifski

Yeah, I actually enjoy bad experiences like these because they teach you what not to do. You'll never have to say "hmmm, I wonder why it's good practice to do X."

Collapse
 
nprimak profile image
Nadya Primak

Hoo boy this is a fun one. I've seen code with comments above such as "No idea what this does" and at the time I thought "can't get any worse than this" in a professional code base. However, I was proven wrong several jobs later when I encountered a chrome extension that was so poorly written only one developer (the one who had written it) was allowed to touch it. Out of morbid curiosity, I decided to look at the code to see just how bad it could be. Literally every single variable was 1 letter, or occasionally 1 letter followed by several numbers. There was no documentation, no comments, no nothing. Things like (for f11 = 0; f11 < b23.length; f11++). I KID YOU NOT FOLKS

Collapse
 
elmuerte profile image
Michiel Hendriks

Maybe not the worst I've seen, and not actually code. But ...

Collapse
 
renegadecoder94 profile image
Jeremy Grifski

Haha that's the best comment.

Collapse
 
david_j_eddy profile image
David J Eddy

Worse code I have ever seen was my own. If you keep at it eventually you'll look back and think 'wtf was I doing?!'.

Collapse
 
renegadecoder94 profile image
Jeremy Grifski • Edited

I’d venture far enough to say that a lot of the code I write now is bad. Haha we all got deadlines!