36

It was friday evening and almost everyone in office had left. I was assigned a bug related to some of my code changes. I called my senior to help me debug (has three years of experience, whereas me having only one year exp, who is also a very good friend of mine *always helps in debugging*).
So the code goes

switch (someEnum) {
case One:
doSomething()
// no break
case Two:
t.x = someEnum
break
case Three:
.....
}

I had recently added new enun One and was reciting the code logic to him as we were looking through code.
Him: Hey you haven't set t.x in case One. How did you miss that?
Me: No look, I haven't but a break on it. It will go ahead and set it in next case.
Him: What are you talking about? if the someEnun is One why would it execute Two case. Lets copy that line up there and try it locally.
Me: No no no wait. Are you saying that groovy doesn't need breaks in switch (Me being new to groovy but good with Java).
Him: Why would you need break in switch case even in Java?
Me: *stares at him*
Him: I'm going to execute a psvm right freaking now.
Me: *while he writes the psvm* Why did you think there were breaks in switch in any code?
Him: Shut up. *writes psvm code cursing me everywhere*
*executes code*
No way. Really??
Me: Tell me why do you think are there breaks in switch.
Him: I though they were to get you out of switch block and not execute the default block.
Me: So were you coding switch until now without breaks?
Him: I don't know man. I'm starting to doubt all the switches I have ever written.
Me: Anyway that's not the problem, so moving on.
*a while later*
Him: If a interviewer would ask me how would you rate yourself in Java. I would be like "Well I worked on various projects for 3 years in Java, but didnt know why we put breaks in switch. So you figure it out yourself."

One of the best moments in office.

Comments
  • 3
    DUDE. RULE ZERO OF SWITCHES.
    YOU NEVER, EVER, EEEEEVEEEEER OMIT THE BREAK, EVEN IF ITS PERFECTLY INTENTIONAL AND BEHAVES EXACTLY HOW YOU WANT IT TO.

    NEVER.
    EVER.

    the only exception is when you do
    case val1:
    case val2:
    actual code for both
    BREAK;
    cases continue...

    except this, NEVER EVER.
  • 1
    @Midnigh-shcode why tho
  • 0
    That was fun to read!
  • 1
    My personal rule #0 for switch statements: don't use them unless absolutely necessary. Too much incidental complexity.
  • 0
    @AL1L
    0. because that's how it should have been designed in the first place. the way it is is basically a branching statement that executes through all the valid leafs unless you explicitly say otherwise, which is stupid. switch should be "branch according to value". if you want "execute if all blocks for which condition is true", we have different constructs for that which are explicit about this. you know, such as series of CONDITIONS.

    1. haven't you read the OP?
  • 0
    @nhll not a good rule, because they are never absolutely necessary.
  • 1
    @Midnigh-shcode exactly!
  • 0
    @nhll =D nice one, got me.
Add Comment