Suggestions how to use contracts

Hans-Eric Grönlund hasse42g at gmail.com
Thu Aug 30 08:04:07 PDT 2007


Preconditions are a conceptual contract that are set up for other parts of the system to obey. If you can't assert the condition, well then it simply shouldn't be a one. In your example, the m_active check should not be a precondition if you can't rely upon it always being true upon invocation.

Regards

Hans-Eric Grönlund
http://www.hans-eric.com

Aarti_pl Wrote:

> Hello!
> 
> I have a difficulty with defining strict boundary between 
> preconditions/postconditions and regular errors throwing by function.
> 
> Can you share your strategies? How to divide checks in proper way, 
> remembering that preconditions and postconditions are stripped from code 
> in release mode.
> 
> Example:
> class Evaluator {
> public:
> 	int evaluate(char[] expression)
> 	in {
> 		assert(expression !="");
> 		assert(m_active);
> 	}
> 
> 	body {
> 	}
> private:
> 	bool m_active;
> }
> 
> Function evaluate can be called only when m_active is true. Should this 
> condition be checked from preconditions block or from body block with
> if (!m_active) throw new Exception("Evaluator is not active.");
> 
> "m_active" depends on some other logic, either set by user or by other 
> classes.
> 
> 
> BR
> Marcin Kuszczak
> (Aarti_pl)



More information about the Digitalmars-d-learn mailing list