April 8, 2014
Language Enhancements in ColdFusion Splendor – Elvis operator
Comments
(12)
April 8, 2014
Language Enhancements in ColdFusion Splendor – Elvis operator
Newbie 8 posts
Followers: 0 people
(12)

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 Comments
2014-04-10 15:20:00
2014-04-10 15:20:00

@Henry, that ?: example just shows how the Adobe guys don’t actually understand how the null coalescing operator is supposed to work, and have ballsed it up.

See: http://cfmlblog.adamcameron.me/2014/02/coldfusion-11-good-stuff.html#nullCoalescingOperator


Adam

Like
2014-04-10 15:13:33
2014-04-10 15:13:33

@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.

Like
2014-04-10 14:59:47
2014-04-10 14:59:47

I think it is a little different. The changes the username, whereas the Elvis operator does not

Like
2014-04-08 11:22:12
2014-04-08 11:22:12

Btw, is

displayName = username ?: “anonymous”;

the same as?

?

Like
2014-04-08 05:50:03
2014-04-08 05:50:03

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

Like
2014-04-08 05:40:49
2014-04-08 05:40:49

@Adam: no, its not me. but your point is well taken.

Like
2014-04-08 05:39:29
2014-04-08 05:39:29

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

Like
2014-04-08 05:29:38
2014-04-08 05:29:38

This new syntax has no tag use equivalence?

Like
2014-04-08 04:43:41
2014-04-08 04:43:41

@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

Like
2014-04-08 04:24:07
2014-04-08 04:24:07

@Peter, thanks for pointing this out. Will make sure this does not happen again.

Like
2014-04-08 04:10:50
2014-04-08 04:10:50

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

Like
2014-04-08 03:49:41
2014-04-08 03:49:41

You need to get a real CFML developer to proof-read these articles before you post them.

At the very least, attempting to run your own examples in CF would highlight the main issues.

Like
Add Comment