3 ms·
> So far we've established that: 1. you run with assertions always on in production, 2. you have encountered errors caught by the always-on assertions in produc
by fdr 14y ago
> So far we've established that: 1. you run with assertions always on in production, 2. you have encountered errors caught by the always-on assertions in production
No. I gave two examples, one where assertions are run in production (mostly because it's easier) and I'm trying to communicate with you that I don't see people leaning on assertions to find bugs in production code very frequently at all, and this is the fear of the Go FAQ. I gave another example were assertions are invariably not compiled into the program when running in production. Am I not being clear about this?
> (it may be "seldom," but then all errors are seldom, hopefully.)
False. Errors (like "no permission to file", "out of disk", "out of memory") are distinct from 'an internal consistency check has failed.'
These happen all the time for completely legitimate reasons.
> Turning off error checking is stupid. Your software will never be bug-free. Start dealing with reality and rename your assertions to sanity checks, which is what they are and always have been.
Strong words. Okay, here's what I think is stupid:
* Software too slow for the purpose.
* Avoiding writing expensive invariants because it would make the software too slow.
* The notational similarity of a broken invariant that intended to never occur vs. an error condition.
My solution has been to accept the last-most option and to stop writing expensive invariants, even though I wish I could succinctly and idiomatically communicate to all maintainers that the invariant is considered impossible rather than merely an error condition and run with the expensive invariants while in development (a global variable and praying to the branch prediction gods can be close enough).
- cmccabe 14y agoNeeding lots of expensive invariants is almost "invariably" (see what I did there?) a code smell. You often see big, complex classes with 10 different booleans and a ton of assertion crap, when what is really needed is to refactor the class into smaller classes which are unit-testable. In C++, I've even seen people assert that an unsigned integer was >= 0. You can just feel the quality. The notational similarity of a broken invariant that intended to never occur vs. an error condition. Go has a notation for problems that are never supposed to happen: panic. For other errors, there are return codes.
- fdr 14y ago> Needing lots of expensive invariants is almost "invariably" (see what I did there?) a code smell. What's your definition of need? I want a lot of invariants if because I have found it reduces the chance of error. This is also the finding in that Microsoft case study I linked to. The more invariants, the better, and that means making them cheap to write. > In C++, I've even seen people assert that an unsigned integer was >= 0. You can just feel the quality. Entirely reasonable in some cases; it's telling me: "this software is not defined for negative numbers". There does exist code that can accept an unsigned number (because it is what callers found most convenient at the time, and casting is wordy) but is defined on negative inputs, and it would not be valid to put that assertion there. Those assertions are to assist future collaborators of your software, and shorter to write than: // This algorithm is only defined on positive integers Until one is using a language with real dependent types, I think your specific example is not on the face of it as egregious as you say it is. > Go has a notation for problems that are never supposed to happen: panic. For other errors, there are return codes. Unfortunately I do see panic used -- even in the standard library -- as a flow control mechanism also. But yes, I do use panic for this reason, and tend to eschew using recover for convenience in most situations. It is the lack of an idiom to obtain complete clarity as to the nature of the panic that I find mildly irritating.
- cmccabe 14y agoAsserting that unsigned numbers are >= 0 is a good idea... if you want to write code that ends up on thedailywtf.com. I think we're done here.
- cmccabe 14y agoOK, my response was maybe a little more snarky than I intended. Anyway, I think we are going to have to agree to disagree about the utility of turn off error checking in production.