All Coded Smorgasbord

Inspired by the Pop-up Potpourri, the examples presented here aren't necessarily "bad" code nor do they imply the miserable failure that we're all used to reading here. The examples are more-or-less fun snippets of code like ones that we've all written at one time or another.

16 Jul 2014

A Priestly Epoch, Running off the Tracts

by Remy Porter in Coded Smorgasbord on 2014-07-16

Peter came across this helpful little utility function:

public static DateTime EPOCH_UTC()
{
   DateTime epoch = new DateTime(1980, 1, 1, 0, 0, 0);
   return epoch;
}

68 Comments - Last Comment @ 20:34
02 Jul 2014

Everyone Wants Validation

by Remy Porter in Coded Smorgasbord on 2014-07-02

Faibbus works with an international team. Some of his fellow developers don’t quite adhere to the same standards. This code was in a module, and the comments and text were originally in Dutch.

if(this.value.length!=0){

   //because the greater/smaller signs are being escaped  

   if(this.value.length==1){alert('The year must have 4 digits.');this.focus();}

   if(this.value.length==2){alert('The year must have 4 digits.');this.focus();}

   if(this.value.length==3){alert('The year must have 4 digits.');this.focus();}

}

45 Comments - Last Comment @ 2014-07-17
01 May 2014

The Long Way

by Remy Porter in Coded Smorgasbord on 2014-05-01

Sometimes, a developer just needs to take the long way around. Sure, a line of code like DateTime StartTime = DateTime.Now looks simple and readable, but what happens if you want the StartTime variable to be not exactly now?

Craig’s co-worker figured out a better solution:

90 Comments - Last Comment @ 20:34
17 Apr 2014

Sweet Mysteries of Life

by Remy Porter in Coded Smorgasbord on 2014-04-17

When you read a lot of bad code, you start to get a sense of why the code exists. Often, it’s ignorance- of the language, of the functional requirements, of basic logic. Sometimes, it’s management interference, and the slavish adherence to policy over practicality. Other times, it’s just lazy or sloppy work.

And sometimes, the mysterious logic that gave birth to a WTF is just that- a mystery.

91 Comments - Last Comment @ 20:34
25 Feb 2014

Code that Works

by Remy Porter in Coded Smorgasbord on 2014-02-25

The first step to working with dates is to learn when to use the built in APIs. The second step is to learn how to do basic arithmetic, as Ron shows us.

if (wishDate.Hour < 10)
{
   while (wishDate.Hour < 10)
   {
       wishDate = wishDate.AddHours(1);
   }
}
else if (wishDate.Hour > 16)
{
   while (wishDate.Hour > 16)
   {
       wishDate = wishDate.AddHours(-1);
   }
}

121 Comments - Last Comment @ 20:34
05 Sep 2013

Comments, Errors, and Log Messages...OH MY!

by Mark Bowytz in Coded Smorgasbord on 2013-09-05

Dave wrote, "Found this comment dated from three years ago. I sure hope that vacation ended well."

//TODO: ask terry about this when he comes back from vacation
//row["VERSION"] = (decimal)row["VERSION"] + 1;
row.AcceptChanges();
row.SetAdded();

 

92 Comments - Last Comment @ 20:34
03 Jul 2013

Ternary Over a New Leaf

by Remy Porter in Coded Smorgasbord on 2013-07-03

Dimitry likes it when the bad code he finds can be sung aloud. This particular line can be sung to the tune of “Rule Britannia”.

for (var html in data["html"])
   $(data["html"][html][0]).html(data["html"][html][1]);

96 Comments - Last Comment @ 20:34
28 Feb 2013

A Simple Misunderstanding

by Remy Porter in Coded Smorgasbord on 2013-02-28

Some terrible code arises out of terrible business rules which no one truly understands. Some terrible code arises from laziness, sloppiness, or the need to just get it done now.

But there’s a special class of awful code that arises for a complete misunderstanding of how the language is supposed to work. That gives us things like the loop Rasmus found:

111 Comments - Last Comment @ 20:34
17 Jan 2013

Comments

