4 ms·
> Is [not testing nil/invalid inputs] a risk? Sure, but only if some other part of the system was written without tests. If you call this function from a module
by thethirdone 7y ago
> Is [not testing nil/invalid inputs] a risk? Sure, but only if some other part of the system was written without tests. If you call this function from a module that you wrote with tests, using something as effective as the TDD discipline, you won’t be passing nil or negative numbers, or any other form of invalid arguments. So I’m not going to worry about that. It’s on you.
The problem with this way of thinking is that it makes tracking down a bug much harder, and even if all software is correct at the end it will still make development slower.
Imagine a bunch of functions A, B, ... and Z where A depends on B and so on. If every one of those doesn't test what happens on incorrect inputs, then an incorrect input to A may cause an exception in Z. You would have to track all the way back up to find the cause of the issue.
For the given simple example, not testing negative of nil inputs may be fine because just looking at the code is enough to document it, but on a larger scale, failure conditions can be very obscure.
Simply put in a long chain of software, you will want to define on what values your function is valid and only accept those (so users don't think something works because only one case does). And to keep your code correct to that specification, you need to make sure it errors.