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 2007

Absurdly High "Values"

by in CodeSOD on

Brian works for a web development shop with an all-too-common formula: lots of intern labor and a very high turnover. As a recent hire, a large part of Brian's job is maintaining old sites developed by various former employees.

"I was recently asked to investigate a bug," Brian wrote, "where selecting a few publications from a multi-list caused strange errors and didn't store them correctly. When I initially went to check on the error, I noticed absurdly high "values" assigned to the problematic publications..."


Hyperverbosity

by in CodeSOD on

When Andy G opened up libsymblogy.h (sic), he was relieved. Finally, some documentation! Of course, upon further inspection, Andy realized that the comments didn’t quite explain how the code works, how it interfaces with other modules, or even why it uses certain magic numbers like -13. Instead, every function had a comment just as hyperverbose and vapid as this…

// Description: Implementation of a C function having the special purpose of
// initializing three particular ex	ternal structure elements with arrays of
// stored values which define all symbol modifiers for a corresponding symbology
// datatype, i.e., airborne, land and naval platforms, sensors/emitters, etc.
// The function's input parameters represent the location of the stored data and
// the type of symbology the instructor/operator desires to view. The function
// operates on the presumption that these parameters are valid. The function
// first opens the stored data file and clears the appropriate symbol modifier
// structure depending on the given datatype.  Then the function populates three
// lookup tables with 1) stored indices destined for use as symbol modifiers,
// 2) stored ASCII representations of each symbol modifier row, and 3) stored
// ASCII representations of each symbol modifier column. The function's output
// parameters represent the filename, section, record and datum which was last
// read from the symbology datafile, if any. The function returns a byte
// value 0 when the desired symbol modifiers are successfully initialized.
// Otherwise, the function returns a byte value -13 if the symbology datafile
// doesn't contain the symbol modifier data

Overruled by RemoveSpecialCharsExceptQuoteAmpersand...

by in CodeSOD on

Finding bad code in some old system you’ve come to maintain is one thing. Being tasked with adding bad code to a new system is a whole other type of pain. Paul G was lucky enough to experience this first hand.

After an hour or two of arguing that there is a better way of doing this, Paul wrote, I was overruled by the longest serving developer and, as a result, forced to write the ‘Remove Special Chars Except Quote Ampersand Apostrophe Open Bracket Close Bracket Comma Hyphen Full Stop Comma Forward Slash’ function. Seriously.


Safety Critical Encapsulation

by in CodeSOD on

The idea behind “information hiding” – i.e. encapsulating certain methods and properties to users of a class – is a fair idea in theory, but in practice, it’s just too slow. Think about it. If a “public” method just ends up calling some “private” method, that’s just a waste of a call. And if a lot of calls are made to that public method, then that’s a whole lot of wasted calls!

Vlad’s predecessor knew this fact well when he was designing “safety critical” functions for his company’s ------ application. Instead of wasting precious CPU time on “information hiding”, he put in a simple request to users of his class…


A Little More Simplified

by in CodeSOD on

After Travis took his new position he was amazed to find that the developers that he was replacing were completely unaware of the Date/Time library in the .NET Framework. Instead the relied upon time spans and complicated regular expressions for simple tasks, such as validating a string as a date...


(click for full size)


Taking a Good Thing Too Far

by in CodeSOD on

If you're good at what you do, you can judge when to employ which design patterns. And, on the other hand, when employing said design patterns would make the means too complicated to justify the ends. Dale M. sent in an example of this sort of overcomplication:

Interface: 10 lines (4 Lines Of Code w/o comments)
     core/src/com/.../enterprise/util/codetable/IYesNo.java
Factory: 141 lines (69 LOC)
     core/src/com/.../enterprise/util/codetable/YesNoFactory.java
     (Note that the factory includes an inner class called YesNoInstanceType)
Abstract class: 43 lines (17 LOC)
     core-restricted/src/com/.../enterprise/util/codetable/restricted/YesNo.java
Concrete class: 87 lines (61 LOC)
     core-restricted/src/com/.../enterprise/util/codetable/restricted/nonpersistent/YesNo.java


Really Descriptive Names

by in CodeSOD on

You've probably encountered more than your fair share of inadequately descriptive variable and function names. Debugging a function with variables named a, a1, a2, a0, aO, al, a1l, al1, aIl, a1I, and al1I is something I wouldn't wish on my worst enemy.

On the opposite end, we have this code from an anonymous submitter:


Trouble Sort

by in CodeSOD on

Suppose you're using C# and you have a bunch of RSS data that you want to sort and put into a file. Think about how you'd approach the task.

You might consider a generic list. Maybe a DataTable and a DataView. Guilherme's colleague decided on (and invented) the Multi Array Bubble Sort technique.