Is there any library that aids in implementing the design by contract principle in a C++ application?
In particular, I'm looking for a library that facilities the usage of the principle, something like this.
Is there any library that aids in implementing the design by contract principle in a C++ application?
In particular, I'm looking for a library that facilities the usage of the principle, something like this.
Try this one: Contract++. It has been accepted to Boost (but is not shipping yet).
If you don't mind using C++0x features, you could implement preconditions and postconditions using lambdas and RAII.
A simple example of postcondition:
Similarly, you could implement preconditions and invariants. The code was taken from an extremely simple C++0x Contracts library.
I followed the teachings of the following articles:
What I ultimately applied was pretty much Samek's approach. Just creating macros for REQUIRE, ENSURE, CHECK and INVARIANT (based on the existing
assert
macro) was very useful. Of course it's not as good as native language support but anyway, it allows you to get most of the practical value from the technique.As for libraries, I don't think that it pays to use one, because one important value of the assertion mechanism is its simplicity.
For the difference between debug and production code, see When should assertions stay in production code?.
Use standard ASSERT/Q_ASSERT, but beware of "invalid" assertions, especially if you leave such diagnostics in external testing (build without NDEBUG).
Small story regarding DBC implementation (using assertions) in a C++ project and "debugging always enabled" policy.
We were using pretty standard tools (ASSERT()/Q_ASSERT()) as DBC implementation until we hit the following situation in integration testing: our latest build was always failing just after start. It was not very professional to release such version (after week of internal QA efforts).
How the problem was introduced?
As a result poor developer was blamed for this error (obviously without this ASSERT there would be no crash) and we had to release hotfix to allow integration tests to be continued.
First of all: I need assertions enabled in integration tests to track failed conditions (the more assertions the better), on the other hand I don't want developers to be afraid that some "extra" ASSERT will crash full software stack.
I found, probably interesting C++-based resolution for this problem: weak asserions. The idea is to not stop whole application on failed assertion, but to record stacktrace for later analysis and continue. We can check as many expectations as we like without fear of crash and we get feedback (stacktraces) from integration. Single process run can provide many failed assertion cases for analysis instead of only one (because there's no abort() called).
Implementation of this idea (using some LD_PRELOAD magic) is shortly described here: http://blog.aplikacja.info/2011/10/assert-to-abort-or-not-to-abort-thats-the-question/
I have a litle c++ header with requirements, insurances and invariants. It has less than 400 loc and should fulfill your needs. You can find it under dhc.hpp It reports errors in a useful way and can be compiled out via defines.
Some design patterns, such as the non-virtual interface make it natural to write pre/post-conditions for a given method: