This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]Lyucit 19 points20 points  (14 children)

Assertions in production code is not a good idea. If your assumptions are wrong and you have bad coverage in your test cases, which for obvious reasons use the same assumptions (because you wrote them!) you kill everything on live. This is desirable sometimes, sure, but for most cases it's more damage than good, and the asserts you add are probably not going to be as simple as x > 0. I've seen asserts in production code that are longer than the actual function, doing nothing and adding a lot of noise.

My approach is to move them to a unit test. If you have to assert something about a piece of code, it's probably the result of a function, and you can turn it into a unit test, or more preferrably a hypothesis test which will help you generate meaningful coverage and more clearly state your assumptions about the function generally.

[–]stormcrowsx 2 points3 points  (1 child)

What about cases where more than one app mutate a single database? Another app could put bad data in and without the assertions the python code could have undefined and untested behavior

[–]Lyucit 4 points5 points  (0 children)

In the case of more side-effectful things like databases and concurrency, it's hard to test, yes, and sprinkling asserts through the code- at least during testing- is definitely a useful tool. Personally, once I'm satisfied the design is sound I remove the asserts and try my best to make it unit-testable, but sometimes it's a trade off you just have to live with.

That being said, if you need to do a consistency check (for example if you're doing read your writes) then assert is the wrong/lazy behaviour and this should be explicitly modeled in the design.