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 2010

Avoiding the Exception

by in CodeSOD on

"I found this interesting tidbit while making some changes to a .NET application," Tim Kowalski writes.

"Although the original developer claims to 'spend 9/10ths on the rule and not the exception', I would argue it's more like 5/10ths on the excuse, 4/10ths on the rule, and 1/10th avoiding the exception."


Warts and All

by in CodeSOD on

"We need to make sure that the new PL/SQL version that runs on Oracle will work exactly as it does on SQL Server," stated the directive given to Andy by his supervisor.

Andy had been tasked to port over a T-SQL function which, when passed valid string, would convert it into a data format that could then be returned back to the calling application and submitted to a label printer to produce a bar code.


"True" and True

by in CodeSOD on

"We have an old codebase where strings are used as a swiss army knife," writes Jimmi Hested, "almost everything that goes in or out of a function is ends with a .ToString(), even it's already a string... and sometimes even if it already ends with .ToString()."

"Here is a bit of code from our web front-end. Not only is there a try/catch (just to make sure everything works), but the original developer felt a string was way better than a bool."


That's One Way to Fulfill a Requirement

by in CodeSOD on

Everyone responds to new requirements on already-behind-schedule projects in a different way.  Many people feel anger, and try to find an innocuous way to show it.  Others, realizing this might finally be their chance to shine, take it as a challenge with a smile.  Still others, like Gary's colleague Steve, find a way to fulfill the requirements without actually fulfilling the requirements.

With less than a week remaining till the deadline of a 4-month-long project, Gary and Steve's boss demanded everyone write Unit test code.  Gary spent hour upon unpaid overtime hour adding as many unit tests as he could come up with, while Steve left by 5pm each day.  Their boss also asked them to set up a nightly script to perform the unit tests automatically.  Each night Steve's test set would always come back with 100% successful results.


Maximum Pad

by in CodeSOD on

Brian's company needs to track financial information indexed by 100 digit routing numbers. Now, obviously, not all of those digits are significant, so if a user enters "123", the application needs to be smart enough to pad out the other 97 digits with leading zeros. Sane people might think this should be implemented as a one-line call to a built-in method. The more DIY among us might waste time building up a for loop. And, of course, a LISP fan would simply torture future coders with recursion and parentheses.

Then there's this approach that Brian found:


1's and 0's

by in CodeSOD on

Consider "0010000000100000". It's a string filled with nothing but "1" and "0" characters. Now, unless such a string is part of some classroom assignment where the goal is to programmatically convert Based 2 to Base 10 — or, perhaps, existing in some highly-limited and/or perverted language like MUMPS — there is never a good reason for it to exist in a program.

Because there are so many more appropriate data types — a boolean array, an integer bitmask, or even an integer array, just to name a few — a "boolean string" is a canary in the coalmine. If it's in an application's codebase, then chances are, there's something seriously wrong with that code. If, perchance, such a string is found in a VARCHAR column in some database, then something isn't just seriously wrong, the application is nothing short of an epic disaster.


IsListconntSmalleThaOne

by in CodeSOD on

If Peter Moberg were to give a single criticism of his colleague's work, it would be that it represents a complete and total misunderstanding of the principles of software development.

Criticisms #2 and #3 would probably center around typos/misspellings and completely superfluous code. Today's example illustrates all three.


Well-Formed XML

by in CodeSOD on

Diego was excited. A vendor — one that, for the record, he had no choice but to work with — finally put together a web service. This was big news because it meant that Diego would no longer have to battle proprietary TCP protocols to get data extracts; he could use a simple, well-established protocol to download the data he needed.

When the vendor delivered the service documentation, however, Diego was a little disappointed. It was a little more... simple... than he had imagined.