To break or not to break

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
User avatar
Kadanis
Forum Contributor
Posts: 180
Joined: Tue Jun 20, 2006 8:55 am
Location: Dorset, UK
Contact:

To break or not to break

Post 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?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

I use the fall-through abilities of switch all the time. :)
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post 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.
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post 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.
User avatar
Mordred
DevNet Resident
Posts: 1579
Joined: Sun Sep 03, 2006 5:19 am
Location: Sofia, Bulgaria

Post 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;
User avatar
Kadanis
Forum Contributor
Posts: 180
Joined: Tue Jun 20, 2006 8:55 am
Location: Dorset, UK
Contact:

Post 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
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Re: To break or not to break

Post 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;
}
User avatar
Kadanis
Forum Contributor
Posts: 180
Joined: Tue Jun 20, 2006 8:55 am
Location: Dorset, UK
Contact:

Post 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

:)
Post Reply