25 October 2008

Bad Code

Every programmer has experienced it. Code that is so bad, you have to share it with someone. Here are some recent gems I have encountered.

Poor man's comments.
If 1 = 0 Then
End If
Is nameString empty?
if (nameString + "x" != "x") {
Hmm, is the comment wrong, or the code?
// Comma-separated list of numbers
string configurationIds = "254:762:2:236:23:5:21";


Germán said...

Worth a CodeSOD entry in www.thedailywtf.com

Quintesse said...

Well the first is actually a pretty normal thing to do in languages that have no block comments (like ASP web pages). You might think that it would be nicer to write "if false then" in that case, but it's far easier to turn the block of code on and off by changing just one number than by deleting "false" and typing "true" ;)

And the second can be found a lot in shell scripts where actual textual substitution of variables might be done before the code is parsed, so if nameString was empty the code would be turned into something like:

if ( != '')

which would be a syntax error. So it's normal to write something ugly as:

if (nameString 'x' != 'x')

because in most shell scripts you don't need the + for joining strings this is actually valid regardless if nameString is empty or not.

It might be that this is a shell programmer that went on to Java or C# but still has to unlearn some of his old habits ;)

Dustin Ted Whitney said...

the nameString one made me snort