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.

Jun 2016

The Bare Minimum

by in CodeSOD on

Let’s say you needed to find the maximum and minimum values for a field in a SQL database. If you’re like most people, you might write a query like SELECT MAX(someval), MIN(someval) FROM table.

That’s the least you could do. That’s the bare minimum. And do you want to be the kind of person who does the bare minimum? Kevin L’s co-worker doesn’t. He’s a Brian.


Built Up

by in CodeSOD on

In most languages, strings are immutable. As developers, we often need to manipulate strings- for example, constructing output through concatenation.

Constructs like foo += " and then I appended this"; “solve” this immutability issue by creating a new string instance. If you’re doing a long round of concatenation, especially if it happens inside of a loop, this could get very expensive, which is why most languages also have a StringBuilder type, which allows you to append without all that overhead of new instances. Often, the advice is that you should prefer StringBuilder objects to string.


Now There's a Switch…

by in CodeSOD on

You know what’s awful? If-then-elseif conditions. You have this long, long chain of them, and then what? If only there were a shorter, clearer way to write a large number of conditions.

Oh, what’s that? There is? It’s called a switch statement? But doesn’t a switch statement only work on equality comparisons? I’d really like something that works on any condition.


Trained Developer

by in CodeSOD on

ASP.NET, like any other web development system, has a “role provider” system to handle authorization. With a small quantity of code, you can hook your custom security settings into this API and get authorization essentially for “free”. Not every organization uses it, because it’s not sufficient for every security situation, but it’s a good starting point, and it’s guaranteed that it’ll be covered in any ASP.NET training course.

Paul’s employer recently found a new hiring strategy. Instead of hiring expensive, well qualified people, they hire completely inexperienced people on the cheap, and send them to training classes. That’s likely where this code started its life- cribbed from notes in a training class.


Simulated Congealing

by in CodeSOD on

Simulated Annealing is a class of algorithms from moving through a search space to find a solution, balancing “good enough” results against a computational budget.

John L has a co-worker that has taken this logic and applied it to writing code. Whenever code needs to change, he “randomly” changes the function in small increments until it works. The result is code that looks like this:


Lines and Lines and Lines of Order Lines

by in CodeSOD on

Darlene’s company uses Siebel for managing their enterprise. Like most enterprise software packages, it’s complicated, incomprehensible, and any significant maintenance depends on very expensive consultants.

During an upgrade, one of those Highly Paid Consultants caught a new requirement: customers wanted to be able to change an order, replacing one product code with another, all the way up until the order went into fulfillment. Now, the logical thing would have been to cancel the changed order line and create a new one, but our HPC couldn’t quite figure out how to cancel an individual line item, so he just decided to delete it instead.


A Dated Inheritance

by in CodeSOD on

Teppo works for a Finnish company that, among other things, develops a few mobile applications. This company is growing, and as growing companies do, it recently purchased another company.

One of the applications that came with this company had a mongrel past. It started as an in-house project, was shipped off to a vague bunch of contractors in Serbia with no known address, then back to an intern, before being left to grow wild with anyone who had a few minutes trying to fix it.


Returnary

by in CodeSOD on

There’s a certain class of bad code we’ve all seen before:

boolean someFunction() {
    if (someBooleanExpression) {
        return true;
    } else {
        return false;
    }
}