282

I think I am tired.

I almost did a switch-case on a boolean.

Comments
  • 7
    Bro get some sleep
  • 3
    Damn, get some 10 hours of sleep...

    Out of curiosity:
    Why even use switches? For me theyre hard to read and serve no purpose.
  • 17
    @Strazdonis Because having 10 if for an enum is annoying to read to me
  • 3
  • 1
    Well, switch(true) works, it'll choose the case that is true..
  • 0
    Maybe you can do a switch case on a nullable Boolean.
  • 0
    @Strazdonis gonna need more than 2 hours of sleep...
  • 15
    By design switch performance is O1. If-elif tree performance -- o(n). Switch calculates the exact jump spot and sets eip immediatelly while if-elif does that using sort of a bruteforce.

    Also switch cases are quite often easier to read when used properly, because they rely on a constants.

    [someone above asked why use switches over ifs]
  • 0
    @Brosyl well, in checkboxes for example. Checked is true, unchecked is false, null is when no choice is made. You need a switch case to check those.
  • 2
    @netikras thanks a lot it is a lot clearer for me now! I've never seen a switch that was easy to read for me though :/
  • 0
    @Brosyl C#. But it appears to exist in Java as well, since version 8. In Typescript as well I think.
  • 0
    @DodgerAkame that's why you use polymorphism instead.
  • 1
  • 0
    You can do that in some languages (kinda), it’s called pattern matching. Check out Rust!
Add Comment