Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you simplify this expression?

One for the mathematicians. This has gone around the office and we want to see who can come up with a better optimised version.

(((a+p) <= b) && (a == 0 || a > 1) && (b >= p)) && 
    ((b - (a + p) == 0) || (b - (a + p) > 1))

Edit: all data is positive int's

Edit: Better == simpler

like image 556
Adam Naylor Avatar asked Sep 12 '25 14:09

Adam Naylor


2 Answers

(a + p <= b) && (a != 1) && (b - a - p != 1);
like image 112
nickf Avatar answered Sep 14 '25 08:09

nickf


If the formula works and come from your business rules there is no real need to simplify it. The compiler probably knows better than us how to optimizing the formula.

The only thing you should do is use better variables names which reflect the business logic.

Beware of applying any of the proposed solution before unit testing them.

like image 39
Andrea Francia Avatar answered Sep 14 '25 09:09

Andrea Francia