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.

Jan 2013

Reusable Code

by in CodeSOD on

Code reuse is one of the key steps to maintainability. There are many ways a developer might make their code reusable. For example, Steve’s co-worker wrote this block, which generates 1000 log entries:

int next = 0;  

List<FeedSearchTransactionLogResult> allDataSimulated = new List<FeedSearchTransactionLogResult>();  

allDataSimulated.Add(new FeedSearchTransactionLogResult(next++));  
allDataSimulated.Add(new FeedSearchTransactionLogResult(next++));  
allDataSimulated.Add(new FeedSearchTransactionLogResult(next++));  
//SNIP skip 995 lines…  
  
allDataSimulated.Add(new FeedSearchTransactionLogResult(next++));  
allDataSimulated.Add(new FeedSearchTransactionLogResult(next++));  
allDataSimulated.Add(new FeedSearchTransactionLogResult(next++));

You’ve Got to Give to Retrieve

by in CodeSOD on

It was a dark and stormy night. The kind of night envisioned by Mr. Bulwer-Lytton when he penned his infamously bad opening sentence, but the weather was not a deterrent to Craig. Craig faced a task more daunting than mere lightning and rain. He was on the trail of an annoying bug and was determined to track it down come hell or high water (a possibility made more real given the weather).

How Craig came to this point is not unfamiliar to anyone who writes code. It was the last day of the sprint, in fact, it was more like the last hours of the sprint. He had been struggling mightily against the whims of programming fortune all day (and into the evening) trying to stamp out the final stubborn bugs in a feature that was his responsibility. A feature that, if it wasn’t delivered, would mean a failed sprint for the entire team.


De-Populate Dates

by in CodeSOD on

"That was call seventeen about the sales logger," said Jeff, "The dates are all mangled. Nothing’s getting logged. We need to escalate."

Tabitha, Jeff's supervisor, furrowed her brow. "Oh, we can’t escalate until we get eighteen complaints," she replied very matter-of-factly. Her sense of humor was so dry Jeff could never guess when she was kidding.


A Forest of Trees

by in CodeSOD on

Our system was written by neophyte troglodytes who didn't follow standards of any kind. They coded whatever they wanted, however they wanted, whenever they wanted, wherever they wanted. Usually via copy/paste/plagiarize.

In particular, we have a class called Tree. More accurately, there are 18 (yes, that's e-i-g-h-t-e-e-n) Tree classes; one each for: char, byte, short, int, long, float, double, Character, Byte, Short, Integer, Long, Float, Double, String, BigInteger, BigDecimal and Object[]. One might think: Hey! Let's use generics: Tree<T>. Nope! Each one is needlessly just a little bit different. Some implement the same methods with different types. Some have methods with completely different implementations to do the same thing. Some use completely different names for the same methods. Sometimes, the methods are identically named, and the parameters are the kind that can be auto-up-cast (e.g.: short -> int):


InsertSql()

by in CodeSOD on

Data-driven applications need to generate SQL from time to time. Usually, we leverage things like stored procedures or ORM tools to keep our code sane, but from time to time, we might hard code our SQL statements. You sacrifice some flexibility for some transparency into what your code actually does to the database.

Sometimes, however, writing SQL is just too darn readable. Adam’s co-worker has a better way:


Divide and Validate

by in CodeSOD on

In IT, what many developers mistakenly consider to be magic code or evidence of elite (aka "L337") hacker-type abilities actually turns out to just be some cleverly applied math concocted by a developer who chose to think outside of the box.

Take into consideration the following code that Piotr Kaminski submitted. I have no doubts that it validates user information just fine, but the fact that it does so by adding booleans and division is a new one for me.


Slow Down, You Click Too Fast

by in CodeSOD on

You know what really gets to me? People who open links too quickly. Haphazardly opening new tabs all over the place...it really grinds my gears.

Ok, I've never thought that, but apparently somebody did when they added the code that Jonathan Peppers found in an abstraction layer for System.Windows.Forms.WebBrowser.