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.

Feb 2025

A Secure Item

by in CodeSOD on

Kirill writes:

I've worked in this small company for a year, and on a daily basis I've come across things that make my eyes sink back into their sockets in fear, but mostly I've been too busy fixing them to post anything. It being my last day however, here's a classic


Allowed Savings

by in CodeSOD on

The CEO of Delia's company retired. They were an old hand in the industry, the kind of person who worked their way up and had an engineering background, and while the staff loved them, the shareholders were less than pleased, because the company was profitable, but not obscenely so. So the next CEO was a McKinsey-approved MBA who had one mission: cut costs.

Out went the senior devs, and much of the managers. Anyone who was product or customer focused followed quickly behind. What remained were a few managers handpicked by the new CEO and a slew of junior engineers- and Pierre.


Uniquely Expressed

by in CodeSOD on

Most of us, when generating a UUID, will reach for a library to do it. Even a UUIDv4, which is just a random number, presents challenges: doing randomness correctly is hard, and certain bits within the UUID are reserved for metadata about what kind of UUID we're generating.

But Gretchen's co-worker didn't reach for a library. What they did reach for was… regular expressions?


Every Day

by in CodeSOD on

There are real advantages to taking a functional programming approach to expressing problems. Well, some problems, anyway.

Kevin sends us this example of elegant, beautiful functional code in C#:


The Right Helper

by in CodeSOD on

Greg was fighting with an academic CMS, and discovered that a file called write_helper.js was included on every page. It contained this single function:

function document_write(s)
{
   document.write(s);
}

The Mask Service

by in CodeSOD on

Gretchen saw this line in the front-end code for their website and freaked out:

let bucket = new AWS.S3({ params: { Bucket: 'initech-logos' } });

Double Checking

by in CodeSOD on

Abdoullah sends us this little blob of C#, which maybe isn't a full-on WTF, but certainly made me chuckle.

if (file!= null)
{
  if (file.name.StartsWith(userName))
  {
    if (file.name.StartsWith(userName))
    {
      url = string.Format(FILE_LINK, file.itemId, file.name);
      break;
    }
  }
}

Finally, a Null

by in CodeSOD on

Eric writes:

Yes, we actually do have code reviews and testing practices. A version of this code was tested successfully prior to this version being merged in, somehow.


On Deep Background

by in CodeSOD on

Andrew worked with Stuart. Stuart was one of those developers who didn't talk to anyone except to complain about how stupid management was, or how stupid the other developers were. Stuart was also the kind of person who would suddenly go on a tear, write three thousand lines of code in an evening, and then submit an pull request. He wouldn't respond to PR comments, however, and just wait until management needed the feature merged badly enough that someone said, "just approve it so we can move on."

int iDisplayFlags = objectProps.DisplayInfo.BackgroundPrintFlags;

bool bForceBackgroundOn = false;
bool bForceBackgroundOff = false;

// Can't use _displayTypeID because it will always be 21 since text displays as image
if (_fileTypeID == 11) // TEXT
{
    if ((iDisplayFlags & 0x1008) != 0) // Text Background is required
    {
        bForceBackgroundOn = true;
    }
    else if ((iDisplayFlags & 0x1001) != 0) // Text Background is not available
    {
        bForceBackgroundOff = true;
    }
}
else if (_displayTypeID == 21) // IMAGE
{
    if ((iDisplayFlags & 0x1200) != 0) // Image Background is required
    {
        bForceBackgroundOn = true;
    }
    else if ((iDisplayFlags & 0x1040) != 0) // Image Background is not available
    {
        bForceBackgroundOff = true;
    }
}

bool useBackground = bForceBackgroundOn;

// If an object does not have an Background and we try to use it, bad things happen.
// So we check to see if we really have an Background, if not we don't want to try and use it
if (!useBackground && objectProps.DisplayInfo.Background)
{
    useBackground = Convert.ToBoolean(BackgroundShown);
}

if (bForceBackgroundOff)
{
    useBackground = false;
}

Not Exactly Gems

by in CodeSOD on

Sammy's company "jumped on the Ruby on Rails bandwagon since there was one on which to jump", and are still very much a Rails shop. The company has been around for thirty years, and in that time has seen plenty of ups and downs. During one of those "ups", management decided they needed to scale up, both in terms of staffing and in terms of client base- so they hired an offshore team to promote international business and add to their staffing.

A "down" followed not long after, and the offshore team was disbanded. So Sammy inherited the code.


Device Detection

by in CodeSOD on

There are a lot of cases where the submission is "this was server side generated JavaScript and they were loading constants". Which, honestly, is a WTF, but it isn't interesting code. Things like this:

if (false === true)
{
	// do stuff
}

No Limits on Repetition

by in CodeSOD on

Just because you get fired doesn't mean that your pull requests are automatically closed. Dallin was in the middle of reviewing a PR by Steve when the email came out announcing that Steve no longer worked at the company.

Let's take a look at that PR, and maybe we can see why.