6

Is this good practise ... ie, grouping the default case with another?

    switch ($cond){
            case 1:
                ...;
                break;
            case 2:
                ...;
                break;
            case 3:
            default:
                ...;
                break;
        }
4
  • 2
    Nothing wrong with doing it... nothing wrong at all. Commented Sep 12, 2011 at 16:12
  • Absolutely nothing wrong with that! Commented Sep 12, 2011 at 16:13
  • It is not a bad practice. Nothing more, nothing less. Commented Sep 12, 2011 at 16:13
  • A comment to indicate that falling-through is INTENDED would be good, but otherwise there's nothing wrong with it. Commented Sep 12, 2011 at 16:24

2 Answers 2

3

It makes perfect sense to do it that way.

Also, @Ian is correct, but in a limited scope. If you wanted additional functionality applied to case 3 you would leave it the way it is. As long as you don't break, it will go on to the next case.

Sign up to request clarification or add additional context in comments.

Comments

1

It kind of makes case 3 redundant though, so I'd remove it and just leave it as default

2 Comments

There are times when you want to put that in your code to show that case 3 might be needed later, but just has the default action now. It seems like a fine and helpful pointer to me.
Your answer certainly isn't wrong... but I disagree. Having case 3 in there lets the person looking at the code know that there is a case 3 which, for now, should act like the default case.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.