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.

Dec 2008

Classic WTF: A Function to Quit For

by in CodeSOD on

A Function to Quit For was originally published on July 17, 2006


It's easy to understand how Mike Hartnagel got himself into the classic "should I quit after three days?" dilemma. Who would have guessed that a single bullet point on the job description -- "utilizing some Excel Interoperability" -- actually described the architecture of the system: a horrid amalgamation of Excel spreadsheets interacting with C# interacting with other spreadsheets. Who would have thought that all other bullet points -- from "multi-tiered architecture using remoting" to "automated/integrated build process" -- were actually features they'd like to have at some point in the distant future.


Classic WTF: The Challenges of Negation

by in CodeSOD on

It never ceases to amaze me the lengths that certain programmers will go to solve the simplest of problems. Like, say, negation.

When "n * (-1)" Won't do ...

Originally published on August 12, 2004.


Classic WTF: Now That's A Neat Trick

by in CodeSOD on

Now That's A Neat Trick was originally published on September 9th, 2006.


Brian was excited to join an organization that boasts managing data for "93% of all health care facility inspections." That's important stuff, after all: inspections are essential to maintaining the integrity of facilities and can make the difference between life and death of its patients. But after eight months working there, Brian now looks for facilities in the "other 7%" for his personal health care needs.


Classic WTF: Journey to the Center of the Database

by in CodeSOD on

Journey to the Center of the Database was originally published on June 5th, 2006.


It'll be a long while before Donnie will forget about March 5th, 2004. Early that morning (during one of the ungodly hours), Donnie received his first off-hours support call. Naturally, one of the "mission critical" applications (more specifically, one that Donnie knew only by name) was completely broken and needed to be fixed right away. With the primary and secondary support guys unavailable, Donnie was on his own.


The 8 Report and Other Gems

by in CodeSOD on

Peter Korsten used to work with a truly brilliant and exceptional individual. Before leaving for greener pastures and better pay, one of his former co-workers left an indelible mark on their system via his rather unique approach to programming.

The Great 8 Report

One of these artifacts was the "8" report which consisted of the following SQL query: 


Security by Posterity

by in CodeSOD on

When Rory was interviewing for his position he liked what he heard. A robust data access layer, and a company policy that dictated that if you get caught writing a non-parameterized query, you're tarred, feathered, and recommended for execution. Their rigidity and adherence to the practices, as well as an interviewer asking all of the right questions regarding secure coding and good design ultimately lead to his acceptence of the position.

Once he was finally in, he cracked open the code, excited to bask in the radiance of beautiful data access layers, secure code, and well-implemented design patterns.


Validating a Date with a Sledgehammer

by in CodeSOD on

I think that it goes without saying that handling freeform user input, no matter the programming language, can be a tricky endeavor.  Having to work with code like today's SOD makes me appreciate the pain felt by this bit of code Catherine Palmer inherited. 

Now, I won't say that this function is necessarily terrible code, but when you consider that the field that code is intended to handle demands (in big red text) the entry of a date in a specific format (DD/MM/YYYY to be exact) and that it's meant to prevent a user from entering a date in the future, it'll leave you wondering why "TONY B" chose this exact plan of attack.

