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.

Mar 2009

Technically, It's Still GIF Animation

by in CodeSOD on

I'll be honest. Code submissions come into our queue sometimes and I have to read them a few times before I can identify the WTF. When Alex asked me to write this one up, I had to read it over line-by-line for almost an hour and figure out what was wrong with it.

This was sent in anonymously, with the note that it's designed to display a pretty, spinning "now loading" image while a report is created.


Just a Small Change to the Invoice

by in CodeSOD on

When Yaniv started at his new job, he was replacing a developer who had quickly left for undisclosed "personal" reasons. Bypassing any training, his first assignment was meant to be an easy hands-on task where all he needed to do was to adjust the comments on the company's invoice over just a little bit so that it didn't spill over the edge of the paper.

"None of our customers have ever really complained about it, but we do get the occasional call where the CSR has to read the notes section of their order to confirm what we entered," said Yaniv's manager, "I figure that you'll just open the template for the invoice, change the field width, and be done!"


Where the Wild Web Things Are

by in CodeSOD on

"Working for Opera Software's QA department gives you in-depth perspectives on the web's wild and varied coding practises," writes Hallvord R. M. Steen. "I still wasn't prepared for the curious solutions that power the menu on the new Israel Railways website."

"The coding is unbelievable," Hallvord continues. "Diving into the website's source code shows that its coders must have fallen asleep during the what's the point of XSLT lesson. It's more like an XML parser/serializer stress test than a production site."


Code Ownership Gone Awry

by in CodeSOD on

"Here at Company X," Jay Smith writes, "we have a senior programmer who, through the years, has developed a terrifying combination of complete code control issues and an image with the owners that makes his decisions right (despite real life implications)."

"This is the programmer who hates to use freely available libraries like STL, boost, or any third party API that could make his (and his team's) world simpler. He rewrites data structures per project, drags around a custom XML parser when others would suffice, reinvented the wheel so many times he may actually may own the patent by now. He has such code ownership problems, if you propose a solution that doesn't involve his code, hissy fits ensue complete with resultant silence treatment.


Try Doing That with a Field

by in CodeSOD on

Years ago I was talking to a friend who was just starting to learn about coding. He'd looked at some of my code, and asked why I used class properties with getters and setters instead of public fields. "Isn't it just more code for no real benefit?" he asked. And he was right; in every case, I had a getter and setter simply acting as a public interface to a private field.

I thought about his question for a second, not really knowing the answer. Frankly, it was just The Way I Learned To Do It. And to my dismay, he didn't just go away while I struggled to think of an answer. I eventually waved my hands dramatically and yelled "encapsulation!" Yes, that'll do nicely, I thought. Later that day I was thinking about it some more, and realized that in most cases, he was right. Still, getters and setters are great for inherited classes in which a method of calculation may be different. Accessors certainly have their place, but that place isn't necessarily everywhere.


Bulletproof JavaScript Detection

by in CodeSOD on

"I work at a major financial services company," writes Seth. "We have a public site that requires JavaScript be enabled. When it's not, the visitor is redirected to an error page that explains the website's requirements."

Definitively determining whether someone has JavaScript turned on or not can be a pretty challenging task, but fortunately, Seth's colleagues developed a bulletproof method to figure this out. They used JavaScript.


The Quest for the Unique ID

by in CodeSOD on

When Yohan J. heard that the invoice generation code had started to run slowly, alarm bells sounded in his head. The invoicing system hadn't been touched in a long while, no business process had changed, no new outside interfaces, no hardware changes, no logical reason that it wouldn't be running as quickly as it always had. In fact, this was just the step that generated the invoice; not invoice items, just creating a draft invoice. A single insert that would produce a single row.

Yohan started by checking the invoice table to get a spread of invoices over the past few months to see if volume had increased substantially. It hadn't. Though, oddly, the six-numerical-digit invoice numbers were all over the board. He spotted an 000015 and a 702438 that were created within hours of eachother. How is it even generating these? he wondered, expecting to find a constraint on the column containing an algorithm to generate the invoice numbers.


Using Design Patterns...or not...

by in CodeSOD on

Recently, the anonymous submitter of the following code had been mentoring novice developers in the use of design patterns.  I'm sure one of the things he stressed was creating a reusable template for how to solve a problem that can be used in many many situations. All good stuff.

Well, the submitter took a peek at the work of one of the developers and felt that the author of the below code may need some coaching on what "reuse" means.

case SOME_CONST1:
        _panelInstance  = DetailPanelFactory.getInstance(type);
        _panelInstance.parseXML(panelList);
        // add the new panelItem to the panelCollection
        _panelCollection.addItem(_panelInstance);
break;

case SOME_CONST2:
        _panelInstance  = DetailPanelFactory.getInstance(type);
        _panelInstance.parseXML(panelList);
        // add the new panelItem to the panelCollection
        _panelCollection.addItem(_panelInstance);
break;

case SOME_CONST3:
        _panelInstance  = DetailPanelFactory.getInstance(type);
        _panelInstance.parseXML(panelList);
        // add the new panelItem to the panelCollection
        _panelCollection.addItem(_panelInstance);
break;

case SOME_CONST4:
        _panelInstance  = DetailPanelFactory.getInstance(type);
        _panelInstance.parseXML(panelList);
        // add the new panelItem to the panelCollection
        _panelCollection.addItem(_panelInstance);
break;
 

Formatting a Phone Number - The Long Version

by in CodeSOD on

Dear Daily WTF Readers,

Please, take a moment of your time to consider the plight of the poor 'for' loop and its sister 'case' in the below code which has chosen to remain anonymous as per its submitter.


Input Validation... The Clever Way

by in CodeSOD on

Question: when you're accepting input from a user, one should always:

  1. Ensure the data are valid before writing to the database
  2. Ensure the data are valid while writing to the database
  3. Ask the user to please not perform an injection attack since your system isn't designed to handle it
  4. Do it the clever way!

I'll leave that to you to determine the correct answer. One of Joshua S.'s colleagues, "Dave," chose D.


The Apocalypse Must Have Occurred?!

by in CodeSOD on

Recently, Jörg S came across a rather interesting trouble ticket:

___ The Apocalypse Must Have Occured?! ___
 Ticket ID   : 76831
 Created By  : Cary L----------
 Assigned To : Jörg S-----
 Priority    : Low

   Legal wants to make sure that we have some special language in for 
   the 2010 Q1 release, but when I try to add it to ComplianceTraq,
   I keep getting the same thing:

       ERROR: Please ensure the system clock is correct. And if 
       it is... then God help us all, because the apocalypse must
       have occurred due to Compliance bugs, and we are still 
       trying to fix them. Damn you, Compliance!

   I know times are tough these days, but does ComplianceTraq know 
   something I don't? I didn't miss the apocalypse, right?!?!