CodeSOD: A Case of Bad Timing


Although I’ve retired from full time work, I still consult for lots of small mom-n-pop places. Mostly, it’s little scripts to automate doing this and that. Sometimes, the boss’ kid or nephew was asked to get ambitious and solve a problem. When the inevitable happens, they call me to bail them out.

For the most part, it’s usually something like some file got moved/renamed/deleted. Sometimes, they got ambitious and attempted to write a batch file. This time, a college freshman, who claimed to be “good with computers”, had written a program to control the little scripts and jobs in an automated fashion. Apparently, it was getting too complicated for him and they asked me if I could work with it.

It’s a pity that Windows doesn’t have some sort of way to run a task on a schedule…

Anonymized, but structurally unmodified, and no, there wasn’t a single comment in the file:

public class TaskScheduler {

    public void runTask(int taskNum, int ...args) throws Exception {
        switch (taskNum) {
            case 1: function1(args[0]);
                    return;
            case 2: function2(args[0],args[1]);
                    return;
            case 3: function3(args[0],args[1],args[2],true);
                    return;
            case 4: function3(args[0],args[1],args[2],false);
                    return;
            case 5: runTask(2,args[1]+1); return;
            case 6: runTask(3,args[1]+1,args[2]+1);
                    runTask(5,args[1]);
                    return;
            // OP: triple-nested switch meaning: "Run only during business hours: 9-5, M-F, with special case on Wed"
            case 7: switch(new GregorianCalendar().get(Calendar.DAY_OF_WEEK)) {
                      case 0: return;
                      case 1:
                      case 2:
                      case 3: runTask(3, 5, 8);
                      case 4:
                      case 5: {
                                int hourOfDay = new GregorianCalendar().get(Calendar.HOUR);
                                runTask(6, hourOfDay, 23);
                                switch (hourOfDay) {
                                    case 0:
                                    case 1:
                                    case 2:
                                    case 3:
                                    case 4:
                                    case 5:
                                    case 6:
                                    case 7:
                                    case 8: return;

                                    case 9:
                                    case 10:
                                    case 11:
                                    case 12:
                                    case 13:
                                    case 14:
                                    case 15:
                                    case 16:
                                    case 17: runTask(2, args[1]);
                                             return;

                                    case 18:
                                    case 19:
                                    case 20:
                                    case 21:
                                    case 22:
                                    case 23: return;
                                    default: return; // OP: I suppose that 25'th hour *could*
                                                     //     fall during business hours!!!
                                }
                                return;
                              }
                      case 6:  return;
                      default: return; // OP: in case of days outside the range: Sun..Sat
                    }
            //...
            case 184: { function184(new Date());
                        runTask(1);
                        runTask(27);
                        runTask(16, 1, 15, 34);
                        // OP: 84 more
                        return;
            }
            default: return;
        }
    }
    
    // all renamed for anonymity, but they were about this meaningfully named

    void function1(int arg) {
        // ...
    }
    void function2(int arg1, int arg2) {
        // ...
    }
    void function3(int arg1, int arg2, int arg3, boolean arg4) {
        // ...
    }
    // ...
    void function184(Date d) {
        // ...
    }
}

[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/2wIhOot

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

%d bloggers like this: