24

Is there a more "Groovy" way to write this Groovy code:

def myVar=(System.getProperty("props") == null)?
    null : System.getProperty("props")

Logic is:

  • If System.getProperty("props") is NULL, I want props to be NULL;
  • Else, I want props to be the value of System.getProperty("props")
IAmYourFaja
  • 55,468
  • 181
  • 466
  • 756
  • 8
    Halt, have a coffee, and rethink about the statement you have in the question. :) – dmahapatro Jul 30 '14 at 14:27
  • This is definitely a good idea :) – Paweł Piecyk Jul 30 '14 at 14:28
  • Not sure why this is downvoted. It hasn't been asked before, cannot really be easily researched, and is a programming/code question. As for the coffee @dmahapatro, that didn't help at all. I have the same question as before... – IAmYourFaja Jul 30 '14 at 14:39
  • i didn't downvote, but i think it's totally unclear what you're asking for. If Pawel's answer is not what you want then I don't know what you're looking for. – Nathan Hughes Jul 30 '14 at 14:40
  • Thanks @NathanHughes but Pawel's answer is "*This is definitely a good idea :)*". He is referring to dmahapatro's suggestion for me to have a coffee, and isn't an answer. As to what I'm asking, I'm asking if there is a syntactic Groovy shortcut for the way I currently have the code written. For instance, Groovy allows you to remove semi-colons on the ends of statements, etc. I'm wondering if there's anything I can do here to "minify" my code and make it more "Groovy". – IAmYourFaja Jul 30 '14 at 14:42
  • Ok let me help you. If **something** is null, I want it to be evaluated as null otherwise I want **something** to be evaluated as is, which implies I can write `def a = **something**`, which was the answer. – dmahapatro Jul 30 '14 at 14:45
  • oops, i was referring to Pawel's answer, which I only just now noticed he deleted. he posted `def myVar = System.getProperty("props")` which is the same place my answer ends up. – Nathan Hughes Jul 30 '14 at 14:51
  • My point was to focus on the requirement and match it with the line of code present in the question. Answer lies in the detail of the question. You cannot be more groovier than `def myVar = System.getProperty("props")` (unless `System.property["props"]` is used). – dmahapatro Jul 30 '14 at 14:52
  • Guys, sorry for deleting the answer and mixing-up the discussion as the side effect :) – Paweł Piecyk Jul 30 '14 at 17:14
  • @PawełPiecyk though coffee is still a good idea – Will Jul 30 '14 at 18:29

1 Answers1

56

Typically for null-checking I reach for ?: (elvis operator, returns a default value if the left-hand side is null or resolves to false) or ?. (safe navigation, evaluates to null if left-hand side is null). If you want to set a default value to use when a property is not present you can do this:

def myVar = System.properties['props'] ?: 'mydefaultvalue'

which sets myVar to 'mydefaultvalue' if there is nothing found in System.properties for the key 'props' (or if the value returned resolves to false).

But since the default value in your case is null then

def myVar = System.properties['props']

would do the job as well, because when nothing is found for the given key then null is returned.

The Groovy-ifications here are:

  • prefer single-quoted strings to double-quoted ones if you don't need GroovyString interpolation

  • use indexing-with-brackets syntax for maps and lists (instead of 'get' or 'put')

  • use the shortened property form (without the get prefix) if the getter has no arguments (unlike Java, Groovy implements the universal access principle); System.getProperty(String) is a convenience for Java programmers but it's unneeded in Groovy

  • shorten default-if-null cases with ?:

This idiom found in JavaScript using || :

def myVar = System.properties['props'] || 'mydefaultvalue'

doesn't work in Groovy. The result of a boolean test is a boolean, so myVar gets set to true.

Nathan Hughes
  • 94,330
  • 19
  • 181
  • 276
  • Or `def myVar = System.properties['props'] || 'mydefaultvalue'` – Krzysztof Romanowski Jul 30 '14 at 14:44
  • 2
    @castus: this seems like a familiar idiom in Ruby iirc, and i expected this to work here too. but in groovy your line results in myVar being set to the boolean value true. – Nathan Hughes Jul 30 '14 at 15:01
  • 1
    In a build.gradle file you can use this for getting Gradle properties with a default: `findProperty('foo') ?: 'bar'` The `findProperty()` method belongs to Project (https://docs.gradle.org/current/javadoc/org/gradle/api/Project.html) - a top-level object you can interface with directly from your script. – Phil Brock Jun 11 '19 at 11:22