Another shot at math wizards?
How about making your existing solution a little more readable, but still use the instructions? true: false shortcut?
Your decision was:
amount *= (negate ? -1 : 1);
Perhaps refactoring to
amount = (negate ? amount*-1 : amount);
To add even more readability to your code, you can create a reuse class that handles these things for you:
public static class MathHelpers() {
And in your other code, you now have a very readable function to use ...
decimal amount = 500m; bool negate = true; amount = MathHelper.Negate(amount, negate);
In general, although I agree that elegance and readability live in the same trolley, not different:
if (condition) output *= -1;
more readable than
value *= condition ? -1 : 1;
source share