lundi 9 juillet 2018

Code style: Reuse of a control expression or control variable in the branch of a switch/if statement

Recently, I've found a piece of code which looks like this.

switch(type)
{
    case TYPE1:
        doSomething1(type, arg1);
        break;
    case TYPE2:
        doSomething2(type, arg1, arg2);
        break;
}

Why don't just pass explicitly those enums (TYPE1, TYPE2) instead of the type argument? Like in the following example.

switch(type)
{
    case TYPE1:
        doSomething1(TYPE1, arg1);
        break;
    case TYPE2:
        doSomething2(TYPE2, arg1, arg2);
        break;
}

I understand that passing the type argument to a function is OK when you have a piece of code/functions which uses this variable in a default branch, or in multiple merged cases (pass through). But in the above example it seems to be useless.

What do you think about that? Is there any advantage of such convention?

Aucun commentaire:

Enregistrer un commentaire