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.

Jan 2009

Whom Should I Say is Calling?

by in CodeSOD on

An Anonymous reader who is part of a team that works for a small company, maintaining a messy codebase of a large and expensive company writes, "We were having trouble finding why a particular PL/SQL Stored Procedure was failing. Inside the SP, we found the following:

IF .. AND p_program_name <> '[executable name].exe'
THEN   RAISE invalid_calling_pgm; END IF;

Delaying the Evitable

by in CodeSOD on

Wow, Paul thought to himself, "signing up for a new account takes a little while, huh? He was new on the job and clicking through to get a handle on the application he'd be maintaining. He opened the code to see what the signup button was actually doing behind the scenes.

The design was kind of strange; all HTTP requests were routed through Actions. Most Actions inherited from other Actions – for instance, LoginAction and SignupAction inherited from DelayableAction, which inherited from ActionBase.


self->Static Anti-Pattern

by in CodeSOD on

If someone comes up to you and says that they work in the video game industry you might think that he or she has an interesting job (and if you're a gamer, then it sounds they have an AWESOME job). Surely, in between marathon video game playing sessions, they would be working with some true geniuses on really cool programs instead of working with the regular schmoes maintaining the Batch Load Processing Report - right? Well, wake up from that dream, bucko.  The same bad coders from whom you inherited that insipid, "enterprise level" application get to make video games while you slave away for "the man".

Consider Andy Goth's submission.  After successfully integrating a flight simulator with an image generator and making it a sparkling gem of efficiency, maintainability, and clear documentation, he was given the task of documenting the high-level architecture and low-level implementation of which the following code is a part of.  In between writing Doxygen comments saying "This function does nothing" or "Use of this macro is a syntax error" or "Using this macro on the left-hand side of a multiplication will result in zero", considered if it would take longer to finish his documentation or ask management if he could throw away the old code and rewrite the entire application from scratch.


CommonUtils and the Inadequate java.lang.*

by in CodeSOD on

CommonUtils
"Right around Christmas time," Earl B writes, "I inherited a lovely Java project that contained a library class named CommonUtils. Wandering inside, I found something quite strange in the middle of quite a few other weird things."

public static Integer getIntegerValueOf(int value) {
        Integer result;
        if ( value == 0 ) {
            result = DomainConstants.INTEGER_0;
        } else if ( value == 1 ) {
            result = DomainConstants.INTEGER_1;
        } else {
            result = new Integer(value);
        }
        return result; 
    }

Longjmp - FOR SPEED!!!

by in CodeSOD on

For those who don't know, there is a standard C header called setjmp.h that defines two methods: longjmp() and setjmp(). The longjmp works just like a goto statement in C in that, when executed, the program jumps to another pre-defined point in the code, defined by a setjmp. However, where goto is limited to the current, local scope, longjmp can span outside of the current scope and across functions.

If goto is considered harmful, then longjmp() should definitely be considered dangerous. As in, thermo-freakin-nuclear bomb dangerous. Fortunately, its complexity and frequency of use seem to be on par with that of a nuke, and most C developers have not brought it in their everyday. Most. And then there are the folks like Giovanni Verza's predecessor, who left behind functions like this.

/* this subroutine is called thousands of times.
use longjmp instead of loops to increase speed.
Daren 12/03/05 */


void
calculate(struct salesinfo*sales){
        jmp_buf buffer;
        int i=setjmp(buffer);

        if (!(i<sales->count))
                RETURN_NOTHING;

        addvaluetosubtotal(sales->values[i]);

        if (i<sales->count){
                longjmp(buffer,i+1);
        }
}

Your E-tailer Hates You

by in CodeSOD on

You'd think that buying things online eliminates the human element of shopping in a retail store. And you'd almost be right.

Nolan heard about one of his company's "problem customers" from a friend of his in the customer service department. This customer was satisfied exactly as often as he was not right – never. If there was a coupon code for $5 off five items, he'd be calling to ask why it didn't work for one item. If they caved and gave him the discount, it'd only result in more calls demanding more and more discounts. If his order arrived on the last day of the estimated shipping date, he'd bitch and moan until the shipping cost was refunded.


Enterprise Level Access

by in CodeSOD on

Some time ago Martin F. was sent in by his IT consulting company to help fix some problems with the HR Database at a major European banking / insurance firm.  He admits that the WTF worthy warning signs were there at the onset (among them being that he was the 4th in a series of consultants assigned to this project), but being relatively naive to such things, he accepted the position and spent a year shaking his head in bewilderment and, at the same time, his fists at Rob.  He was an HR 'specialist' and a true IT genius who had a self-proclaimed hobby of programming in Visual Basic and was, of course, long gone from the corporation.

The HR Access-pool

The problem database held monthly snapshots of information about all the (over 50,000) employees of the bank, such as their names, DOBs, home address, function, fixed and variable salaries going back about 4 years.  Over that period of time, it had expanded to an impressive 2 Million records in size which is not unheard of in any large corporation, but your typical "Select and Group By" query on 200 people took about eight minutes.  However, as Martin came to discover, this was mostly due to the fact the corporate HR database was in reality an Access database sitting on some network fileshare.