int add(int a, int b) {
return a + b;
}
int add(int a, int b) {
if ((a > 0 && b > 0) && (INT_MAX - a < b)) {
return INT_MIN + (b - (INT_MAX - a + 1));
} else if ((a < 0 && b < 0) && (INT_MIN - a > b)) {
return INT_MAX + (b - (INT_MIN - a - 1));
} else {
return a + b;
}
}
Which one expresses the intent more simply and understandably?Which one is more robust?
Which one would you bet the future of your company on?
I offer that, sometimes, simplicity of expression is at odds with protection against edge cases.