177

When someone uses a *switch* statement with only 2 cases

Comments
  • 28
    Coming from a working environment with plenty of on-the-run modifications and requests if there is even a tiny chance it will recieve more cases then switch it is. Easier to add/remove.
  • 7
    @andrasadam93 totally agree. Like for example, for an enum I would almost always use a switch in case the enum grows. Switch has also (in some languages) the benefit of warning you if you forgot some of the enum values! So when you add more values, warnings will pop up, which is great.
  • 9
    I'll tell you what's worse, I found in some live code

    Switch (var)
    {
    Case default:
    <code>
    Break;
    }
  • 3
    @Bramskyyy It's multiple if statements broken down into cases where the selected case is determined by the value of a switch variable.

    So it's multiple ifs without all the hassle.
  • 4
    @Bramskyyy it also exists in JS
  • 3
    @Bramskyyy
    switch(e){
    case 5:
    console.log ("e is 5");
    break;
    default:
    console.log ("e is not 5");
    break;
    }
  • 2
    @wbhob I though he was joking, but I provided an explanation anyway because I wasn't sure.

    No I see that all of us tought so.
  • 3
    Btw, a switch IS NOT equivalent to multiple IF statements. The compiled binary is much more efficient most of the times, like in the case of an enum. If you have N values, with ifs you will have to make N comparisons and N conditional jumps. With a switch you do one jump!
  • 0
    I would still like switch instead of if else.
  • 0
    @Joserc87 that's why i said 2 cases and not N cases
  • 1
    @0x41414141 even with 2 cases it's more efficient
  • 0
    @Joserc87 Yeah, but I didn't over analize, nor did I say that they were equivalent.
  • 1
    @Codebeard yeah yeah. I know. I'm just saying, I'm not attacking you
  • 1
    @Joserc87 No sweat guy, don't worry. 😊
Add Comment