Blog

ColdBox Free Tip 2: Fluent If Statements using the when() Method

Maria Jose Herrera October 08, 2024

Spread the word

Maria Jose Herrera

October 08, 2024

Spread the word


Share your thoughts

Tip 2 - Fluent If Statements -> when()

Continuing our "102 ColdBox Tips and Tricks" series, let’s explore a powerful feature introduced in ColdBox 6: the when() method. This method simplifies conditional logic by allowing you to chain execution blocks based on boolean values in a clean and fluent way.

More Tips and Tricks

Using the when() Method

The when() method is available in the super type, so all handlers, interceptors, layouts, and views have access to it. This method allows you to create a functional approach to if/then/else statements.

  • Here’s the method signature:
/**
 * Functional construct for if statements
 *
 * @target The boolean evaluator, this can be a boolean value
 * @success The closure/lambda to execute if the boolean value is true
 * @failure The closure/lambda to execute if the boolean value is false
 * @return Returns the SuperType object for chaining
*/
function when( required boolean target, required success, failure )

Example in Action

  • Let’s take a look at an example to see how you can use this method:

var search = getInstance( "SearchCriteria" );
when( rc.filter, () => search.addFilter( rc.filter ) )
  .when(
    currentUser.checkPermission( 'admin' ),
    () => search.showSalaries(),
    () => search.hideSalaries()
  )
  • target: The boolean condition.
  • success: A closure/lambda to execute if the condition is true.
  • failure: A closure/lambda to execute if the condition is false.

This approach helps you manage complex logic with cleaner and more maintainable code. You can chain multiple when() methods for a fluent and functional coding style.

Why Use when() ?

The when() method improves the readability of your code, reducing the need for nested if/else statements and making your logic flow more naturally.

Stay tuned for the next tip in our series! If you’re enjoying these tips, share them and follow us for more insights.

Flow Delegates

The when() is just one method in a collection of what we call flow methods available to you in ColdBox. They come from the Flow delegate class in the core, which means you can easily use them in your models as well as using our delegates pattern. Some of the available methods are:

  • when()
  • unless()
  • throwIf()
  • throwUnless
  • ifNull()
  • ifPresent()

component delegates="Flow@coreDelegates"{
	
	// Now you can use all the methods of the Flow class

}

Learn More ColdBox Tips and Tricks

Stay tuned for more ColdBox tips in our ongoing series. if you want to learn more than 10 free tips and tricks make sure to get your “ColdBox 102 tips and tricks book” We have a variety of formats you can choose from.

Get your Copy

Add Your Comment

Recent Entries

Is Cloud the Answer for Your ColdFusion Dilemma?

Is Cloud the Answer for Your ColdFusion Dilemma?

Feeling the limits of an on-premise ColdFusion setup?

Many businesses face high costs, limited scalability, and performance bottlenecks, leaving them wondering if the cloud could be the answer.

In our FREE whitepaper, "Is Cloud the Answer for Your ColdFusion Dilemma?", we explore:

  • Benefits of Migrating to the Cloud: From cost savings to increased flexibility, find out what a cloud-based ColdFusion setup can do.
Cristobal Escobar
Cristobal Escobar
November 13, 2024
Mastering Events and Listeners in CBWIRE

Mastering Events and Listeners in CBWIRE

In CBWIRE, events and listeners are the backbone of building responsive, modular applications without relying heavily on JavaScript. This guide walks you through setting up and using CBWIRE events to create seamless interactions between components, from dispatching events in CFML and frontend templates to listening with Alpine.js and JavaScript. Learn how to make your applications feel dynamic and engaging by effortlessly connecting components. Whether you’re triggering events to update a dashboard or targeting specific parts of your app with dispatchTo, these techniques will empower you to create a modern, interactive CFML experience with ease.

Grant Copley
Grant Copley
November 11, 2024
10 Key Benefits of Hiring a Specialized ColdFusion Consulting Team

10 Key Benefits of Hiring a Specialized ColdFusion Consulting Team

ColdFusion remains a powerful and versatile platform for building dynamic web applications. However, keeping your ColdFusion environment optimized, secure, and scalable requires specialized expertise. Whether managing a long-standing ColdFusion application or planning new development projects, hiring a dedicated ColdFusion consulting and support team can be a game-changer for CTOs, CIOs, and developers. Here's why:

1. Expert Guidance on ColdFusion Web Development

...

Cristobal Escobar
Cristobal Escobar
November 08, 2024