codebook icon indicating copy to clipboard operation
codebook copied to clipboard

Differentiate when to use assertions vs exceptions

Open GuillaumeQuenneville opened this issue 2 years ago • 0 comments

Hi,

I really liked your talk on test & code, and I'm working through the book at the moment.

One comment/question I have is that I found your suggestions when to assert vs raise an exception were a little vague. From my understanding, assertions are great in tests (its the main mechanism to implement them) but should not be in production code. Production code should raise a ValueError, NotImplementedError, SomeCustomError... Something more representative than AssertionError.

For example, the exercise https://github.com/patrickmineault/codebook/blob/083bf3840bebee35e99203c87ab00b265841a392/testing.md?plain=1#L433-L435 Might lead us to believe that you should have asserts in your production code vs a more appropriate exception.

In the testing to maintain your sanity section you mix tests at runtime (the data is bad) and tests that can be done at implementation time (you're loading the data incorrectlly) which add to the vagueness of when to put asserts. https://github.com/patrickmineault/codebook/blob/083bf3840bebee35e99203c87ab00b265841a392/testing.md?plain=1#L29-L33

Thanks again for the ideas, ill be implementing some of them!

GuillaumeQuenneville avatar Aug 31 '22 18:08 GuillaumeQuenneville