by Remy Porter in Coded Smorgasbord on 2013-01-17

Comments are a vital part of making code readable, but they’re more than just documentation. They’re communication between one developer and another. They tell us what the code does, but also how we feel about it.

Scott found this, which conveys a most fervent hope for this block of code:

135 Comments - Last Comment @ 20:34
28 Nov 2012

The New TODO and More

by Mark Bowytz in Coded Smorgasbord on 2012-11-28

While wading through hundreds of disallowed implicit casts and such from turning option strict on for a large project, Tevildo came across this little gem.

Sort of like Visual Studio's comment tasks except potentially visible to the user. Who knows, it might actually get attended to if a user reports it as a bug.

127 Comments - Last Comment @ 20:34
21 Nov 2012

Code Comedians

by Remy Porter in Coded Smorgasbord on 2012-11-21

When it comes to bad code, everybody thinks they’re a comedian. Heck, look at us! Stupid programmer jokes are a game everyone can play, though, so let’s enjoy an evening at the Improv with some code comedians.

Brian sends us this enum, which I’m sure was very funny back in 2007.

68 Comments - Last Comment @ 20:34
05 Nov 2012

Truth or Sim

by Remy Porter in Coded Smorgasbord on 2012-11-05

A huge number of the bad code submitted to TDWTF is related to dates. This isn’t all that surprising- dates are very complex data structures with a vast number of possible representations and huge cultural variations.

I have no idea what the excuse is for all the bad code relating to booleans. Today, let's take a moment to explore some philosophy, and see if we can understand what truth really is.

110 Comments - Last Comment @ 20:34
11 Oct 2012

The Secret Squirrel Club, a Gun, and More

by Mark Bowytz in Coded Smorgasbord on 2012-10-11

"I guess the developer wanted to make make sure that all his bases were covered!" wrote Ryan.

/// <summary>
/// Returns True if the input string is 
/// null, empty, "undefined", or "null".
/// </summary>
/// 

98 Comments - Last Comment @ 20:34
09 Jul 2012

The Object Test, a New PI, and More SHEEIT

by Alex Papadimoulis in Coded Smorgasbord on 2012-07-09

"I've been trying to fix up the last project a colleague completed before heading for greener pastures," wrote Philip Tyre. "After finding this comment, I'm beginning to have bad thoughts about what I may find lurking around..."

//  Apparently, in at least on situation, a Statically declared public
//  variable will hold its value across different browser instances making calls to the
//  same web page.
//  For now, reset these variables on Page_PreInit.

196 Comments - Last Comment @ 20:34
14 May 2012

FAIL FAIL,FAIL FAIL,FAIL FAIL and More

by Alex Papadimoulis in Coded Smorgasbord on 2012-05-14

"We're had been using a manufacturer's web service, but started getting errors all of a sudden," wrote Peter Lindgren. "Something has really, really failed."

<StatusCode>InternalServerError</StatusCode>
<StatusDescription>Internal Server Error</StatusDescription>
<WebHeaders>
  <X-Backside-Transport>FAIL FAIL,FAIL FAIL,FAIL FAIL</X-Backside-Transport>
</WebHeaders>

185 Comments - Last Comment @ 20:34
09 Apr 2012

EvalToInteger, 'gumdrops', and More

by Alex Papadimoulis in Coded Smorgasbord on 2012-04-09

"I recently found this while going over some code in a Util library used by my project," Ian writes, "clearly, this method makes sure the job gets done."

public static int EvalToInteger(string statement) {
     string s = EvalToString(statement);
     return int.Parse(s.ToString());
}

86 Comments - Last Comment @ 20:34
16 Jan 2012

Schizophrenic Haiku Comments and More

by Alex Papadimoulis in Coded Smorgasbord on 2012-01-16

"I found a schizophrenic comment that either intentionally or unintentionally happens to be a haiku," wrote Ben Vanik, "svn blame says this single line is the work of 3 different people across 3 years of coding."

// should delete the temp files here, no cannot because, we havent read it in yet!

113 Comments - Last Comment @ 20:34
31 Oct 2011

Useless Functions, Extreme Naming, and More

by Alex Papadimoulis in Coded Smorgasbord on 2011-10-31

"We had a programmer in our company whose specialty was to write functions that embodied his own personal traits," Winston Muller, writes "do absolutely nothing and mislead everyone to think that something was done."

/// <summary>
/// Log the user off the system. The method returns true or false for success or failure
/// </summary>
public bool Logoff(string strUser)
{
  return true;
}

119 Comments - Last Comment @ 20:34
26 Sep 2011

SingleQautedString(), RemoveMeFunction(), Human-Friendly Dates, and More

by Alex Papadimoulis in Coded Smorgasbord on 2011-09-26

"Following the code-as-a-commodity principle," writes Jan, "our management team not only sends large projects overseas, but goes with the lowest bidder."

/**
 * @param str The string which must put into 2 Signle Quat
 * @return The single quated String
 */
public String SingleQautedString(String str) {
  return "'" + str + "'";
}

167 Comments - Last Comment @ 20:34
20 Jul 2011

Not Non-uninvisible, addfunk(), and More

by Alex Papadimoulis in Coded Smorgasbord on 2011-07-20

"I just inherited a PHP application and discovered this," writes David Green. "I don't know what's worse: the fact that it says add but is performing subtraction, or that I can't find the accompanying addnoise() function."

public function addfunk($one, $two) {
    $val = $one - $two;
    return $val;
}

166 Comments - Last Comment @ 20:34
04 Apr 2011

Important-Function(), IsNumber(), and More()

by Alex Papadimoulis in Coded Smorgasbord on 2011-04-04

" "I found a function called 'Important-Function' in a colleague's PowerShell script," wrote B. Versteylen, "after expanding the function, I discovered this."

Function Important-Function()
{
#Hehehehehehe ^^
$logTime = Get-Date -Format "yyyy-MM-dd HH:mm:ss" #Just so it looks the same as a regular log entry :+
$randomNumber1Method = New-Object  System.Random
$randomNumber1 = $randomNumber1Method.Next(1,100) 
If ($randomNumber1 -EQ 30) 
	{
	$randomNumber2Method = New-Object System.Random
	$randomNumber2 = $randomNumber2Method.Next(1,15) 
	Switch ($randomNumber2) 
	{ 
     1 {Write-Host $logTime "Watching a colleague pick his nose..."}
     2 {Write-Host $logTime "Solving a binary sudoku puzzle..."}
     3 {Write-Host $logTime "Hoping to return anything useful..."}
     4 {Write-Host $logTime "Slapping random people with a trout..."}
     5 {Write-Host $logTime "Surfing porn sites on the internet..."}
     6 {Write-Host $logTime "Pondering the next move..."}
     7 {Write-Host $logTime "Formatting the hard drive..."}
     8 {Write-Host $logTime "Dividing by `$null..."}
     9 {Write-Host $logTime "Getting a cup of coffee..."}
    10 {Write-Host $logTime "Trying to fit a square into a circle..."}
    11 {Write-Host $logTime "Yawning as a cow races by..."}
    12 {Write-Host $logTime "Milking a goat..."}
    13 {Write-Host $logTime "Playing rock-scissors-paper..."}
    14 {Write-Host $logTime "Reading a manual on WMI..."}
    15 {Write-Host $logTime "Thinking about Jessica in Berlin..."}
	}
	}
}

129 Comments - Last Comment @ 20:34
20 Dec 2010

CompareObjectAsIAlertDocumentOrNullIfNotCastable and More

by Alex Papadimoulis in Coded Smorgasbord on 2010-12-20

"I found this line of code while doing some cleanup," Adam Aldrich writes, "this has to be the worst way to set a boolean variable to false."

bool memberHasFingerprint = bool.Parse(bool.FalseString);

151 Comments - Last Comment @ 20:34
29 Sep 2010

ISelfAware, Very Thorough, Crazy Hashmaps, and More

by Alex Papadimoulis in Coded Smorgasbord on 2010-09-29

Seeing that I somehow neglected to publish an article yesterday, I figured today would be a great day for a big ole smorgasbord of interesting code. Enjoy!


"I found the following code in our core architecture library," Steven writes, "I could make a smart-ass comment about needing a reference to get a reference, but I think the code speaks for itself."

285 Comments - Last Comment @ 20:34
19 May 2010

Convoluted toString, Interesting Comments, and More

by Alex Papadimoulis in Coded Smorgasbord on 2010-05-19

"Everyone knows that Russians are the best programmers, and the guy we recently hired is no exception," Kip DeMane wrote, "though his English is a little... off. Here are his comments from a recent commit."

r22795 | t-------- | 2010-05-10 16:06:35 -0400  | 1 line

cleaned code, modified function format_for_rtf . no more 
numbered key to access video array elements, renamed all 
camel toe vars, applied consistent coding formating

123 Comments - Last Comment @ 20:34
10 Mar 2010

In A Rush, Properly Handled, and More

by Alex Papadimoulis in Coded Smorgasbord on 2010-03-10

"This code was left by the Senior Software Consultant," Michael Wheeler writes, "I'm not sure if it's insurance against 'Return' not returning... or a comment that explained why the line of code was left in."

Public Shared Function GetItemFromValue(ByVal ddlControl As DropDownList) As Integer
    Dim i As Integer
    If ddlControl.Items.Count > 0 Then
        For i = 0 To ddlControl.Items.Count - 1
            If ddlControl.Items(i).Selected() Then
                Return i
                Exit For     'leaving this here cause we're in a rush
            End If
        Next
    End If
End Function

162 Comments - Last Comment @ 20:34
15 May 2009

Defensive Programming and a Whole Lot More

by Alex Papadimoulis in Coded Smorgasbord on 2009-05-15

"Some programmers like to program defensively," wrote Sam, "and then there's some of my coworkers. This is found at the top of nearly every function of our C++ classes."

if (!this) return false;

139 Comments - Last Comment @ 20:34
05 Sep 2008

A Smorgasbord of Classics

by Alex Papadimoulis in Coded Smorgasbord on 2008-09-05

It's a particularly busy week for me: on top of a few looming deadlines, I'll be at Business of Software 2008 in Boston. So, I figured it'd be the perfect opportunity to revisit some classics.

This article is a jumble of jumbles, pulled a handful of Coded Smorgasbord-style posts; this was definitely a fun series I'd like to bring back, so please do send in your "more-or-less fun snippets of code like ones that we've all written at one time or another."


109 Comments - Last Comment @ 20:34
24 Nov 2006

Coded Smorgasbord: Prepare For Return

by Alex Papadimoulis in Coded Smorgasbord on 2006-11-24

For more fun-but-not-necessarily-bad code, check out the previous episode: Coded Smorgasboard: Muhahahahaha


It's not that Thorsten doesn't have a sense of humor, it's just that finding this function (which simply processes a list of names and converts them to proper case) after hours of debugging several similarly named functions, it gets a little tiring ...

86 Comments - Last Comment @ 20:34
29 Sep 2006

Coded Smorgasbord: The Pilot Episode

by Alex Papadimoulis in Coded Smorgasbord on 2006-09-29

It's time for a new series: The Coded Smorgasbord. Inspired by the Pop-up Potpourri, the examples presented here aren't necessarily "bad" code nor do they imply the miserable failure that we're all used to reading here. Like previous "bunch o' code" articles, the examples are more-or-less fun snippets of code like ones that we've all written at one time or another. Enjoy!


Let's start things off with Miki Watts, who came across this line in some API examples provided by an ERP software vendor. J., we're all behind you on this: we hope it's not a bug, too ...

129 Comments - Last Comment @ 20:34
27 Jan 2006

A Collection Of Comments

by Alex Papadimoulis in Coded Smorgasbord on 2006-01-27

It's time once again to give all those cool-but-too-small-to-make-a-full-post submissions a home ...


Nick was disappointed that, despite all of GetGlobalTime()'s robustness, it didn't actually provide a method to apply the path integral transformation ...

109 Comments - Last Comment @ 20:34