5 ms·
My experience is that this type of coding actually introduces more fragility and sensitivity to change. If you validate conditions you think will be true, but d
by marginalia_nu 5y ago
My experience is that this type of coding actually introduces more fragility and sensitivity to change. If you validate conditions you think will be true, but do not explicitly depend upon at every level the data passes through, you'll find your application randomly blowing up when those conditions change.
I think the problem boils down to a violation of separation of concerns. Defensive code inevitably concerns itself with ensuring guarantees about state that is the concern of other modules.
- Retric 5y agoBlowing up can be the correct answer. What should you do if a slider that ranges from 0 to 100 sends you a negative number? Fail loudly, and log it. Silently, trying to work with invalid inputs just results in bugs that people may not notice and are much harder to track down.
- marginalia_nu 5y agoRight, I would say the problem exists within the slider, and not the consumer of its state. With a sensible design, you might even have the slider producing a type that makes strong guarantees about the range of its value. That moves the concern of validating the value range to that type's constructor, and removes it from the rest of the codebase.
- jve 5y ago100% agree. That's why eventually I want to learn F# (currently my main driver is C#). It features more advanced type system and I love static typing, because by itself it provides nice guarantees. F# brings it further by guaranteeing specific state. I may learn F#, but I don't think I'm gonna use it in existing code base, as maintainability with fellow developers or whoever works after me is perhaps more important than F#... Not that your example cannot be achieved in C# - it can. But what I suspect F# just brings those static typing guarantees further https://fsharpforfunandprofit.com/posts/designing-for-correctness/ https://fsharpforfunandprofit.com/posts/designing-for-correc...
- deleted 5y ago[deleted]
- Retric 5y agoThat doesn’t resolve the issue. If at runtime the physical memory associated with your strongly typed data has a negative value before the function is called either the type system checks it again which then blows up, or assumes it’s still valid and your fiction operates on faulty data. Essentially what you’re describing is automating such checks.
- marginalia_nu 5y agoHardware failure is virtually impossible to deal identify and deal with from software, because the validation relies on the same unreliable hardware. If you want to harden your stuff against that category of errors, then the solution exists with in hardware. RF-shielding and ECC memory for starters.
- Retric 5y agoMost hardware failure is extremely intermittent, otherwise it almost instantly result in a crash. ECC etc helps, but you still run into manufacturing defects etc. That said, I agree you can’t guarantee everything is working via software but detecting hardware failure via software is simply good practice.
- dale_glass 5y agoTechnically true, but practically hardware is very unlikely to fail in a way that maintains self-consistency. Eg, if you checksum all the data, what you can expect from bad RAM is either to detect a mismatch, or to mis-calculate the checksum and something else will detect a mismatch later. You certainly can't count on it, but enough of that sort of thing makes it a lot more likely you'll notice something is not quite right eventually.
- deleted 5y ago[deleted]
- mannykannot 5y agoThis is turning into an argument for strong typing, but, in light of your original claims, only for external inputs, not for internal interfaces - but if it is useful in the former case, why not the latter?
- marginalia_nu 5y agoIn some cases, say when you work very close to a business domain, this may very well be a good thing. But for larger applications, especially that do low-level stuff, the tradeoff is cluttering your namespace with single use parameters. You quickly run into scaling problems. It's exactly the same reason bindly following "textbook" OOP design is a bad idea. Unless you're able to do some very prescient upfront design of a large part of your application, you're in for a big headache later.
- mannykannot 5y ago> But for larger applications, especially that do low-level stuff, the tradeoff is cluttering your namespace with single use parameters. What does this mean? I have not figured out any way to interpret this, such that it is a consequence of choosing strong typing. Your second paragraph is more of the straw man that we are discussing in an adjacent thread.
- marginalia_nu 5y agoI mean that you need to assign each type a distinct name, and in many programming languages, the namespace for types is effectively global. If you add say a thousand types for all the things combinations of things you may want to represent, then you've effectively added the need to have a thousand type names.
- mannykannot 5y agoThat is merely being explicit about what is implicit otherwise. When you are being explicit, there is at least the possibility that these declarations could be scoped - and, in many languages, this is something you can do.
- deleted 5y ago[deleted]
- mannykannot 5y ago> I think the problem boils down to a violation of separation of concerns. Defensive code inevitably concerns itself with ensuring guarantees about state that is the concern of other modules. This is something of a straw man argument. If someone is doing this, they are simply doing it wrong. It is certainly possible to write validations that only check for constraints that must hold within the code they are gatekeeping for.
- marginalia_nu 5y agoThe context of this discussion is adding null checks for parameters that are not explicitly used within the module, thus impairing the ability to test specific parts of the code without instantiating or mocking huge parts of the application. That is, this pattern F(x, y, Z) { if (x == null) bad(); if (y == null) bad(); if (Z == null) bad(); Z.doThing(x, y); } I would argue that Z may have reason to validate x and y, but in this scenario, F only has reason to ensure that Z is not null. What Z does and requires is Z's concern, not F's.
- mannykannot 5y agoThat's what I am saying - your argument boils down to "bad validation is harmful" - true, but hardly controversial; bad code in general tends to be harmful.
- marginalia_nu 5y agoIs it controversial to state that defensive programming boils down to "more validation is better"? This is a style of programming I've seen in the field, and it's seemed to emerge within code bases with major data quality problems, but done little to actually fix the problem. You're less likely to get disastrous train wrecks, but the number of errors largely remains the same unless the cause is addressed.
- mannykannot 5y ago> Is it controversial to state that defensive programming boils down to "more validation is better"? It is a straw man to take this as being intended to justify incorrect and useless validation. If this is something to be decided by personal anecdote, I will just add mine: I haven't seen this, while I have seen many cases where validation has picked up subtle and edge-case errors.