In general, it is appropriate to frequently use short assertions indicating important assumptions concerning a program's behavior.
In the absence of an assertion facility, many programmers use comments in the following way:
if (i%3 == 0) { ... } else if (i%3 == 1) { ... } else { // (i%3 == 2) ... } |
When your code contains a construct that asserts an invariant, you should change it to an assert. To change the above example (where an assert protects the else clause in a multiway if-statement), you might do the following:
if (i % 3 == 0) { ... } else if (i%3 == 1) { ... } else { assert i%3 == 2; ... } |
Note, the assertion in the above example may fail if i is negative, as the % operator is not a true mod operator, but computes the remainder, which may be negative.