CodeSOD: The Strangelet Solution


Chris M works for a “solutions provider”. Mostly, this means taking an off-the-shelf product from Microsoft or Oracle or SAP and customizing it to fit a client’s specific needs. Since many of these clients have in-house developers, the handover usually involves training those developers up on the care and maintenance of the system.

Then, a year or two later, the client comes back, complaining about the system. “It’s broken,” or “performance is terrible,” or “we need a new feature”. Chris then goes back out to their office, and starts taking a look at what has happened to the code in his absence.

It’s things like this:

    var getAdjustType = Xbp.Page.getAttribute("cw_adjustmenttype").getText;

    var reasonCodeControl = Xbp.Page.getControl("cw_reasoncode");
    if (getAdjustType === "Short-pay/Applying Credit" || getAdjustType === "Refund/Return (Credit)") {
        var i;
        var options = (Xbp.Page.getAttribute("cw_reasoncode").getOptions());
        reasonCodeControl

        for (i = 0; i < options.length; i++) {
            if (i <= 4) {
                reasonCodeControl.removeOption(options[i].value);

            }
            if (i >= 5) {
                reasonCodeControl.clearOptions();

            }
            if (i >= 5) {
                reasonCodeControl.addOption(options[5]);
                reasonCodeControl.addOption(options[6]);
                reasonCodeControl.addOption(options[7]);
                reasonCodeControl.addOption(options[8]);
                reasonCodeControl.addOption(options[9]);
                reasonCodeControl.addOption(options[10]);
                reasonCodeControl.addOption(options[11]);
                reasonCodeControl.addOption(options[12]);
                reasonCodeControl.addOption(options[13]);
                reasonCodeControl.addOption(options[14]);
                reasonCodeControl.addOption(options[15]);
                reasonCodeControl.addOption(options[16]);
                reasonCodeControl.addOption(options[17]);
                reasonCodeControl.addOption(options[18]);
                reasonCodeControl.addOption(options[19]);
                reasonCodeControl.addOption(options[20]);
                reasonCodeControl.addOption(options[21]);


            }
        }
    }
    else {
        var options = (Xbp.Page.getAttribute("cw_reasoncode").getOptions());
        for (var i = 0; i < options.length; i++) {
            if (i >= 4) {
                reasonCodeControl.removeOption(options[i].value);

            }
            if (i <= 4) {
                reasonCodeControl.clearOptions();

            }
            if (i <= 4) {
                reasonCodeControl.addOption(options[0]);
                reasonCodeControl.addOption(options[1]);
                reasonCodeControl.addOption(options[2]);
                reasonCodeControl.addOption(options[3]);
                reasonCodeControl.addOption(options[4]);

            }
        }
    }

There are patterns and there are anti-patterns, like there is matter and anti-matter. An anti-pattern would be the “switch loop”, where you have different conditional branches that execute depending on how many times the loop has run. And then there’s this, which is superficially similar to the “switch loop” anti-pattern, but confused. Twisted, with conditional branches that execute on the same condition. It may have once been an anti-pattern, but now it’s turned into a strange pattern, and like strange matter threatens to turn everything it touches into more of itself.


[Advertisement] Release!
is a light card game about software and the people who make it. Play with 2-5 people, or up to 10 with two copies – only $9.95 shipped!

http://ift.tt/2xA4s0i

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s