On May 20, 3:53 am, Phlip <phlip2...@gmail.com> wrote:
joshuamaurice wrote:
http://c2.com/cgi/wiki?DoNotUseAssertions
The author of which obviously doesn't understand software
engineering, or reliability issues.
I added the following verbiage, where "assertion" specifically
meant the C assert.h macro:
Step zero: Refactor comments into assertions
Good idea when possible. Preconditions, for example, should be
expressed as asssertions, when possible. Sometimes, it's not
practical for performance reasons. Something like a binary
search requires that the array passed to it be sorted; asserting
this more or less defeats the purpose of the binary search,
since it requires linear execution time. And of course, some
things simply cannot be verified from within the program. (A
precondition of, say, std::vector<>::push_back is that no other
thread is currently accessing the vector; that if other threads
can access the vector, then the accesses must be externally
synchronized.)
I'm not sure about your use of "refactor" here, though. Just
because you add assertions (in the implementation of the
function, thus in the source file) doesn't mean you should
remove the comments (in the header file).
Step one: Refactor assertions out of the code into unit tests
Again, I question the use of the word "refactor". And I'm not
too sure what you're really asking for: client code of the
function with assertions should definitely have unit tests to
ensure that it never triggers the assertion. But testing isn't
perfect, shit happens, and the exception should stay in so that
it triggers if an error occurs after delivery.
Step three: Escalate the remaining assertions into program exce=