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.

Dec 2023

Vestigial Organs

by in CodeSOD on

Matt inherited some C# code that reads from a JSON config file.

public ServerJsonLoader(string configFile)
{
	using (StreamReader reader = File.OpenText(configFile))
	{
		JObject config = ... //snip

		if (config.GetValue("inputs") != null)
		{
			this.mixedConfig = config;

			//Inputs
			var inputs = ... //snip

			//Outputs
			if (config.GetValue("outputs") != null)
			{
				var outputs = ... //snip
			}                
		}
		else
		{                 
			if(config.GetValue("inputs") != null)
				this.serverConfig = config;

			if (config.GetValue("outputs") != null)
				this.clientConfig = config; 
			
		}                    
	}
}

Misunderstood the Assignment

by in CodeSOD on

Benjamin doesn't have a lot of complaints about the project he works on. "There is," he writes, "no tale of daily woe." But even an otherwise healthy project isn't immune to WTFs.

BOOL CCache::CalculateBmpImageSize(SIZE BackgroundControl, SIZE& NewImageSize)
{
        BOOL    bRes = TRUE;

        try
        {
                NewImageSize = BackgroundControl;
        }
        catch(...)
        {
                bRes = FALSE;
        }

        return bRes;
}

A Reckless Driver

by in CodeSOD on

For a variety of reasons, Robert's company needed to ship a small linux box with a USB ethernet adapter. The driver for said adapter was giving him some issues with their setup, but fortunately the driver was open source, so Robert could at least try and diagnose the problems.

In the file inst.c, he found this:


No Lifeguard in this Connection Pool

by in CodeSOD on

Michael was debugging a system where the J2EE service would just… crash. It was running on a Windows host set to restart the service when this happened, so the outages were brief, but it was a serious problem.

He eventually tracked down the problem to a class called JdbcSchema, which is about 1,100 lines of homebrew database management code. It does "fun" things, like create extra tables in the database to track "foreign key metadata".


Monthly Totals

by in CodeSOD on

Daniel spotted an array called $months in a PHP application. The data being stored in the array was some total of the sales of some commodity, for the past 5 months.

That much all made sense. What didn't make sense was how the array was indexed.


Secure Mentorship

by in CodeSOD on

Sophia entered the software development industry through an "alternative" path. In Germany, they have a tradition of Ausbildung. Instead of going to university, you focus on on-the-job training with some classes mixed in. There are still exams and all the traditional academic trappings, but with one huge bonus: you're working closely with an Ausbilder- an experienced industry veteran who can show you the ropes.

Sophia's Ausbilder was named Tim, and at the company she was working at, he was widely hailed as a genius. He had designed their entire web system from the ground up in Java, which given that every other developer at the company only worked in Delphi, that made him a pretty unique talent. Everyone was pretty convinced he was brillant.


Padding the Amount

by in CodeSOD on

Matt's co-worker needed to handle some currency values coming in as strings. As this was in C++, we're already in a fraught territory of having to worry about whether the callers and various APIs we're using handle C-style strings or C++ std::string.

Or, you can just mix-and-match in code and hope for the best, while having some interesting approaches to handling your inputs. That's what this developer did.


A Nice Break

by in CodeSOD on

Once upon a time, HTML had tags like <marquee>, which scrolled text across your page, and when combined with animated GIF backgrounds basically defined the Geocities aesthetic.

Since then, the HTML specification has been refined, and the choice has been made that HTML tags should (mostly) be about semantics- describing the structure of a page, the meaning of elements, and the relationship between those elements. It generally shouldn't describe the presentation of those elements (CSS should do that)- even though the semantics generally imply something about the display (paragraphs and divisions are block elements, for example).


Evaluating Perks

by in CodeSOD on

Today's anonymous submitter works for a company that handles customer rewards perks. It's handling thousands of dollars of transactions a day, which isn't a huge amount, but it's certainly non-trivial.

Now, there's a conversion formula from points to dollars: points/100*1.7. Now how would someone implement this complex formula in PHP? Well, our submitter's predecessor did it this way:


TrUe Romance

by in CodeSOD on

Mario's predecessor was writing some Java code that parsed textual input. Thus, it needed to be able to turn the string "true" into the boolean value true, but also needed to handle "True" and "TRUE".

Now, in Java, the obvious answer would be to use the equalsIgnoresCase String member function. if (value.equalsIgnoresCase("true"))…. I mean, obvious to you or me, anyway.


Capitalizing on Memories

by in CodeSOD on

Gavin inherited some "very old" C code, originally developed for old Windows systems. The code is loaded with a number of reinvented wheels.

For example, they needed to do a case insensitive string comparison. Now, instead of using stricmp, the case insensitive string comparison, they wrote this: