Recent CodeSOD

Code Snippet Of the Day (CodeSOD) features interesting and usually incorrect code snippets taken from actual production code in a commercial and/or open source software projects.

Apr 2023

An Incremental Replacement

by in CodeSOD on

Jon sends us an entire function, but I'm just going start with the comment, after which you'll understand everything bad about this without me explaining further. Don't worry, I will, but the comment says it all:

/**
 * Returns the next ID of a table. This function is a replacement for MySQL's
 * auto-increment so that we don't need it anymore.
 *
 * @param  string  $table The name of the table
 * @param  string  $id    The name of the ID column
 * @return integer
 */

Bubbled to the Top

by in CodeSOD on

What makes code bad? Is it an inherent property of the code itself? Or is it the context in which the code is placed?

For example, let's look at this function, found by Lars.


Dining on Leftovers

by in CodeSOD on

Sometimes, you go up to the fridge, looking for a snack, and see the array of leftovers, and your appetite fails you. You want to eat, you know you have plenty of options that you just need to throw into the microwave, but none of them look appetizing. Leftovers always seem like a better idea when you're busy making them than when you're trying to deal with them after the fact.

Stev's co-worker has left them a large pile of leftovers. This block is representative of all the half-finished leftovers that don't do anything and shouldn't be there, but happily make it much harder to understand the codebase:


Randomly Generated

by in CodeSOD on

There was an era, briefly, where corporations flirted with the idea of replacing their web based UIs with Flash. There were even UI frameworks, like Flex, built to support that kind of migration. Yes, it was a disaster.

But not quite as much of a disaster as this action script code that Daniel found. At some point, someone at Initech decided they needed to implement a random number generator. This is what they came up with:


Get Selected

by in CodeSOD on

Matt is supporting an old VB.Net application. This representative line highlights the overall code quality:

If Me.lstRequests.GetSelected(Me.lstRequests.SelectedIndex) = False Then

Providing Authentication

by in CodeSOD on

Paulette's team had built a wrapper method around the C# authentication APIs that acted as a convenience function. Called GetAuthenticationProvider, it accepted two parameters. The first, a string, could be used to look up the provider's type in a lookup table. The second was a boolean, which indicated whether it should be an local authentication provider, or an external provider (e.g., OAuth). At least, that's what the documentation says.

The function would find and/or construct the correct type of provider, and return it. They all shared an interface, so this all should work just fine.


Random Permutation

by in CodeSOD on

Gary inherited some C code. This C code is correct, and does what it is supposed to, which is itself a pretty straightforward problem: create an array of values from 0-N, arranged in a random order.

Now, there are a lot of possible ways to do this, most of which involve swapping cells in the array around. It's quick, efficient, and easy to understand. Which means Gary's co-worker needed to find a better solution.


Projection Your Failures

by in CodeSOD on

Geoinformatics ends up needing to deal with a pretty thorny problem: projecting a sphere onto a 2D coordinate system. There is no single correct way to do this, and depending on many factors, you may need to choose different projections- for maps covering small regions, the curvature of the Earth might have only a minor effect; the curvature is different depending on your latitude; depending on your needs, you might need different projections that prioritize retaining (or discarding) certain relationships between map coordinates.

Given how hard this problem is, people have put a lot of time and effort into solving it with some standards. In this case, the standard is EPSG codes, a registry of reference systems, different values for the shape of the Earth, and different coordinate systems, all granted a unique identifier in the range 1024–32767.


Regular Query String

by in CodeSOD on

Robert H is taking the first steps required to make existing code supportable: writing unit tests. The code in question isn't that old, it was just developed by someone who didn't care about mundane tasks, like testing.

They also didn't care about things like paying attention to web standards, and thus were using the same copy/pasted utility functions they'd been using for a decade.


Self-Documentation

by in CodeSOD on

It's rare to find truly self-documenting code. Adam, however, while hunting down a bug, found some.

Adam was trying to understand exactly where some functionality was coming from in a Java program. The code base had some very long inheritance trees, which was an absolute nightmare for maintenance. The mix of overridden methods and mysterious interfaces and dependencies from the lowest levels of the hierarchy all the way back to the top created some of the most spaghettied spaghetti code you could imagine.


Constantly Named

by in CodeSOD on

We’ve seen constants in the form of static final int ONE = 1 before. At this point, it’s barely worth discussing, it’s become so common. But Susan has found us yet another unique twist on the subject.

It started when a contractor wrote Java code that looked like this:


Uniquier

by in CodeSOD on

Sole Purpose of Visit's team had a simple enough problem: they needed to generate unique labels for widgets on the screen. These widgets just need those unique IDs for internal purposes, so a simple incrementing counter would have been sufficient.

That isn't the direction they went in.


Select Your Dropdown

by in CodeSOD on

Francisco's co-worker needed to populate a dropdown with three items: "--All--", "Accepted", and "Pending". Unfortunately for this co-worker, a basic understanding of "array literals" or "lists" or, well, any collection type was slightly beyond their grasp. Instead, they chose to do what many a developer who doesn't understand the date/time functions of their languages does: ask the database for help:

select '--All--' union select 'Accepted' union select 'Pending'

In House Refactoring

by in CodeSOD on

Today's submitter works for a company that has a history of outsourcing pretty much all development, and to the lowest bidder, at that. This resulted in some terrible code, complete with 30,000 line classes, so the company decided to bring development back in house and clean up the code.

After three years of hard work, the in-house team was proud to announce their new, more maintainable, version of the code. Unfortunately, they didn't have the best sense of what maintainable looked like, so their Java code had methods with signatures like this:


Changing the Type of Newbie

by in CodeSOD on

Today's story isn't really a WTF. No, Phil R was mentoring a new developer, and as such, Phil was sending some simple, easily scoped Python tasks over to that developer. After a few really low hanging fruit, Phil gave the Newbie a trickier one. The contact_type field was currently a string. That was a mistake, and it needed to be changed to a number. The change would involve a whole bunch of find-and-replace, but mostly it was just changing strings like '07' into 7.

Newbie trotted off, and found this code block: