0

Don't forget to use 'break' in switch/case statements

ColdFusion

I was recently attempting to broaden my skillset a bit by reading a simple introductory book that is more or less the equivalent of "Java for Dummies".  In a section that talked about conditional logic, it made the statement that you should always remember to add a "break;" at the end of each case within a switch statement or the remaining conditions would be processed in after the matching one.

I was thinking at the time that I knew this was not how ColdFusion processed code using cfscript/cfcase, but I realized that I wasn't entirely sure how this was handled in cfscript.

I decided to run a quick test and found that it behaves identically to Java.  To set up a baseline for my test, I made a quick switch/case in tags like this:



As expected when I ran this, the string "second" appeared on the screen.  In atempt to do the exact same thing in script, only excluding the "break" after each case, I ran this:

Sure enough, it behaved exactly as the Java book had described that Java would handle this as the screen displayed: "secondthird".

So, understanding this behavior, is there something useful that could be accomplished by purposefully omitting breaks?   

 

tags:
ColdFusion
Rob Wilkerson said:
 
Actually, yes. They're definitely edge cases - at least in my experience - but they do exist and I know I've run into one or two in my time. If someone else doesn't chime in with a good example, I'll try digging through some of my old C code. Seems like that's where I remember using this technique.
 
posted 943 days ago
Add Comment Reply to: this comment OR this thread
 
Danilo Celic said:
 
Not sure how well it pertains to ColdFusion, but there is a loop optimization called Duff's Device that leverage's the "fall through" of not including the breaks. Here's a page that I've used with JavaScript on occasion: http://www.websiteoptimization.com/speed/10/">http://www.websiteoptimization.com/speed/10/

See listing 10-16, and 10-17.
 
posted 943 days ago
Add Comment Reply to: this comment OR this thread
 
Nolan said:
 
Acutally, yes there is something useful...

I'm currently building an app with a _very_ hierarchially driven data model. We have parent-child relationships between tables that go down about 10 or more levels (this is part of the business model, which is designed to mirror an industry standard so our code is compatible with other vendors).

In my case, I often need to delete "child data" at a certain level in my heirarchy. and the break-less "case" statements are perfect for this. Like so:

case "parent": delete_child();
case "child": delete_grandchild();
case "grand_child": delete_great_grandchild():
default: return false;

so if I pass in "parent", all of the "child records" below that will get deleted. But if I pass in "child", my "parent" stays valid, but eveyrthing below is still deleted, which is correct.

hth
Nolan
 
posted 943 days ago
Add Comment Reply to: this comment OR this thread
 
 
Nolan, that is a great example. Thanks!
 
posted 943 days ago
Add Comment Reply to: this comment OR this thread
 
Rob Wilkerson said:
 
Thanks, Nolan. Now I don't have to scrape up the courage to sort through C code that I'm not sure I understand any longer... :-)
 
posted 943 days ago
Add Comment Reply to: this comment OR this thread
 
Andy Matthews said:
 
You do have to include the break inside a cfscript based switch, but not a plain cfswitch.
 
posted 942 days ago
Add Comment Reply to: this comment OR this thread
 
Sam Farmer said:
 
This difference between cfscript and the cfswitch/cfcase tags caught me out once when I was demoing some code...

Anyhoo, JavaScript works the same way to cfscript which I often use in similar ways to Nolan's example. cfcase is a little different in that it can take in a list of values.
 
posted 942 days ago
Add Comment Reply to: this comment OR this thread
 
Rob Wilkerson said:
 
All the other languages I've used also take lists, but in a different format:

switch ( expression ) {
case 1:
case 2:
case 3:
/** do something if the value is 1, 2 or 3 */
break;
case 4:
/** do something if the value is 4 */
break;
}
 
posted 942 days ago
Add Comment Reply to: this comment OR this thread
 
 
@Andy, that about sums up what I was saying in one line. Thanks. :)
 
posted 942 days ago
Add Comment Reply to: this comment OR this thread
 

Search