How do you test pre-/post-conditions and invariants?

David Nadlinger see at klickverbot.at
Sat Feb 26 06:20:19 PST 2011


On 2/26/11 1:15 PM, Jonathan M Davis wrote:
> […]  And from a perfectly practical standpoint, as soon as your code ends
> up in a library, assertions are generally useless anyway,[…]

I don't quite think asserts are useless in libraries. If you need to 
care about performance in a library, you hit cases quite frequently 
where sanity-checking the input would be too expensive to be done in 
release mode, and thus you can't specify behavior on invalid input as 
part of your API using exceptions. Nevertheless, it is still useful to 
people using your library to get notified when they are messing 
something up as early as possible in debug mode, which is precisely what 
asserts are made for, at least in my opinion.

You can find this use of asserts in many libraries out there, LLVM being 
the first example that comes to my mind.

David



More information about the Digitalmars-d-learn mailing list