I’m trying to avoid mutable variables as much as possible, but sometimes it just feels too hard, and I don’t want to end with an extremely complicated code
Nevertheless I found this way to do it, but it certainly feels like cheating
I’m parsing a query, and I find find a field!value, I want to translate it to field:<>value, and then go on processing, so I came out with this code to avoid mutables, or at least to have them confined…
val (operator, negated) = {
var operator = ConditionOperator.toConditionOperator(parsedOperator)
var negated = parsedNegated == "!"
// field!value => field notEqual value
if (negated && operator == ConditionOperator.Unknown) {
negated = false
operator = ConditionOperator.NotEqual
}
(operator, negated)
}
Is this the right way to do it??? or is there a more idiomatic (and clear!) way to achieve this kind of things?
This is indeed cheating since
varis mutable!The way to correct this is to simply return the if-statement’s result directly: