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
<cfscript>
   if(isdefined('username')){
       displayName = username;
   }
   else {
    displayName = "anonymous";
   }
</cfscript>

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 thoughts on “Language Enhancements in ColdFusion Splendor – Elvis operator

  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: https://bugbase.adobe.com/index.cfm?event=bug&id=3710381

    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.


    Adam

  2. @Awdhesh: are you the one writing the code for these blog articles / the docs that @Peter has draw your attn to recently?

    If so – seriously – stop writing CFML. Get someone else to do it.


    Adam

  3. 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:


    Adam

  4. Thanks Awdhesh. Can I make a (constructive) suggestion? Get your peeps to first post their code on http://codereview.stackexchange.com/, 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.

    Cheers.


    Adam

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

    However, I just checked out http://www.raymondcamden.com/index.cfm/2014/4/10/Recording-and-demos-from-my-ColdFusion-11-presentation and his sample demonstrates elvis operator much better.

    displayName = encodeForHTML(url.name) ?: “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

Your email address will not be published. Required fields are marked *