Skip to main content
Ben Nadel at CFCamp 2023 (Freising, Germany) with: Vinicius Perdigão
Ben Nadel at CFCamp 2023 (Freising, Germany) with: Vinicius Perdigão

Using Multiple Break Statements In A Single Case Clause In ColdFusion

By
Published in

For the last few weeks, I've been quasi-heads-down, noodling on a workflow that builds-up complex objects using form POSTs in ColdFusion; and, as part of that workflow, I have a switch statement that defines some mutations on the pending-object in question. The other day, my logic was getting a bit complicated and I went to see if I could use multiple break statements within a single case clause. Turn out, this totally works in ColdFusion.

I actually tried this in JavaScript about 6 years ago, and it works there too. So, all I did was take my multi-break JavaScript demo and rewrite it in ColdFusion (testing this both in Adobe ColdFusion 2021 and Lucee CFML 5.3.8.201).

ASIDE: One of the things that I absolutely love about modern ColdFusion is that translating logic from JavaScript is really simple. If you compare the JavaScript version to the ColdFusion version below, the difference are minute, primarily around method signatures and double-vs-triple equals.

In this ColdFusion demo, I have several case clauses that all fall-through to the same block of logic. Then, within that logic, I try to exit-out early (ie, short-circuit the clause) based on the current value. Ultimately, each collection of clause should echo out only up to and including the given value.

<cfscript>

	// Test values against the various ranges.
	// --
	// NOTE: Using arrayEach() instead of .each() because Adobe ColdFusion 2021 _still_
	// doesn't like member functions being called on a LITERAL object definition.
	arrayEach( [ "A", "B", "C", "D", "E", "F", "G" ], runTest );

	// ------------------------------------------------------------------------------- //
	// ------------------------------------------------------------------------------- //

	/**
	* I test to see if I can break out of a SWITCH CASE early by using multiple break
	* statements inside the same case.
	*/
	public void function runTest( required string value ) {

		switch ( value ) {
			case "A":
			case "B":
			case "C":

				echoLine( "Testing #value# in [ A - C ]:" );
				echoLine( "A" );

				// Try to break out of case early.
				if ( value == "A" ) {

					break;

				}

				echoLine( "B" );

				// Try to break out of case early.
				if ( value == "B" ) {

					break;

				}

				echoLine( "C" );

			break;
			case "D":
			case "E":
			case "F":

				echoLine( "Testing #value# in [ D - F ]:" );
				echoLine( "D" );

				// Try to break out of case early.
				if ( value == "D" ) {

					break;

				}

				echoLine( "E" );

				// Try to break out of case early.
				if ( value == "E" ) {

					break;

				}

				echoLine( "F" );

			break;
		}

	}

	// ------------------------------------------------------------------------------- //
	// ------------------------------------------------------------------------------- //

	/**
	* I run echo() with an appending break tag.
	*/	
	public void function echoLine( required string value ) {

		writeOutput( value & "<br />" );

	}

</cfscript>

As you can see, in the A,B,C block and the D,E,F block, each value should echo the previous values and itself. Meaning:

  • A should echo A, then short-circuit.

  • B should echo A and B, then short-circuit.

  • C should echo A and B and C.

The D,E,F clauses act the same way. And, when we run this ColdFusion code (in either Adobe ColdFusion or Lucee CFML), we get the following output:

Output demonstrating that each case statement can be short-circuited by multiple break statements in ColdFusion.

As you can see, each case clause was able to use multiple, conditional break statements in order to affect the control flow in ColdFusion. It's very possible that the need for this indicates a "code smell" that should be refactored. But, for getting an MVP (Minimum Viable Product) on the screen, I'm not afraid to use this kind of logic.

Want to use code from this post? Check out the license.

Reader Comments

Post A Comment — I'd Love To Hear From You!

Post a Comment

I believe in love. I believe in compassion. I believe in human rights. I believe that we can afford to give more of these gifts to the world around us because it costs us nothing to be decent and kind and understanding. And, I want you to know that when you land on this site, you are accepted for who you are, no matter how you identify, what truths you live, or whatever kind of goofy shit makes you feel alive! Rock on with your bad self!
Ben Nadel