mardi 1 mars 2016

Switch or else if statements - what is more clear in C++

As in title. There are a lot similar questions but I will give different example: I have 2 enums

enum A
{
  A_ONE,
  A_TWO
};

enum B
{
  B_ONE,
  B_TWO
};

What is more clear switch by enum A and then in all cases switch by enum B?

A type1;
B type2;

switch(type1)
{
case A_ONE:
    switch(type2)
    {
    case B_ONE:
       //statement1
       break;
    case B_TWO:
       //statement2
       break;
     }
     break;
case A_TWO:
     switch(type2)
    {
    case B_ONE:
       //statement3
       break;
    case B_TWO:
       //statement4
       break;
     }
     break;
}

or using else if

if(type1 == A_ONE && type2 == B_ONE)
    //statement1
else if(type1 == A_ONE && type2 == B_TWO)
    //statement2
else if(type1 == A_TWO && type2 == B_ONE)
    //statement3
else if(type1 == A_TWO && type2 == B_TWO)
    //statement4

Which is better practice? What do you preffer

Aucun commentaire:

Enregistrer un commentaire