Assertions in programming languages such as #c, #cpp and #rust are used as a mean of comparing the output of a function or method of a class to the expected value. They can be incorporated well into the test facility like Rust.
In C and C++, the keywords assert
and _assert
are usually viewed as a Debugging Tools. However, as recommended by Hunt et al., if you ever feel that #something can’t happen, it is a great opportunity to ensure it won’t by using assertions. Even though assertions can add overhead to the program, turning them off when building the binary is a bad idea since it assumes tests alone would find every bug in the codebase which is not the case.
Note: Never put code that must be executed into assertions as it might be turned off by some pricks.
Note: Avoid side effects in assertions at all costs.
Don’t use assertions as a mean of error handling! The difference between the two is the former checks things that must not happen, the latter checks things that could happen.