Monday 14 July 2014

Odd: one cannot have a <cfbreak> within a <cfcase>

G'day:
This is an odd one. This morning I was looking at a StackOverflow question ("ColdFusion cfcase statements and referencing their variables?") which had me writing a <cfswitch> statement which had some compound logic in it which was best served by having an explicit <cfbreak> statement:



<!--- cfbreak.cfm --->
<cfparam name="URL.test" default="">

<cfset originalExpression = URL.test>

<cfswitch expression="#originalExpression#">
    <cfcase value="Test,Another,Yes,No">
        stuff common to all of TEST,ANOTHER,YES,NO<br>

        <!--- stuff specific to various cases --->
        <cfif originalExpression EQ "test">
            stuff specific to TEST<br>
            <cfbreak>
        </cfif>
        <cfif listFindNoCase("Yes,No", originalExpression)>
            stuff specific to YES,NO<br>
            <cfbreak>
        </cfif>
        stuff specific to ANOTHER<br>
    </cfcase>

    <cfcase value="otherCase">
        stuff specific to OTHERCASE<br>
    </cfcase>

    <cfdefaultcase>
        default case<br>        
    </cfdefaultcase>
</cfswitch>

Here's the odd thing: it seems <cfbreak> isn't supported inside <cfcase>:

Context validation error for the cfbreak tag.

The tag must be nested inside a cfloop tag. The cfbreak tag was encountered on line 13 at column 26.

What the hell? Breaking inside a case statement is a fairly fundamental operation, innit? Now I get that a <cfcase> block has an implied break at the closing tag, but this should not prevent one from betting able to call <cfbreak> explicitly too.

This, admittedly, is the first time I've had the need to do this, but then again most of my CFML is done in CFScript, and I've certainly done precisely this sort of thing using script. My work-around here took the obvious path of using an if/elseif/else instead:

<cfif originalExpression EQ "test">
    stuff specific to TEST<br>
<cfelseif listFindNoCase("Yes,No", originalExpression)>
    stuff specific to YES,NO<br>
<cfelse>
    stuff specific to ANOTHER<br>
</cfif>

I can't help but think this is a bug. Even if Allaire / Macromedia / Adobe did this by design, it's still a bug: just a design bug, not an implementation one. FYI: Railo has exactly the same issue.

On the other hand... maybe someone can see a rationale here that I'm missing? Do tell.

--
Adam