I'm learning groovy to work on smartthings and found a relatively common command among the various examples and existing code (see below).
Reading the function of the && operator I would think the "&& cmd.previousMeterValue" is superfluous. Or is there some code shortcut I'm missing?
Thanks John
if (cmd.previousMeterValue && cmd.previousMeterValue != cmd.meterValue) {
do something
}
Not knowing what type
previousMeterValuehas, this answer is somewhat generic.Groovy follows common operator precedence, i.e.
!=is evaluated before&&.To show it explicitly, the full expression is the same as:
cmd.previousMeterValueis testing the value for the Groovy-Truth.Depending on value type, the following might be applicable:
So if the value is
nullor0, the expression isfalse.If the first part of the expression evaluated to
false, then the second part is skipped.If the first part of the expression evaluated to
true, thencmd.previousMeterValue != cmd.meterValueis evaluated, using the following rule:So if value is a number object, then it is evaluated as:
This means that
BigDecimalvalues are compared by value, ignoring specific scale.