TITLE: design by contract in C++ (Newsgroups: comp.lang.c++.moderated, 2 Feb 99) [ Betrand Meyers is "Mr. Eiffel" and the first to popularize the notion of "design by contract", which introduces the concepts of preconditions, postconditions, and invariants. You can find discussions of this in his book "Object Oriented Software Construction". - adc ] UNKNOWN: >> >Can anyone supply me with information and, if possible implementaton >> >code for REQUIRE, ENSURE and MAINTAIN functions in C++ to support Meyers >> >Design by Contract. [ Apparently REQUIRE=precondition, ENSURE=postcondition and MAINTAIN=invariant -adc ] MALEY: Dave Maley >> probably the most comprehensive treatment was Bob Binder's >> percolation scheme (I don't dare say 'pattern'). I'm sorry but >> I don't have the URL. SURESH: sureshvv@hotmail.com Here it is. http://www.rbsc.com/pats/Percolation.html [ A side note on Eiffel ] (Private email, 4 Mar 99) TRIBBLE: dtribble@technologist.com >True; the Eiffel language has the keywords 'require', 'ensure', >and 'invariant' (IIRC). > >An optional 'require' block occurs at the beginning of a function >and contains one or more expressions (assertions) involving the >function parameters that must be true upon entry to the function. >These consitute the "client" side of the contract; if any of the >assertions are false, the caller has failed to abide by the contract >by passing invalid parameters to the function. Such a failure >causes an exception to be raised. > >An optional 'ensure' block occurs at the end of a function and >contains expressions (assertions) that must be true upon return >from the function. These constitute the "provider" side of the >contract; if any of the assertions are false, the function has >violated its half of the contract, and an exception is raised. > >Similarly (IIRC), a loop may contain 'invariant' clauses that >contain expressions (assertions) that must remain true during the >execution of the loop. > >The 'require' and 'ensure' clauses can be disabled by a compiler >option, for those who think that validating function parameters >and return values is too expensive for production code (an >assumption that almost always courts disaster). > >Obviously, these same things can be done in C++ using 'if' and >'throw' statements. 'assert()' macros can also be used, but >these are more devastating to program execution than exceptions.