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.
- Script support for tags
- Member functions for CF data type/data structure
- Improved JSON serialization
- Easy to use CF functions for Query tag
- Elvis operator (?:)
- Promoting built-in CF function to first class
- Miscellaneous new functions: QueryGetRow, ListEach and others.
<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
@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
@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.
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
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
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
You must be logged in to post a comment.