Page 1 of 1

To break or not to break

Posted: Tue Feb 20, 2007 9:42 am
by Kadanis
Just a curiousity about how people see this. I've put it in here as I think of this more as a design thought and not a "How do I do this?"

In the past I have always used break on a switch statement. Each case being handled separately and a default being left at the end for exceptions.

Today I came across a different scenario, and in this example by leaving out the break; it will save me lines of duplicate code. At least I think it will.

Code: Select all

switch ($action){
   case 0: #new file
      //do something
   break;

   case 1: #edit file
      //do something
   break;

   case 2: #create a default file
      //set up variables as if a new file had been created but using default values

   case 3: #save the file
      //run save routines, that are needed to create any file.
   break;
}
This is the general idea. I assume that if $action = 2 then the code will run to set up a default file, then will also run the Save routines and therefore save me having to write them all as part of the case 2: statement as well as the case 3 statement.

I was wondering if this is a good/bad coding practice. A colleague thinks it could be shaky if someone else edits the code and put something between case 2 and case 3, but I think with suitable commenting a decent programmer would understand whats been done.

What are your thoughts? Comment?

Posted: Tue Feb 20, 2007 9:45 am
by feyd
I use the fall-through abilities of switch all the time. :)

Posted: Tue Feb 20, 2007 9:47 am
by Jenk
switches will continue to progress down the 'ladder' of commands until either the break keyword, or the end of the switch.

PHP.net has a good example of this:
http://us3.php.net/switch


edit: and I've got the wrong end of the thread, sorry.

Posted: Tue Feb 20, 2007 9:52 am
by onion2k
As Feyd says, it's quite a common thing to do. It's very useful. The only real danger is someone coming along and thinking "there should be a break; in there" and adding one. Leave a comment explaining why it's missing if the code might be maintained by someone else.

Posted: Tue Feb 20, 2007 9:53 am
by Mordred
Yep, just put a comment for clarity:

Code: Select all

case 2: #create a default file 
      //set up variables as if a new file had been created but using default values 
//ATTN: break intentionally missing, because ....
case 3: #save the file
    //run save routines, that are needed to create any file. 
break;

Posted: Tue Feb 20, 2007 9:58 am
by Kadanis
Cool. Well thanks for the input. I'm going to go with it, just with comments as mentioned/suggested.

Doing it this way saves the need for duplication around 20+ lines of code so I'm happy. :D

Re: To break or not to break

Posted: Tue Feb 20, 2007 10:21 am
by timvw
I wonder why you use hardcode 0, 1, 2 etc...

eg:

Code: Select all

define('NEW_FILE', 0);
define('EDIT_FILE', 1);
...

switch($action) {
 case NEW_FILE: #new file comment is redundant 
   break;
}

Posted: Tue Feb 20, 2007 11:26 am
by Kadanis
lol,

i see your point. actually the example was breifly thrown together for this thread. its not a copy and past of the code.

although i didn't use the define method you suggested the case values are more readable. just didn't think of it when i was writing the post

:)