Make a precondition visible
Before computing a greatest common divisor, a wrapper checks its inputs with `assert(a);` and `assert(b);`. The recursive helper begins with `assert(a <= b);`. These checks state conditions that are supposed to hold whenever the functions run. section 7.3
8 size_t gcd2(size_t a, size_t b) {
9 assert(a <= b);
10 if (!a) return b;
11 size_t rem = b % a;
12 return gcd2(rem, a);
13 }
!@%STYLE%@!
{"css":"{\"css\": \"font-weight: bold;\"}","target":"[[{\"line\":0,\"ch\":1},{\"line\":0,\"ch\":2}],[{\"line\":0,\"ch\":5},{\"line\":0,\"ch\":11}],[{\"line\":3,\"ch\":7},{\"line\":3,\"ch\":13}],[{\"line\":0,\"ch\":5},{\"line\":0,\"ch\":11}],[{\"line\":3,\"ch\":7},{\"line\":3,\"ch\":13}],[{\"line\":0,\"ch\":5},{\"line\":0,\"ch\":11}],[{\"line\":3,\"ch\":7},{\"line\":3,\"ch\":13}],[{\"line\":1,\"ch\":1},{\"line\":1,\"ch\":2}],[{\"line\":1,\"ch\":7},{\"line\":1,\"ch\":13}],[{\"line\":2,\"ch\":0},{\"line\":2,\"ch\":2}],[{\"line\":2,\"ch\":7},{\"line\":2,\"ch\":9}],[{\"line\":2,\"ch\":15},{\"line\":2,\"ch\":21}],[{\"line\":4,\"ch\":7},{\"line\":4,\"ch\":13}],[{\"line\":3,\"ch\":0},{\"line\":3,\"ch\":2}],[{\"line\":0,\"ch\":5},{\"line\":0,\"ch\":11}],[{\"line\":3,\"ch\":7},{\"line\":3,\"ch\":13}],[{\"line\":4,\"ch\":0},{\"line\":4,\"ch\":2}],[{\"line\":2,\"ch\":15},{\"line\":2,\"ch\":21}],[{\"line\":4,\"ch\":7},{\"line\":4,\"ch\":13}],[{\"line\":5,\"ch\":0},{\"line\":5,\"ch\":2}]]"}
!@%STYLE%@!
In the recursive helper, `assert(a <= b)` checks the argument order before the termination test or remainder calculation. The wrapper arranges that order for the initial call; subsequent calls preserve it. section 7.3
`assert` is a macro from `assert.h` for checking a runtime expression. When `NDEBUG` is not defined during compilation, the expression is evaluated each time execution reaches the assertion. If it is false, a diagnostic is printed and `abort` is called. The diagnostic can help locate a false assumption while debugging; abnormal termination is not a useful way to handle an expected customer-facing condition. section 8.7
TAKEAWAY: Make function preconditions explicit, and use assertions to confirm runtime properties. Define `NDEBUG` in a production compilation to inhibit both expression evaluation and the call to `abort`, reducing overhead. Because evaluation can be switched off, do not put work the program needs inside an asserted expression. For conditions that can be checked at compile time, use `_Static_assert` or `static_assert` instead. section 7.3 section 8.7














