Hi Jeroen, Thanks for raising this point!
On Mon, Sep 24, 2012 at 09:24:37AM +0200, Jeroen Demeyer wrote: > In Sage, I have seen some quite some places where assert/AssertionError > is used for control flow, where an > > if condition: > raise ValueError("...") # or other exception > > would be the right thing to use instead of an assert. > > I find this bad, because an assert checks something which should always > be true, a failed assertion is always a bug in the program. It's not > something which should be raised and caught like other exceptions. Also > remember that assertions are something which can be turned off, and > doing that should not change the program. Definitely +1. For the same reason, I particularly dislike calling a function with random garbage to see if it can handle it, hopping that the function will properly catch and report bad values. We are doing it a lot in the coercion code, and this is sooo brittle and unexplicit. > That being said, assertions certainly have their place if used well. > But if there is any user input to a public function which can cause an > assertion, that is by definition a bug. Just like any segmentation > fault in the program is by definition a bug (even if it's caused by > bogus input). I personally consider that using ``assert`` for checking the preconditions of a function has two strong values: - It's very concise, readable and to the point. Compare: assert i > 0 With: if i <= 0: raise ValueError("i should be a positive integer") An specific point is that it is hard to enforce consistency across all hand written error messages. Also note that, upon error, the `assert i > 0` line appears in the traceback. - It can be turned off This point is *crucial*: it relieves the programmer from the dangerous temptation of not testing the input to save on speed. Granted, it's on the harsh side on the user to raise an assertion, so I see the point of writing nice ValueError error messages for the most public or non speed-critical functions. But for other functions I think we should allow for using asserts. That being said, if there is a way to raise ValueError in a way that can be turned off like for assertions, I am happy to change my mind! (I am missing this from MuPAD; we could even change this at runtime which was really cool),. By the way, we should encourage more assert's everywhere in the code. And to start with, implement ``sage -O`` which passes the -O option to python to disable assertions (#13523). Cheers, Nicolas -- Nicolas M. ThiƩry "Isil" <nthi...@users.sf.net> http://Nicolas.Thiery.name/ -- You received this message because you are subscribed to the Google Groups "sage-devel" group. To post to this group, send email to sage-devel@googlegroups.com. To unsubscribe from this group, send email to sage-devel+unsubscr...@googlegroups.com. Visit this group at http://groups.google.com/group/sage-devel?hl=en.