3 ms·
Tests work because we don't use only tests - we use tests to support and correct our logical reasoning. Even for a very simple task, if you literally didn't thi
by jhpriestley 11y ago
Tests work because we don't use only tests - we use tests to support and correct our logical reasoning. Even for a very simple task, if you literally didn't think about how to solve it at all and just did the simplest thing to make each test pass, you'd never accomplish it. E.g., "find the absolute value of a number" would end up with a function like
abs(n) {
if(n == 1) return 1;
if(n == -1) return 1;
if(n == -3.5) return 3.5;
...
}
This is why testing is not a substitute for logical reasoning, but a complement; once we think we've solved a problem logically, we can test several cases to check our reasoning.
This is also why no amount of testing will make up for a program with no logical structure. For example, if you use unstructured GOTO as the only means of control flow, then you won't be able to solve the resulting bugginess by writing more tests.
- jimmaswell 11y agoPeople actually do write tested and reliable "unstructured" code, eg pure assembly. It's harder but it's not automatically impossible as is often implied.
- ArkyBeagle 11y agoStructure matters less than being able to reason about the set of all possible inputs and outputs. Structure only matters in that inferring the range and domain of the inputs and output is then possibly constrained. We could produce completely bug-free code every time but it's simply not an interesting problem if we listen to our revealed preferences about defects. We arbitrage defects in the service of other values.