3 ms·
I am skeptical about the assertions stuff. I rather write my methods so that they don't blow up if they are being called with wrong data. Throwing an Exception
by Tichy 19y ago
I am skeptical about the assertions stuff. I rather write my methods so that they don't blow up if they are being called with wrong data. Throwing an Exception is OK. I can't recall a case where assertions where really necessary. In fact it seems messy to me to write a method but then to add instructions for humans of the form "but don't call it with parameters like that". It should all be in the code, not in the instructions.
- allenbrunson 19y agoi disagree. if my code is being called with incorrect parameters, i WANT it to blow up. if a method silently fixes input data to something it can deal with, then it's hiding a bug that needs to be fixed. also, assertions are good documentation. they let maintenance programmers a better idea of what's expected at this point.
- edw519 19y agoYes! If it causes pain, I WANT it to hurt. Has worked pretty well for us for 3 million years. The same for our code.
- Tichy 19y agoMabye I never needed them much because I have mostly been using Java, which has things like NullPointerException. So no need to write an assertion for that special case. By blow up I meant do something bad, not fail in a noisy way. That would be the exception.
- darkclark 19y agoUnit tests are the way to separate assertions from your implementation code to avoid messiness.
- paulgb 19y agoAssertions are in the code. And they can really be helpful.