One lesson I learned quite some time ago is to "Spend time getting to know the language." I find myself occasionally just browsing documentation to see if there are any functions I don't know or haven't seen before. I have seen many programmers/developers waste a lot of time and effort "re-inventing the wheel" because they weren't aware of a function available to them. Let's discuss two semi-related things that have increasingly permeated my programming style the more I use them, the Ternary Operator, and the Elvis Operator!
The ternary operator and the Elvis operator are both used for conditional expressions, but they have different syntax and serve slightly different purposes. The ternary operator was first introduced in the C language in 1972. It has since been adopted by many other programming languages, including ColdFusion. The Elvis operator, on the other hand, was introduced in Apache Groovy in 2007, and PHP 5.3 in 2009, and has since been adopted by other programming languages, including ColdFusion.
The ternary operator, also known as the conditional operator, is a shorthand way to express an if-else statement. It takes the form condition ? expression1 : expression2. If the condition is true, it evaluates to expression1, otherwise, it evaluates to expression2. Here's an example:
condition ? true value : false value
Here, the condition is the expression to be evaluated. If the condition is true, the true value is returned. Otherwise, the false value is returned.
Let's take a look at an example to understand how to use the ternary operator in ColdFusion.
<cfset age = 25>
<cfset isAdult = age GTE 18 ? "Yes" : "No">
<cfoutput>#isAdult#</cfoutput>
In this example, we first set the age variable to 25. We then use the ternary operator to check whether age is greater than or equal to 18. If the condition is true, the isAdult variable is set to "Yes". Otherwise, it's set to "No". Finally, we output the value of the isAdult variable using the cfoutput tag.
The Elvis operator, also known as the null coalescing operator, is a shorthand operator that returns a default value if a variable is null. It's used to avoid null pointer exceptions when dealing with nullable variables. The syntax for the Elvis operator in ColdFusion is as follows:
variable ?: default value
Here, the variable is the variable to be checked for null. If the variable is not null, its value is returned. Otherwise, the default value is returned.
Let's take a look at an example to understand how to use the Elvis operator in ColdFusion.
In this example, we first set the firstName and lastName variables. We then use the Elvis operator to check whether lastName is null. If it's not null, it's concatenated with firstName to form the fullName variable. Otherwise, an empty string is concatenated with firstName to form the fullName variable. Finally, we output the value of the fullName variable using the cfoutput tag.
The ternary operator and the Elvis operator are two shorthand operators that can help you write cleaner and more concise code in ColdFusion. So, remember, while they are similar in structure, they're used for different purposes. The ternary operator is used to evaluate a condition and return one of two values, while the Elvis operator is used to provide a default value when dealing with nullable variables.
I use Ternary and Elvis Operators often; however, it is important to understand that just like with most things you can overuse them. I personally avoid using them when the logic being used in the condition is more than one MAYBE two operators. While I love clean, streamlined code, I also equally hate coming back to code I have written in the past and spending more time than I should just figuring out what I was doing!
P.S. I am sure you know already or have been inquisitive and found the answer to why it is referred to as the Elvis Operator. Well, if you haven't already it's because when ?: is turned sideways it resembles an Elvis emoji with the iconic swoop of hair on one side of his forehead.
I have been building web applications with ColdFusion since 1999. ColdFusion has always been my server of choice and I have utilized it to build and improve numerous business processes, tasks and systems for numerous clients. It has always proven to be the most reliable, stable, powerful and agile tools in my Toolbox.
Comments
Write A Comment