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.

Jun 2013

We're Going to Need Another Guru!

by in CodeSOD on
Jonathan Mooney's office recently lost their resident SQL Server Reporting Services guru to greener pastures. As such, a general consensus began to take hold of the teams that nobody could ever hope to match his high-level abilities and that their organization as a whole was doomed.

This fear was confirmed after the first programming change arrived after the guru's departure. A relatively simple one really - allow for the addition of another parameter - "Overview". Unfortunately, the departed guru prided him self on his complex methods, as represented by the code below.

=SWITCH(
Parameters!View.Count = 1,IIF(Parameters!View.Value(iif(Parameters!View.Count=1,0,0)) = "Redemptions" ,false,true),
Parameters!View.Count = 2,IIF(Parameters!View.Value(iif(Parameters!View.Count=2,1,0)) = "Redemptions" or
Parameters!View.Value(iif(Parameters!View.Count=2,0,0)) = "Redemptions",false,true), Parameters!View.Count = 3,
IIF(Parameters!View.Value(iif(Parameters!View.Count=3,2,0)) = "Redemptions" or
Parameters!View.Value(iif(Parameters!View.Count=3,1,0)) = "Redemptions" or
Parameters!View.Value(iif(Parameters!View.Count=3,0,0)) = "Redemptions",false,true), Parameters!View.Count = 4,
IIF(Parameters!View.Value(iif(Parameters!View.Count=4,3,0)) = "Redemptions" or
Parameters!View.Value(iif(Parameters!View.Count=4,2,0)) = "Redemptions" or
Parameters!View.Value(iif(Parameters!View.Count=4,1,0)) = "Redemptions" or
Parameters!View.Value(iif(Parameters!View.Count=4,0,0)) = "Redemptions",false,true), Parameters!View.Count = 5,
IIF(Parameters!View.Value(iif(Parameters!View.Count=5,4,0)) = "Redemptions" or 
Parameters!View.Value(iif(Parameters!View.Count=5,3,0)) = "Redemptions" or 
Parameters!View.Value(iif(Parameters!View.Count=5,2,0)) = "Redemptions" or 
Parameters!View.Value(iif(Parameters!View.Count=5,1,0)) = "Redemptions"  or 
Parameters!View.Value(iif(Parameters!View.Count=5,0,0)) = "Redemptions",false,true), Parameters!View.Count = 6,
IIF(Parameters!View.Value(iif(Parameters!View.Count=6,5,0)) = "Redemptions" or 
Parameters!View.Value(iif(Parameters!View.Count=6,4,0)) = "Redemptions" or 
Parameters!View.Value(iif(Parameters!View.Count=6,3,0)) = "Redemptions" or 
Parameters!View.Value(iif(Parameters!View.Count=6,2,0)) = "Redemptions"  or 
Parameters!View.Value(iif(Parameters!View.Count=6,1,0)) = "Redemptions"  or 
Parameters!View.Value(iif(Parameters!View.Count=6,0,0)) = "Redemptions",false,true), Parameters!View.Count = 7,
IIF(Parameters!View.Value(iif(Parameters!View.Count=7,6,0)) = "Redemptions" or 
Parameters!View.Value(iif(Parameters!View.Count=7,5,0)) = "Redemptions" or 
Parameters!View.Value(iif(Parameters!View.Count=7,4,0)) = "Redemptions" or 
Parameters!View.Value(iif(Parameters!View.Count=7,3,0)) = "Redemptions"  or 
Parameters!View.Value(iif(Parameters!View.Count=7,2,0)) = "Redemptions"  or 
Parameters!View.Value(iif(Parameters!View.Count=7,1,0)) = "Redemptions"  or 
Parameters!View.Value(iif(Parameters!View.Count=7,0,0)) = "Redemptions",false,true), Parameters!View.Count = 8,
IIF(Parameters!View.Value(iif(Parameters!View.Count=8,7,0)) = "Redemptions" or 
Parameters!View.Value(iif(Parameters!View.Count=8,6,0)) = "Redemptions" or 
Parameters!View.Value(iif(Parameters!View.Count=8,5,0)) = "Redemptions" or 
Parameters!View.Value(iif(Parameters!View.Count=8,4,0)) = "Redemptions"  or 
Parameters!View.Value(iif(Parameters!View.Count=8,3,0)) = "Redemptions"  or 
Parameters!View.Value(iif(Parameters!View.Count=8,2,0)) = "Redemptions"  or 
Parameters!View.Value(iif(Parameters!View.Count=8,1,0)) = "Redemptions" or 
Parameters!View.Value(iif(Parameters!View.Count=8,0,0)) = "Redemptions",false,true)
)

