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.

Jul 2023

Just a Copy of Copy

by in CodeSOD on

I don't think anyone would argue that BASH provides a good scripting environment. It provides a commonly available scripting environment that even its successor shells attempt to emulate. But it's idiosyncratic, at best, and leads to bug-prone scripts.

But there are certain tasks that it excels at- specifically, the kinds of tasks that you'd normally do from the shell but just want to wrap some light automation around. Want to scan a directory and then copy some subset of the files out based on user arguments? BASH is your friend! It's just some basic conditionals and some cp commands, what could be easier?


Fast Conversion

by in CodeSOD on

Clara had a database of users. The EmailAddress field in the user table was, by design, nullable, but some downstream applications didn't like nulls and wanted something there. Now, it didn't particularly matter what the values were, just that there were some, so Clara wrote up a quick stored procedure that would return the users with the null emails converted to the format BlankEmail_YYYYMMDD, e.g., BlankEmail_20230726.

This was all well and good until one of the senior developers decided that Clara's use of the T-SQL date functions was killing performance. They made this decision based on being the senior developer, but not based on any metrics or performance monitoring. Real seniors don't need such trivialities- they know what badly performing code is just by looking at it.


Base of the Hash

by in CodeSOD on

Jamie Kitson followed the instructions to integrate their software with a new payment provider. The payment API was fairly straight forward, mostly a straightforward call to a web endpoint. As an error check, the request required an base-64 encoded, MD5 hash of its contents appended to the end of it.

Jamie did just that, in C#. And the payment processor balked: the hash was wrong. There was no information beyond that, just "bad hash".


The Set Up

by in CodeSOD on

My heretical opinion on object-oriented programming is that I don't like getters and setters. They're often trivial boilerplate (boilerplate is a code smell), or they're hiding behavior where behavior probably doesn't belong.

Yes, yes, I understand the importance of encapsulation, but in a lot of ways, trivial getters/setters break encapsulation. void setFoo(T foo) { this.foo = foo; } does nothing to protect foo against unauthorized modifications.


The Apex of Development

by in CodeSOD on

David S writes: "I'm undertaking a refactor and facelift of an Oracle APEX application."

That, already, is the real WTF. Oracle Application Express, or APEX (formerly ApEx, formerly HTML DB) is Oracle's offering in the low-code business application space. Using a WYSISYG designer, you build pages and bind them to SQL queries, stored procedures, etc., allowing users with little to no programming experience to design data driven applications.


Paging the Bean

by in CodeSOD on

Today's an interesting case of code that seems perfectly reasonable at first glance. Sent to us from FinalGuy, this Java code caused no end of problems for him due to its rather unexpected behavior:

public void setCurrentPage(int currentPage) {
    if ( currentPage < 1 ) {
        currentPage = 1;
    }
    if ( currentPage > maxPages ) {
        currentPage = maxPages;
    }
    this.currentPage = currentPage;
}

Evaluating Tax Burdens

by in CodeSOD on

Many years ago, Westie's employer got hired to help a client rebuild their tax advice website. The current version was… not in good shape, didn't provide a good user experience, and was incredibly unmaintainable. Westie's mission? Rewrite it from scratch.

As these projects go, however, the requirements were ill documented, and basically were "Uh, just make it do all the same things, but it shouldn't suck anymore."


Universal Stream

by in CodeSOD on

Twenty years ago, Stefano Z worked with some Very Smart Very Senior Engineers. These Very Smart Senior Engineers liked to find all sorts of "interesting" solutions to common problems.

For example, they had a module in C++ that needed to send data to other systems. To formalize this messaging, they needed a set of Data Transfer Objects (DTOs). Now, there are many libraries that might make generating these kinds of objects easier, but the Very Smart Very Senior Engineers didn't want to use a library. So they started out with just large piles of hand coded:


edoced_46esab

by in CodeSOD on

Rick is supporting a Magento-based e-commerce site. As many such sites, it uses a lot of third party plugins. One of those third party plugins wants to make sure no one "steals" its code, and thus obfuscates the code.

$_F=__FILE__;$_X="JF9GPV9f...oJF9EKCRfWCkpOw==";$_D=strrev('edoced_46esab');eval($_D($_X));

Yearly Updates

by in CodeSOD on

C. Wiles has the "joy" of working on a SAS application for a large health insurance company. This snippet was extracted some time ago, but the application continues to live on. It has one job: pad the month part of dates to two digits. Let's see how it's done.

