mercredi 13 décembre 2017

Ternary operation in setter VS Classic If

Currently working on modifying big mapping classes and some new rules have appeared making me wonder what is the best option here.

Imagine a classic mapping function like so:

yyy.setType(xxx.getType);
yyy.setSomething(xxx.getSomethigElse);
yyy.setThisAsWell(xxx.getThatAsWell);

And I now have a condition to check, what would be better? (knowing that I won't have future similar condition checking to do):

final Boolean isRuleApplying = xxx.getRule == RULE;
yyy.setType(
   isRuleApplying ? RULE_STUFF : xxx.getType
);
yyy.setSomething(
   isRuleApplying ? RULE_STUFF_OTHER : xxx.getSomethigElse
);
yyy.setThisAsWell(
   isRuleApplying ? RULE_STUFF_AGAIN : xxx.getThatAsWell
);

Or is it better to use the old if else?

if (xxx.getRule == RULE) {
   yyy.setType(RULE_STUFF);
   yyy.setSomething(RULE_STUFF_OTHER);
   yyy.setThisAsWell(RULE_STUFF_AGAIN);
} else {
   yyy.setType(xxx.getType);
   yyy.setSomething(xxx.getSomethigElse);
   yyy.setThisAsWell(xxx.getThatAsWell);
}

I feel that using ternary operation make it less maintainable and adds more complexity (checks everytime). But I wanted to get some other opinions.

Note: I have a bunch of try..catch so using if means duplicating those try blocks or adding an if in every block which kind of kills readability.

Aucun commentaire:

Enregistrer un commentaire