4 ms·
Every check that you can add to a language means ways that the programmer cannot mess up. That's a gain. It also means things that programmers cannot do. Tha
by AnimalMuppet 10y ago
Every check that you can add to a language means ways that the programmer cannot mess up. That's a gain. It also means things that programmers cannot do. That's a loss. For some restrictions, the gains outweigh the losses. But that won't be true for every restriction; kitchen knives are sharp for a reason. Restricting everything so that nobody can do anything wrong is unworkable; it is, if not the dark path, at least a dark path.
But so is relying solely on testing for safety. There's always a path through the code that you didn't test. Even if your tests execute every path through every function, there's always a combination of paths that wasn't, or an input value that wasn't, or, or or. You cannot achieve quality solely by tests.
You need a balance. Use language features that prevent errors - not that prevent every error, but at least that prevent some. Use good design. Use static code analysis. Use thorough testing. It all matters; no one thing will save you.
- kazinator 10y agoA static check adds a way in which the programmer can mess up. A code path which fails a check was obviously not tested (at least, not since the change was introduced which triggers the check). The programmer can massage the code so the check goes away, yet leave the code path still untested, due to the "safyness" feeling that that the machine "got his/her back", so all is well.
- AnimalMuppet 10y ago> A static check adds a way in which the programmer can mess up. True, though it is one that you can ignore. You can't ignore the compiler. Reading through the output of a static checker is 95% "Stupid hyper-picky static checker", and 5% "Oh #&%$@, we'd better fix that right away". Ignore the ones that should be ignored. > A code path which fails a check was obviously not tested (at least, not since the change was introduced which triggers the check). Or not tested with the right combination of inputs, or not tested looking for the right thing... > The programmer can massage the code so the check goes away, yet leave the code path still untested, due to the "safyness" feeling that that the machine "got his/her back", so all is well. Sure, they can, if they aren't trying to understand what the checker is telling them, and instead just try to get it to shut up. And they can do the same with a test.
- kazinator 10y ago> Or not tested with the right combination of inputs, or not tested looking for the right thing... There's the thing. Unless the code is exploiting dynamism, probing large input spaces is not required to catch the same issues. Just enough variety to achieve path coverage. We don't need to probe the space of integers to catch that a string operation is applied; running that expression with any value will do. I added a static check for uses of undefined functions and variables to a dynamic language quite recently. It didn't find anything in any of my application code. One instance was found in the language's standard library. The function which had that error was never tested. Calling that function with any argument at all triggered the dynamic check. The static check only pointed out this fact: hey, you didn't test this! That's a poor substitute for an actual path coverage tool. You can develop untested code which passes various static checks; only a coverage tool will show you the untested paths.
- Jweb_Guru 10y agoA coverage tool will not come remotely close to showing you untested paths. Array indexing, for example, is an implicit branch between every possible element in the array, but most coverage tools I've seen count just evaluating the indexing expression as covering the line. On the other hand, judicious use of indexed data types can guarantee a data path for all indices into the array. Simple sum types like Maybe and Either guarantee a data path for the more simple use cases of null or errors. Uses of undefined functions and variables are not what people are talking about here (though those do happen in dynamic languages); we're talking about stuff that can't be checked locally (like "is a null allowed to come into the function at all, and if it does are you covering it") that are hard or impossible to catch with coverage tools.
- kazinator 10y agoIf for some crazy reason we choose to regard an array access as if it were a big switch or case statement with a branch for each index, we are wrong in concluding that this creates a path coverage problem. These cases are machine-generated and do not require separate coverage treatment. An analogy might be found in, say, machine-unrolled loops. I don't care about coverage of multiple copies of a loop body caused by a compiler optimization.