best practices - multiple functions vs single function with switch case

Posted by Amit on Stack Overflow See other posts from Stack Overflow or by Amit
Published on 2010-06-11T05:12:47Z Indexed on 2010/06/11 5:22 UTC
Read the original article Hit count: 381

I have a situation where I need to perform several small (but similar) tasks. I can think of two ways to achieve this.

First Approach:

function doTask1();
function doTask2();
function doTask3();
function doTask4();


Second Approach:

// TASK1, TASK2, ... TASK4 are all constants
function doTask(TASK) {
    switch(TASK) {
        case TASK1:
            // do task1
            break;
        case TASK2:
            // do task2
            break;
        case TASK3:
            // do task3
            break;
        case TASK4:
            // do task4
            break;
    }
}

A few more tasks may be added in future (though the chances are rare. but this cannot be ruled out)

Please suggest which of the two approaches (or if any other) is a best practice in such a situation.

© Stack Overflow or respective owner

Related posts about best-practices

Related posts about problem-solving