To break or not to break
Posted: Tue Feb 20, 2007 9:42 am
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.
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?
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;
}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?