Groovy Elvis Operator?:
by Colin on Oct.30, 2008, under Groovy-Grails
What is this Elvis Operator I vaguely recall? I’ll get to that just bear with me for a minute.
In Java I’ve seen too many null checks like the following:
If (something != null) {
val = something
} else {
val = defaultValue
}
Its handy to have the Groovy Truth so we don’t have to do null checks. Instead of writing if (something != null) { ... } we write if (something) { ... }
Well this type of branching logic is precisely why we have the ternary operator in both Java and Groovy. I’m surprised that the ternary operator is neglected by many developers. It turns our 4 lines of if/else logic into a single line like:
val = something ? something : defaultValue
The syntax and logic for the ternary operator is the same in Groovy as it is in Java; (Condition) ? Value-If-True : Value-If-False I’m not going to get into a lesson on the ternary operator and you can certainly read more here.
When using the ternary operator I had to repeat the variable something twice to do a simple check; This isn’t groovy and it definitely doesn’t lend itself to the principles of DRY! *Queue the Elvis operator*
Instead of writing: val = something ? something : defaultValue
We write: val = something ?: defaultValue
A more clear example would be as follows:
def rockstar
def defaultrockstar = rockstar ?: "Elvis Presley"
assert defaultrockstar == "Elvis Presley"
*Thank you Satish for the example
InfoQ also has an excellent example under the section Syntax Additions :: Elvis Operator.
It is nothing too special but It ends up being closer to how we think about code. Apparently the Elvis Operator was added in Groovy 1.5 and is called the ‘Elvis Operator’ due to its resemblance of Elvis’ trademark hair.
I find the Elvis operator useful, but I still find myself frequently repeating myself when dealing with the same variable.
def rockstar
rockstar = rockstar ?: "Elvis Presley"
If we have gone this far with the Elvis Operator, why not go the extra step and introduce something like long-eyed-elvis ?= or call it the Rick Astley Operator
def rockstar
rockstar ?= "Elvis Presley"
This would be effectively the same as:
rockstar = rockstar ? rockstar : "Elvis Presley"
or
rockstar = rockstar ?: "Elvis Presley"
One more for Good measure … rockstar.
More Reading on the Elvis Operator:
- http://maas-frensch.com/peter/2007/12/19/how-elvis-showed-me-a-neat-way-of-using-operators-in-ruby/
- http://www.codecommit.com/blog/scala/implementing-groovys-elvis-operator-in-scala
- http://jfkbits.blogspot.com/2008/02/call-by-name-yo-elvis.html
- http://docs.codehaus.org/display/GROOVY/Operators
?;-)
4 Comments for this entry
2 Trackbacks / Pingbacks for this entry
-
[digital:meditation] » JUG Saxony 07/2009: Groovy
July 17th, 2009 on 2:45 am[...] the second insight of the evening was getting to know Groovys “Elvis Operator” which, asides being really helpful at times, in terms of its naming surely once again demonstrates [...]
-
Using Groovy to See the Future of Java | PHP Hosts
August 6th, 2009 on 12:49 am[...] traditional Java without ternary, traditional Java with ternary, and the Groovy Elvis operator. The Elvis operator makes the ternary operator even more concise. Like the safe navigation operator, this can be handy [...]
June 24th, 2009 on 12:21 pm
So is long-eyed Elvis operator implemented? Looks like a cool idea to me!
June 24th, 2009 on 12:38 pm
Your statement “The syntax and logic for the ternary operator is the same in Groovy as it is in Java; (Condition) ? Value-If-True : Value-If-False I’m not going to get into a lesson on the ternary operator and you can certainly read more here.”
is not correct. Groovy’s ternary operator is a lot more handy. for example you can not do this in Java:
debug ? (println “debug is on”) : (println “debug is off”)
you can not do this in Java. In Java you would have to revert to if/else.
July 2nd, 2009 on 12:15 am
Alex,
You are right on that the Groovy Ternary operator can do so much more than the Java one!! My words were to show the basic logical branching in Groovy is similar to Java but thank you for Pointing it out to other readers!
July 19th, 2010 on 6:53 pm
who could have not known the greatest musician of the decade. Elvis is the king..;.