[Solved] Refactor a condition [closed]


Optimization was the wrong term, but the code can be rewritten to find a better (more reduced, or succinct to the business) logic.

X < min(Y, Z)    <=>    X < Y and X < Z
X < max(Y, Z)    <=>    X < Y or X < Z            (less usefull)

Then possible rewrites are

return ((e < std::min(std::max(0, a - g), b)) and 
        (f < std::min(d, std::max(0, c - g))));

return ((e < std::max(0, a - g) and e < b)) and 
        (f < d and  f < std::max(0, c - g))));

return (e < 0 or e < a - g)
       and e < b
       and f < d
       and (f < 0 or f < c - g);

Depending on other constraints this might be reducable.

I would rather make a function between for the hard to read min+max.

return e < minmax(b, a - g, 0) and 
       f < minmax(d, c - g, 0);

I did not call this between, as maxmin is feasible too.


Added

@Jarod pointed me to the fact, that the OP mentioned all numbers would be unsigned. Then

max(a - g, 0)

probably should have been

a > g ? a - g : 0

2

solved Refactor a condition [closed]