Skip to content
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

Open
alexdesiqueira opened this issue Dec 21, 2019 · 5 comments
Open

Defensive Programming: replacing assertions with exceptions #773

alexdesiqueira opened this issue Dec 21, 2019 · 5 comments

Comments

@alexdesiqueira
Copy link
Contributor

alexdesiqueira commented Dec 21, 2019

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 using python -O):

$ python -h                                                                                                     
usage: python [option] ... [-c cmd | -m mod | file | -] [arg] ...
Options and arguments (and corresponding environment variables):
(...)
-O     : remove assert and __debug__-dependent statements; add .opt-1 before
         .pyc extension; also PYTHONOPTIMIZE=x

I propose to rewrite this episode using exceptions instead of assertions. For example, the first piece of code in that lesson would be:

numbers = [1.5, 2.3, 0.7, -0.001, 4.4]
total = 0.0
for num in numbers:
    if num <= 0:
        raise ValueError('Data should only contain positive values')
    total += num
print('total is:', total)

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.

@ldko
Copy link
Contributor

ldko commented Jan 10, 2020

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 assert statements to guard against conditions that don't work for a program (i.e. the Assertions section of the Defensive Programming episode), and that teaching exception handling in its place would be more useful. I think it is still useful to teach assert in regard to Test-Driven Development, as this is a common practice for unit testing (for example with pytest). We also discussed a desire to replace the examples (normalize_rectangle and range_overlap) with examples relevant to the inflammation story line. We would be happy to hear any suggestions from you or others in the community regarding replacement examples.

@alexdesiqueira
Copy link
Contributor Author

alexdesiqueira commented Jan 21, 2020

Hey Lauren,
thank you for getting back to me.

I think it is still useful to teach assert in regard to Test-Driven Development, as this is a common practice for unit testing (...)

I agree with you on that.

We also discussed a desire to replace the examples (normalize_rectangle and range_overlap) with examples relevant to the inflammation story line. We would be happy to hear any suggestions from you or others in the community regarding replacement examples.

I will check the examples carefully and try to get back to you, OK?
Thanks again!

@ldko
Copy link
Contributor

ldko commented Jan 21, 2020

It would be great to have you consider the examples and get back to us, Alexandre. Thank you!

@alexdesiqueira
Copy link
Contributor Author

We also discussed a desire to replace the examples (normalize_rectangle and range_overlap) with examples relevant to the inflammation story line. We would be happy to hear any suggestions from you or others in the community regarding replacement examples.

cc @melissawm :)

@bsmith89
Copy link

Relevant discussion in swcarpentry/curriculum-advisors#1.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants