top of page
need-an-expert-background.png

ColdFusion and Elvis: Exploring the Connection Between the Programming Language and the King of Rock 'n' Roll

need-an-expert-background.png

ColdFusion and Elvis: Exploring the Connection Between the Programming Language and the King of Rock 'n' Roll



Article by Tim Garver, Senior ColdFusion Developer with CF Webtools. ColdFusion and web application developer since 1998 who is driven by writing better code than he wrote yesterday!


When you hear the word “ColdFusion,” the first thing that comes to mind is probably a late 90’s hit movie Chain Reaction with Keanu Reeves, or the movie The Saint with Val Kilmer. But a programming language used for web development? Perhaps these awesome titles were the inspiration for the name given by Jeremy Allaire when launching the language in 1995. We are not sure, but later in almost every programming language, a new shortcut operator was introduced with the name of Elvis.


However, there’s another prominent figure associated with the name Elvis: “Elvis Presley,” the legendary King of Rock ‘n’ Roll. Surprisingly, there is a unique connection between ColdFusion and Elvis that goes beyond their names. In this article, we will explore the fascinating connection between the programming language and the musical icon.

Devs were trying to answer a question: How do we show content dynamically while making it easier to write complex conditionals? Perhaps they were listening to an oldies radio while brainstorming and heard Elvis sing ‘Ask Me.’ At least this is how I imagine the connection was made 😊.


The notation of the Elvis operator was inspired by the ternary conditional operator, ? :, since the Elvis operator expression A ?: B is approximately equivalent to the ternary conditional A ? A : B.

The name “Elvis operator” refers to the fact that when its common notation, ?:, is viewed sideways, it resembles an emoticon of Elvis Presley with his signature hairstyle.

Consider this operation:

// call our remote function that returns a complex object
var myObject = callToARemoteFunction();
if(!isNull(myObject) && structKeyExists(myObject, 'userObject') && structKeyExists(myObject.userObject, 'userPermissions')) {
    // now we are safe to use our user permissions object
    var userPerms = myObject['userObject']['userPermissions'];
} else {
    var userPerms = [];
}

This can be rewritten using Elvis and the safe navigation operators:

// call our remote function that returns a complex object
var myObject = callToARemoteFunction();
var userPerms = myObject?.userObject?.userPermissions ?: [];

As you can see, this really trims down the validity check on the userPermissions object.

The Safe Navigation operator will shortcut the operation into an undefined value if the keys in the object do not exist or are a false value. Then the Elvis operator (?:) is used to return the left side if it evaluates to a truthy condition; else, it returns the right side of the expression, in this case, the new array operator.

Here are a few other examples:

<cfquery name="qryUsers">
select id, name, email, lastLogin
from users
where email = <cfqueryparam cf_sql_type='varchar' value='bob@cfwebtools.com'/>
</cfquery>

if (qryUsers.lastLogin EQ "") {
    // Our lastLogin is NULL
    // update db with now() as last login date..
}

This code can be updated with Elvis; aside from the query, the if statement is where you can take advantage of these new language enhancements.

if (qryUsers.lastLogin ?: true) {
    // Since we only cared about the last login being null, we can shortcut this and force a True
    // update our db last login
}

Since we only cared about the null of last login, this will shortcut over to the true in the if statement.

There are some things you should be aware of when using the Elvis and Safe Navigation features.

  1. If the value you are comparing is a Boolean false, this will backfire on you if you are not planning for it. Also, it depends on the version of CF you are using.

  2. Do not use Elvis alone during assignment operations. It can fail, leaving you scratching your head with questions.

For example:

x = {a = false}; 
b = x?.a ?: true;
if(b) {
    // do some work based on B being true…
    runCoolFunctionB();
}

Do you think the function will be called?

If you answered no, you are sort of correct. If you answered yes, you are also sort of correct. 😊 It depends on your server; Lucee and Adobe ColdFusion behave differently with this feature.

Why is that? It is because the value of X.A = false. And since X.A equals a false Boolean value, Elvis will never be called.

Consider this:

writeOutput(false ?: true);

Adobe ColdFusion 2016+: Outputs TrueLucee 5+: Outputs False

Adobe ColdFusion, if the value on the left side of the Elvis operator is one of these: (Undefined, Null, or False), then the value on the right side of the expression is returned.

In Lucee, a false value is returned; in other words, only these are considered (Undefined, Null) on the left side of the expression. A false value in Lucee will return false in our expression above. So keep these idiosyncrasies in mind when using Elvis. This is why you should not use it alone when creating a variable assignment.

 
 
bottom of page