Language Enhancements in ColdFusion Splendor – Elvis operator

The Elvis operator (?:) is a small but elegant feature added in Splendor. I am going to show you how it shortens your conditional code and  makes it look simpler. But before I get into the details, here is the list of language features added in ColdFusion Splendor.

The Elvis operator assigns the ‘right default’ for a variable or an expression. In an expression, if the resultant value is not defined, then the object will be assigned to the left most part of the expression otherwise a default value (define at the right most part) will be assigned.
Consider a conditional case where the displayname is populated with username variable, if the later is defined otherwise a default value “anonymous” needs to be assigned to the displayname variable. The code snippet shown below display both the syntaxes: one with Elvis operator and other without it:
Without Elvis
       displayName = username;
   else {
    displayName = "anonymous";

With Elvis operator

   displayName = username ?: "anonymous";

The support for Elvis operator has been provided for function calls and expressions too. Some of the expression cases are: 

 securityNumber = securityStruct[‘Joe’] ?: -1;  // Retrieving from a struct
 colourCode = colourArray[index] ?: "black";   // Retrieving from an array
 employeeName = getEmployeeName(ID) ?: “Joe”;  // A function call

12 Responses

  1. Yeah, again I agree with @Peter: you guys need to actually a) write proper code; b) test it before you post it to the public. A lot of your readership will expect you to be competent at the language you are responsible for.

    It is clear the person/people writing the code examples on this blog is incompetent with CFML. I am not exaggerating or being a meany, they’re just not up to the task of writing sample code which the CFML community is supposed to take-up.

    Users of this operator should also be aware that you ballsed-up the implementation of it too:

    Someone there closed it with the excuse that it’s working “as designed”. This might be the case, but this just means the design is wrong.

    But it’s a reasonable attempt at a nice addition to the language.


  2. This is part of the code shoddiness I mentioned above. There is no need to have the example usage as a single line of CFSCRIPT, which gives one the incorrect impression it’s a script-only thing.

    It’s just an operator, so is tag/script agnostic. The example could have (and SHOULD HAVE) been:


  3. Thanks Awdhesh. Can I make a (constructive) suggestion? Get your peeps to first post their code on, and tag it with “ColdFusion”. Then people like @Peter and myself and others will actually volunteer our time to help you with it.

    I am more than happy to help you guys with this stuff. But the quid pro quo is you need to start realising that posting poor code like this in public actually does have a negative impact on the credibility of ColdFusion, and it also does not help your community as it perpetuates bad practice.

    And it’d just be *so easy* to do the job right.



  4. @James yes, but the use case is too similar.

    However, I just checked out and his sample demonstrates elvis operator much better.

    displayName = encodeForHTML( ?: “Anonymous”;

    Without the magic of the new elvis operator, it would have thrown undefined at the encodeForHTML call. It’s like adding a big hidden try/catch for the expression before the operator.

Leave a reply