Thursday, November 2, 2017

CodeSOD: Switching the Search

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!


To read the full article, please visit The Daily WTF

No comments:

Post a Comment