3 ms·
The tradeoff is that conditionals increase the number of paths through the code. It's like Tony Hoare said: > There are two ways of constructing a software des
by chriswarbo 8y ago
The tradeoff is that conditionals increase the number of paths through the code. It's like Tony Hoare said:
> There are two ways of constructing a software design: One way is to make it so simple that there are obviously no deficiencies, and the other way is to make it so complicated that there are no obvious deficiencies.
I hit this problem the other day, writing some code to divide a set of items evenly into N subsets. I wrote a bunch of property tests for this, but kept having problems with the way it was handling remainders. I could have made the tests pass by adding a few more conditionals, but instead I tried to solve it using no more than one conditional plus a handful of arithmetic operations.
The reason I constrained myself was that those test failures were a precious resource to help me understand the problem. Adding complexity to the solution could have produced a correct implementation, but I felt it would more likely out-smart my test suite. I would rather work with something that's clearly broken in an obvious way, rather than possibly broken in an unknown way.
Eventually, I came to realise that the requirements I'd come up with were inconsistent!