bugs should be found as early as possible. There are two basic
methods to find bugs early: assert and unit test.
Idea of assert is to make "an unnormal" can be spotted immediately, or this
"an nunormal" will cause error in other place, then It’s a little difficult to
trace back source.
Although when to use assert depends on context, Use assert to its
fullest. precondition assertion to test the validity of the arguments
passed to a method. and use postcondition assertion to test the
validity of the results produced by the method. In my whirl2llvm
project, I have used them a lot. It really gave me a lot of benefits.
Assert is just if()+abort(): Difference of assert and return error(throw
exception) lies in two sides: 1) It will abort you application, and you
can use GDB to trace back source easily. 2)Do you think thatis a bug or exceptions(error) ? Detail can be seen conclusion
section.
A practical example is dead battery in cell phone, It’s a exception. But if
you have diarrhea, It’s a unnormal, It’s not exception. For example, opening
file failure is exception, You should (throw exception). but age<0 is a
indication of a bug.