%macro yearmonth;
		if year_month = '2010-1' then year_month = '2010-01';
		else if year_month = '2010-2' then year_month = '2010-02';
		else if year_month = '2010-3' then year_month = '2010-03';
		else if year_month = '2010-4' then year_month = '2010-04';
		else if year_month = '2010-5' then year_month = '2010-05';
		else if year_month = '2010-6' then year_month = '2010-06';
		else if year_month = '2010-7' then year_month = '2010-07';
		else if year_month = '2010-8' then year_month = '2010-08';
		else if year_month = '2010-9' then year_month = '2010-09';
		else if year_month = '2010-10' then year_month = '2010-10';
		else if year_month = '2010-11' then year_month = '2010-11';
		else if year_month = '2010-12' then year_month = '2010-12';
		else if year_month = '2011-1' then year_month = '2011-01';
		else if year_month = '2011-2' then year_month = '2011-02';
		else if year_month = '2011-3' then year_month = '2011-03';
		else if year_month = '2011-4' then year_month = '2011-04';
		else if year_month = '2011-5' then year_month = '2011-05';
		else if year_month = '2011-6' then year_month = '2011-06';
		else if year_month = '2011-7' then year_month = '2011-07';
		else if year_month = '2011-8' then year_month = '2011-08';
		else if year_month = '2011-9' then year_month = '2011-09';
		else if year_month = '2011-10' then year_month = '2011-10';
		else if year_month = '2011-11' then year_month = '2011-11';
		else if year_month = '2011-12' then year_month = '2011-12';
		else if year_month = '2012-1' then year_month = '2012-01';
		else if year_month = '2012-2' then year_month = '2012-02';
		else if year_month = '2012-3' then year_month = '2012-03';
		else if year_month = '2012-4' then year_month = '2012-04';
		else if year_month = '2012-5' then year_month = '2012-05';
		else if year_month = '2012-6' then year_month = '2012-06';
		else if year_month = '2012-7' then year_month = '2012-07';
		else if year_month = '2012-8' then year_month = '2012-08';
		else if year_month = '2012-9' then year_month = '2012-09';
		else if year_month = '2012-10' then year_month = '2012-10';
		else if year_month = '2012-11' then year_month = '2012-11';
		else if year_month = '2012-12' then year_month = '2012-12';
		else if year_month = '2013-1' then year_month = '2013-01';
		else if year_month = '2013-2' then year_month = '2013-02';
		else if year_month = '2013-3' then year_month = '2013-03';
		else if year_month = '2013-4' then year_month = '2013-04';
		else if year_month = '2013-5' then year_month = '2013-05';
		else if year_month = '2013-6' then year_month = '2013-06';
		else if year_month = '2013-7' then year_month = '2013-07';
		else if year_month = '2013-8' then year_month = '2013-08';
		else if year_month = '2013-9' then year_month = '2013-09';
		else if year_month = '2013-10' then year_month = '2013-10';
		else if year_month = '2013-11' then year_month = '2013-11';
		else if year_month = '2013-12' then year_month = '2013-12';
		else if year_month = '2014-1' then year_month = '2014-01';
		else if year_month = '2014-2' then year_month = '2014-02';
		else if year_month = '2014-3' then year_month = '2014-03';
		else if year_month = '2014-4' then year_month = '2014-04';
		else if year_month = '2014-5' then year_month = '2014-05';
		else if year_month = '2014-6' then year_month = '2014-06';
		else if year_month = '2014-7' then year_month = '2014-07';
		else if year_month = '2014-8' then year_month = '2014-08';
		else if year_month = '2014-9' then year_month = '2014-09';
		else if year_month = '2014-10' then year_month = '2014-10';
		else if year_month = '2014-11' then year_month = '2014-11';
		else if year_month = '2014-12' then year_month = '2014-12';
%mend yearmonth;	

Terned Off Validations

by in CodeSOD on

Validating user input is one of those problems that's deceptively simple. The actual validation rules tend to be simple to describe: this field should be at least ten characters long, that field should match this regex, this one should be a number. But applying those rules can get cumbersome, especially when you write it yourself.

It can quickly get as thorny and difficult as date handling.


Curious Queries about Performance

by in CodeSOD on

Andreas's employer recently upgraded their SQL Server databases to the latest version. During that upgrade, something went wrong, and performance dropped off a cliff. Applications which used to be merely slow to taking hours to query the data they required. Clearly, something was wrong in the upgrade process, but Andreas wasn't a DBA, so that wasn't specifically Andreas's problem. Instead, there were plenty of badly performing blocks of code- maybe now was the time to try and optimize them.

That's where this block of Visual Basic comes from.


Twice, Thrice

by in CodeSOD on

Most (but clearly, not all) programmers have generally mastered the basic for loop. If you really want to separate the wheat from the chaff, ask them to write a for loop that counts by two, or three, or some other arbitrary number.

That's what happened at J Banana's workplace, where they needed to parse in tokens from a config file. They come in key value pairs, so the code needs to count by twos. This was their solution:


Characters Remaining

by in CodeSOD on

Tina works for a major European government. During the height pandemic, her team needed to roll out many new sites to support remote, self-service interactions with government agencies.

On one of these sites, they needed to create a contact form. The contact form was one of those that applied a character limit to the message. This character limit meant that the code needed to check the length of the input string, and then compare that against some max characters, and then output the "characters remaining" in an output field.