Last minute geek

last minute tech news from around the net

Sunday, Dec 17th

Last update03:08:00 AM

You are here: English WTF CodeSOD: Switching the Search

CodeSOD: Switching the Search

User Rating: / 0
PoorBest 

We return to Virginia N’s refactoring efforts (previously: here, and here).

This code is elegant in its stupidity, combining two anti-patterns in one glorious, “No, don’t do that, whyyyyyyyyyy!”. To wit, the for-switch, and the “build SQL statements through string concatenation”:

for (int i = 0; i < 16; i++)
{

    StringBuilder commandText = new StringBuilder();
    command = new OracleCommand();
    switch (i)
    {
        case 0:
                commandText.Append(searchbuyer);
                parameter = new OracleParameter(":BUYER_DEP", OracleDbType.Char, 2);
                parameter.Value = depBuyer;
                command.Parameters.Add(parameter);
                break;
        case 1:
                commandText.Append(searchStock);
                break;
        case 2:
                commandText.Append(searchArbo1+"E"+searchArbo2);

                break;
        case 3:
                commandText.Append(searchArbo1+"V"+searchArbo2);
                break;
        case 4:
                commandText.Append(searchArbo1+"M"+searchArbo2);
                        break;
        case 5:
                commandText.Append(searchArbo1+"P"+searchArbo2);
                        break;
        case 6:
                commandText.Append(searchArbo1+"S"+searchArbo2);
                        break;
        case 7:
                commandText.Append(searchArbo1+"O"+searchArbo2);
                break;
        case 8:
                commandText.Append(searchArbo1+"T"+searchArbo2);
                break;
        case 9:
                commandText.Append(searchArbo1+"A"+searchArbo2);
                break;
        case 10:
                commandText.Append(searchArbo1+"I"+searchArbo2);
                break;
        case 11:
                commandText.Append(searchDemandeur);
                break;
        case 12:
                commandText.Append(searchDoc);
                break;
        case 13:
                commandText.Append(searchForm);
                break;
        case 14:
                commandText.Append(searchCertif);
                break;
        case 15:
                commandText.Append(searchFormV);
                break;
    }
    //… use the query
}

And for bonus points, this takes a round trip to the database for every iteration of the loop, for 16 hits per search. EVMPSOTAI, indeed.

[Advertisement] Universal Package Manager - ProGet easily integrates with your favorite Continuous Integration and Build Tools, acting as the central hub to all your essential components. Learn more today!

Read all
Comment Policy:
We pre-moderate any comments and welcome all kinds of thoughts, supportive, dissenting, critical or otherwise. We delete or censor comments that are:

* abusive
* off-topic
* contain personal attacks, or against any company or organization
* promote hate of any kind
* use excessively foul language
* is blatantly spam or advertising

We do not discriminate based on the person who is posting, and we never censor comments for political or ideological reasons. We never delete an appropriate comment because we disagree with its viewpoint or ideology, and we never publish an inappropriate comment because we agree with or support its viewpoint or ideology.


Attention spammers: we manually approve all comments. Spamming and blatant advertising will NOT be published on this site and is deleted immediately, you've been warned, do not waste your time here.

Add comment

Security code
Refresh