20

„Couldn‘t figure out how to detect the end of a case, forced every developer to end a case with a break; so it doesn‘t go through all cases“

- the guy who invented switch case

Comments
  • 12
    The end of one case is the next case or default. Easy.

    C's behaviour is because whoever came up with that idea was clearly thinking in assembly. Then again, all good C programmers think in assembly anyway so that it's no big deal.
  • 13
    @Fast-Nop sometimes you want a fall through though
  • 7
    @Codex404 yeah, but there is a design choice. Either fall through by default, which C does unless break is there, or break by default unless continue is there. Fallthrough by default is exactly what would happen with labels in assembly.
  • 1
    Well, in Pascal the Case statement does not fall through and in Swift, the switch does not fall through. Actually, it kinda says something... "Safety Belts"

    These are languages that are not as concerned with performance as they are concerned with safety for the junior programmer and keeping them from ripping their hair out over beginner bugs.

    Stacking a bunch of cases that match a single condition... or putting some "special cases" on top of a base case to leverage the fall-through is the natural way to think when working threads or pre-emptive interrupt work.

    As others have said, C thinkers think at the machine level... in assembly. Someone who writes in C stands a good chance of being very handy with a soldering iron. Someone who writes in Pascal or Swift probably could not tell you what a soldering iron looked like.
  • 3
    @xcodesucks GCC 7 and later has an interesting warning. If the case falls through and is not empty, a comment in certain format is required.

    When upgrading GCC, I got bitten by that one in conjunction with my usual -Werror because I obviously had a comment there, but that was from long before the GCC team decided on the recognised format.

    And I do have a soldering iron for doing electronics, you got me on that one. :-)
  • 2
    this is actually make sense if you ever programmed in Assembly, at least ARM
  • 0
    Ah, check out pattern matching in a ML-family functional programming language like SML, OCaml, F#, or Haskell

    It's glorious.
  • 2
    The fall through can be very useful
  • 2
    @Fast-Nop thanks for sparing me from writing these exact words myself. ++
  • 3
    Assembly is amazing, and case fallthrough is super useful. *nod*
Add Comment