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.

Nov 2018

Classic WTF: A Spoonful of Sugar

by in CodeSOD on
It's Black Friday in the US, which is a fancy way of saying that we're all busy murdering each other in WalMart over PS4s. In case we don't survive, remember us by this classic article about Cold Fusion. Original -- Remy

John S. was doing some work on the search feature of a client's website when he noticed that he would receive a 500 Server Error if he tested against the API with an empty string.

This struck John as being pretty strange since not only had the search feature had been in place for years, but also, he could go to the search page, click on the "Search" button without entering anything and receive an "Item Not Found" response.


Classic WTF: zzGeneralFunctions

by in CodeSOD on
It's Thanksgiving in the US today, so we're running a classic WTF. What are you thankful for? How about not needing to support this application. Original --Remy

A codefile whose name is prefixed with “zz” can be one of two things. It's either a file that someone wanted to get rid of but was afraid to delete, or it's an intentional naming scheme to keep the file at the bottom as part of some crude code-organization technique. There used to be a third option - the file's a part of an application commissioned by a certain American rock trio known for their beards and cheap sunglasses - but the band dropped that requirement a long time ago.

However, when Mark Arnott stumbled across "zzGeneralFunctions.asp" as part of a maintenance project he was assigned, it was pretty clear why the file existed. Its first line contained the following comment:


Classic WTF: Let Me Sleep On It

by in CodeSOD on
We're starting our Thanksgiving break a day early this year. To make up for it, we're dipping back into the archives for a classic WTF. Original

"Perl is a language for getting your job done," is the underlying philosophy of the language. The only right way to write a Perl program is whatever way works. The ultimate flexibility of Perl is a breeding ground for WTFs . That's doubly true when you're new to the language, like Dave once was.

To get Dave started with Perl, his boss paired him up with Alvin, the veteran Perl programmer. He'd been using Perl since version 4, and had a reputation for wielding regexes like a scalpel. After Dave had a few days of ramp up, Alvin started sending him code from their codebase so that Dave could try and understand how their applications worked.


Class Warfare

by in CodeSOD on

Setting aside cross-browser quirks, CSS is a fiendishly complicated specification. There’s a lot to it. From styling rules and how they interact with the DOM hierarchy, to the complexities of using selectors to navigate the DOM- it’s a complex tool that is also very powerful. I mean, it’s Turing complete.

Shiv works with a self-proclaimed “CSS Ninja”- yes, that was actually in their resume when they got hired. They were hired on the strength of their portfolio- it looked very nice. Unfortunately, the implementation left something to be desired.


A Clever Switch

by in CodeSOD on

Today's anonymous submitter has this to say about this code: "It works fine, it's just... clever."

I'm not certain about the relative cleverness of this solution, myself.


Tryception

by in CodeSOD on

"If at first you don't succeed, try, try again."

We have all encountered situations where we need to attempt an operation, with full knowledge that the operation might very well fail. And if it does, we should retry it. Usually after a delay, usually with a maximum number of retries.


A Profitable Education

by in CodeSOD on

Today’s anonymous submitter is an experienced Python developer. Their team is significantly less experienced. In the interests of training, someone suggested, “Perhaps we should buy books for everyone.” Our submitter was handed a stack of possible books, and asked to pick the best one to help the team mature.

One book spent some time discussing types, and the conversion between them. By way of example, it provided a handy method which would turn a string into a floating point number:


To Round a Corner

by in CodeSOD on

Last week we saw an attempt to reinvent the ceil function. Tina raised a protest: "6 lines to re-implement a ceil function? We can do better."

//Rounds to 1000d Superior public int round1000dSup(int value_To_Round) { int finalResult = 0; int resultIntermediate = value_To_Round / 1000; resultIntermediate += 1; int valueRounded = resultIntermediate * 1000; if ((valueRounded - value_To_Round) == 1000) { finalResult = value_To_Round; } else { finalResult = valueRounded; } return finalResult; }

A Swift Update

by in CodeSOD on

Banks are uniquely identified via a “SWIFT Code”. It’s an ISO Standard. Having an accurate SWIFT code for a given bank is of vital importance to anyone doing financial transactions. With mergers, moves, new branches, and so on, the SWIFT codes you do business with won’t change often, but they will change.

Thus, Philip wasn’t terribly surprised when he got a request to update a pile of SWIFT codes. He couldn’t make the change via the database, though, as no one had permission to do that. He couldn’t edit it through an application UI, because no one had ever built one.


Hitting Your Skill Ceiling

by in CodeSOD on

Clia was handed a pile of legacy code and told to upgrade it, but with a very important rule attached: the functionality couldn't change. Any change could break someone's workflow, and thus in the upgraded system, even the bugs had to be reproduced.

Unlike most "legacy" code, this wasn't all that old- it was written in C#. Then again, C# is old enough to drive, so maybe it is old. Regardless, C# has utility methods, like, say, a ceil function. At no point in C#'s history has it lacked this basic functionality.


For a Long While

by in CodeSOD on

Here’s a philosophical question. Let’s say you’re searching an array. Is it clearer to use a for loop and break when you find the element, or is it better to use a while loop and break if you hit the end of the array?

Most of us would likely use the for loop, but it wouldn’t be wrong to use the while- maybe just unexpected.