-
-
Notifications
You must be signed in to change notification settings - Fork 783
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Defensive Programming: replacing assertions with exceptions #773
Comments
Hi Alexandre, thank you for opening this issue. @maxim-belkin, @annefou, and I discussed this today. We are in agreement with you regarding it not being the best practice to use |
Hey Lauren,
I agree with you on that.
I will check the examples carefully and try to get back to you, OK? |
It would be great to have you consider the examples and get back to us, Alexandre. Thank you! |
cc @melissawm :) |
Relevant discussion in swcarpentry/curriculum-advisors#1. |
Hi everyone,
I was lecturing using the Defensive Programming lesson (10-defensive.md) today, and I checked we are using several assertions to verify code. However, this is not a very good programming practice: assertions should provide a security check only when writing and debugging the program.
When the code is released, no assertions should fail, as Bil Lewis explains with more details in this post.
More than that, using assertions in Python could induce to some errors:
assert
statements are removed when the compilation is optimized, as noted in this Stack Overflow answer. This way, all tests and checking would be lost if the code is optimized (executed usingpython -O
):I propose to rewrite this episode using exceptions instead of assertions. For example, the first piece of code in that lesson would be:
If we choose different exceptions, remembering they were shown previously in 08-errors.md, we show how to present more meaningful errors, rather than always presenting an
AssertionError
.Thank you very much for considering this. Thanks also to @stefanv and @jarrodmillman for helping me understanding some points in this issue.
The text was updated successfully, but these errors were encountered: