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 · 3 comments
Open

Defensive Programming: replacing assertions with exceptions #773

alexdesiqueira opened this issue Dec 21, 2019 · 3 comments

Comments

@alexdesiqueira
Copy link

@alexdesiqueira alexdesiqueira commented Dec 21, 2019

Hi everyone,
I was lecturing using the Defensive Programming lesson (09-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

This comment has been minimized.

Copy link
Contributor

@ldko 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

This comment has been minimized.

Copy link
Author

@alexdesiqueira 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

This comment has been minimized.

Copy link
Contributor

@ldko ldko commented Jan 21, 2020

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.