<script language="JavaScript">
<!--
function validate_futuredate(userdate)
//--- THIS DATE VALIDATION WAS DESIGNED BY ----//
//---    TONY B------- ON 02/05/2000 -----//
{
//---- get user date ---------------//
dday = userdate.value.substring(0,2)
mmnth = userdate.value.substring(3,5)
yyear = userdate.value.substring(6,10)

//--- get system date ---------------
curdate = new Date()
curyear = curdate.getFullYear()
curday = curdate.getDate()
curmonth = curdate.getMonth()
curmonth ++
//--- add one dayto curmonth as function produces zero for january ---//

strday=String(curday).length
strmonth=String(curmonth).length
//--- check that system day and month length greater 1 if not insert zero ---//
if (strday == 1)
        {
      curday = "0" + String(curday)
      }
     
  if (strmonth == 1)
        {
      curmonth = "0" + String(curmonth)
      }  
//--- create todays date to write back to input box on user input error ---//    
ddate1 = curday + "/" + curmonth + "/" + String(curyear)

//--- get length and value of user input date ---//
posit = userdate.value
poslen = posit.length
//--- check date contains 10 characters ---//
  if(poslen != 10)
    {
    alert("date contains wrong number of characters default value set")
    userdate.value = ddate1
    userdate.focus()
    return (false)
    }
//--- get first character of date make sure its less than 4 ---//
posit0 = userdate.value.charAt(0)
if (posit0 < "0" || posit0 > "3")
    {
    alert("date contains invalid DAY default value set")
    userdate.value = ddate1
    userdate.focus()
    return (false)
    }
posit1 = userdate.value.charAt(1)

if (posit1 < "0" || posit1 > "9")
    {
    alert("date contains invalid DAY default value set")
    userdate.value = ddate1
    userdate.focus()
    return (false)
    }
   
  if (posit1 == "0" && posit0 == "0")
    {
    alert("date contains invalid DAY default value set")
    userdate.value = ddate1
    userdate.focus()
    return (false)
    }
   
  if (posit0 == "3" && posit1 > "1")
    {
    alert("date contains invalid DAY default value set")
    userdate.value = ddate1
    userdate.focus()
    return (false)
    }
   
posit5 = userdate.value.charAt(5)
posit2 = userdate.value.charAt(2)
if (posit2 != "/" || posit5 != "/")
    {
    alert("date missing forward / mark default value set")
    userdate.value = ddate1
    userdate.focus()
    return (false)
    }

posit3 = userdate.value.charAt(3)
if (posit3 < "0" || posit3 > "1")
    {
    alert("date contains invalid MONTH default value set")
    userdate.value = ddate1
    userdate.focus()
    return (false)
    }
posit4 = userdate.value.charAt(4)
if (posit4 < "0" || posit4 > "9")
    {
    alert("date contains invalid MONTH default value set")
    userdate.value = ddate1
    userdate.focus()
    return (false)
    }
if (posit4 == "0" && posit3 == "0")
    {
    alert("date contains invalid MONTH default value set")
    userdate.value = ddate1
    userdate.focus()
    return (false)
    }

if (posit3 == "1" && posit4 > "2")
    {
    alert("date contains invalid MONTH default value set")
    userdate.value = ddate1
    userdate.focus()
    return (false)
    }
posit6 = userdate.value.charAt(6)
if (posit6 != "2")
{
    alert("date contains invalid YEAR default value set")
    userdate.value = ddate1
    userdate.focus()
    return (false)
    }
posit7 = userdate.value.charAt(7)

if (posit7 != "0")
{
    alert("date contains invalid YEAR default value set")
    userdate.value = ddate1
    userdate.focus()
    return (false)
    }

posit8 = userdate.value.charAt(8)
posit9 = userdate.value.charAt(9)

if (posit8 < "0" || posit8 > "9" || posit9 < "0" || posit9 > "9")
    {
    alert("date contains invalid YEAR at position9/10 default value set")
    userdate.value = ddate1
    userdate.focus()
    return (false)
    }

if (dday == "31" && (mmnth == "04" || mmnth == "06" || mmnth == "09" || mmnth == "11"))
  {
alert("wrong number of days in specified MONTH default value set")
userdate.value = ddate1
userdate.focus()
    return (false)
    }
   
  if (dday > "28" && mmnth == "02" )
  {
alert("wrong number of days in specified MONTH default value set")
userdate.value = ddate1
userdate.focus()
    return (false)
    }
    if (yyear > curyear)
    {
    alert("future dates are not allowed")
    userdate.value =ddate1
    userdate.focus()
    return(false)
    }
else if (yyear == curyear)
{
    if(mmnth > curmonth)
    {
        alert("future dates are not allowed")
            userdate.value =ddate1
            userdate.focus()
        return(false)
    }
   
    else if (mmnth == curmonth)
    {
      if (dday > curday)
      {
      alert("future dates are not allowed")
          userdate.value =ddate1
          userdate.focus()
        return(false)
      }
    }
}
 
return(true)
}
//-->
</script>
 

