Why you shall use one check per assert

Have you ever used the assert macro in c or c++? If not, you should try it out!

It is an easy tool for debugging: You can add checks in your code if your application is in a dedicated state. When this is not the case it will stop your application but only when you build it in debug mode. In release-mode the tests will be ignored.
Depending on your platform this can vary a little bit. For instance the
Qt-framework prints a log-message if you have a failed assert test to stderr when you are currently using a release build.

From my point of view assert is a nice tool to check pre-conditions for your functions/methods during the development. And you will see your application crashing when this precondition is not fulfilled. Thanks to some preprocessor-magic the statement itself will be printed to stdout. So when you are writing something
like:

and your pointer is a nullptr-pointer in your application you will get some info on your stdout like:

Great, you see what is wrong and you can start to fix that bug. But sometimes you have to check more than one parameter or state:

Nice one, your application still breaks and you can still see, what went wrong?
Unfortunately not, you will get a message like:

So what went wrong, you will not be able to understand this on a first look.
Because the pointer could be nullptror the state may be wrong or both of the tests went wrong. You need to dig deeper to understand the error.
For a second developer, this will get more complicated, because he will most likely not know which error case he should check first because he didn’t write the code. So when you have to check more than one state please use more than one assert:

Thanks!

First published here: http://kimkulling.de/2016/01/27/please-use-only-one-statement-per-assert/

I am working as a Software-Architect and System-Designer, in my free time I am working on Asset-Importer-Lib.