getProperty vs findProperty


(Günther Grill) #1

Hi,

currently the two methods:
getProperty(String) throws MissingPropertyException
findProperty(String)
exists. Only difference is, what happens when the property is not found. The first one throws an exception, the second one just returns null.

Wouldn’t it be nice, not to have two different methods-names but just one:
getProperty(String) throws MissingPropertyException
getProperty(String, Object)

So my suggestion is to overload the getProperty and add a default-return value to it which is returned when the property is not found.
IMO this is nice because we have all the possibilities than before, but even one more power: having a default value. Also only one method must be remembered.


(Mark Vieira) #2

The findProperty() method was added for this exact use case. Also, because it follows the pattern of many other parts of the Gradle API where getX() throws an error if the item cannot be found and findX() returns null. To do what you wish you can use the Groovy elvis operator.

def val = findProperty('propertyName') ?: 'default value'

(Günther Grill) #3

All right. Thanks for the clarification.