#ifdef WTF

by in CodeSOD on

Something gives Dave the idea that several of these #ifdefs shouldn't actually be used...

      1 #ifdef BOGUS
      1 #ifdef BRUT_HAS_A_BUG
      1 #ifdef BUG92283
      2 #ifdef commented_out
      3 #ifdef DIRECTIO_ON
      1 #ifdef DONT_DELETE_PER_BOB
      1 #ifdef DONT_DO_THIS
      1 #ifdef DONT_LOG_IF_OK
      1 #ifdef HAS_NAMESPACES
      3 #ifdef HIDDEN
      1 #ifdef needed
      4 #ifdef NOTDEF
      1 #ifdef NotDefined
      1 #ifdef NOTDEFINED
      1 #ifdef NOTDEFINED
      2 #ifdef NOTNEEDED
      2 #ifdef NOT_NEEDED
      1 #ifdef NOT_REQUIRED
      1 #ifdef NOT_USED
      1 #ifdef NOT_USED_DELETE
      9 #ifdef NOTYET
      1 #ifdef NOTYET_NEEDED
      1 #ifdef old
      1 #ifdef OLDWAY
      1 #ifdef OLD_WAY
      1 #ifdef REAL_DELETE
      1 #ifdef SLEEP_DOESNT_WORK
      1 #ifdef TESTING_ONLY
     14 #ifdef TOO_MUCH_DEBUG
      2 #ifdef TOO_MUCH_INFO
      1 #ifdef TOO_MUCH_LOGGING
      1 #ifdef USE_OBSOLETE_CODE
      1 #ifdef USE_OLD_VERSION
      5 #ifdef USE_QUANTIFY
      7 #ifdef USE_STDIO_FILE
      1 #ifdef use_this
      1 #ifdef USE_TIME
      1 #ifdef UTIL_GLOBAL

Thirty X's

by in CodeSOD on

In today's fast-paced world of cell phones, Sega Nomads, and turn signals, we don't have time for anything. That's why we have acronyms. Just try to read until the end of this: International Business Machines Corporation. Congratulations if you're still awake – the rest of us non-MENSA types will keep referring to it as IBM.

At Chris N.'s employer, they needed the ability to look up acronyms' meanings, so one of Chris's predecessors got cracking on a solution. Not one to buy into that whole normalization thing, he created a table like the following:

ROWID ABBR_CDE ABBR_TXT
867 IBM INTERNATIONAL
868 IBM BUSINESS
869 IBM MACHINES
870 IBM CORPORATION

The Sky's the Limit!

by in CodeSOD on

This code was sent to John W. by the support staff of one of the larger software vendors with the stated purpose of determining how much memory a program could use on one of the corporation's Unix servers. 

#include <stdio.h>
#include <malloc.h>
 
int
 
main ()
{
float f;
char *p;
 
   while (1)
   {
      p = malloc(102400);
      if (!p)
         {
          printf ("malloc returned NULL\n");
          exit(1);
         }
  
      f += 102400;
      printf ("%g\n", f);
      printf ("%g %f %f MB\n", f, f, f/1024000);
  
   }
}

The Long Way toUpper

by in CodeSOD on

Have you ever found yourself writing a function to do something that seems pretty simple, then months or years later you find out there's a built-in function to accomplish exactly what you were doing? Maybe you didn't know where to look or the built-in function's name was confusing. I'd argue that Java's toUpperCase() does not fall in this category.

Somehow I imagine that if you were to reverse engineer the built-in method, it wouldn't look like this implementation (submitted anonymously):