Running for Free Memory

by in CodeSOD on

Jon checked the source control system and discovered a recent check-in by a new-hire touched nearly every file. The check-in comment read, “Performance enhancements; reduced memory usage.”

Runtime runny = Runtime.getRuntime();
runny.freeMemory(); //performance

/* Add the allocation level information */
TxnMaker tm = new TxnMaker(txnId, buySell, rate, taxFlag, isForeign, map);
xmlMessage.getTxns().addAll(tm.getTxns());
runny.freeMemory(); //performance

Log.writeTrace("Txns built");

TxnGroupMaker gm = new TxnGroupMaker(txnId, buySell, rate, spanishTaxFlag, isForeign, map);
Log.writeTrace("Txn Groups Built");

gm.smoothAllEconomicFields(tm.getAllocations(), map, isForeign);

xmlMessage.getTxnGroups().addAll(gm.getTxnGroups());
runny.freeMemory(); //performance

You Can't Spell "Date" Without "Database"

by in CodeSOD on

At Shaun's company, the fiscal month ends on the 18th. Nothing wrong with that, but there's plenty wrong with this Perl code that finds the most recent fiscal month-end:

my $date='0000-00-00';
my $dd = 1;
my $mm = 1;
while(substr($date,8,2) ne '18') {
    my $sel0="select date_format(date_sub(date_sub(now(),interval $dd day),interval $mm month),'%Y-%m-%d')";
    my $setup=$dbh->prepare($sel0);
    $setup->execute();
    ($date)=$setup->fetchrow_array();
    $setup->finish();
    ++$dd;
}

Pot o' Gold

by in CodeSOD on

Since the first caveman first discovered the concept of this is more valuable than that, the science of alchemy has captured the imagination of many an enterprising soul. Unfortunately, to date, nobody has had any real success in transforming worthless metal into gold. That was, until the wonder that is PHP came along...

Generally, Brad M, doesn't care who wrote what bad code as long as it gets fixed. This little PHP snippet however, made him look up the responsible programmer on Linked In:


SQL Injection: What's That?

by in CodeSOD on

Jon C. was planning to outsource a very simple order tracking system. He interviewed many prospects, each more hopeful than the last. He viewed samples of the websites they had built, and picked the one he liked the best. Jon then commissioned the job for his order tracking system to a local developer.

Upon delivery, Jon discovered that the email notification function didn't work, so he glanced at the code to see if he could identify the problem. Before Jon got that far, he discovered this on the login page:


A Bad Leg

by in CodeSOD on

“The Killer Robot program won’t run correctly.” An middle-school student beckoned Artyom to her computer in the lab.

“Let me have a look.” Artyom pulled one of the child-sized chairs and sat next to his student. He had given his class some educational, open-source C programs to try out. Killer Robot looked like good, text-based fun, according to the SourceForge description.


The Slow Down Loop

by in CodeSOD on

Arlen's manager was the kind of guy who always smiled, so when he stepped into the conference room full of developers without his trademark grin, everybody knew something was definitely wrong.

"The good news is that that Jones likes the revamped site since we uplifted that gnarled mess of ASP to PHP, but there's just one thing: he's not so much of a fan of the performance."