Contract based programming Using pre and postconditions and
Contract based programming Using pre- and post-conditions, and object invariants Contract based programming 1
Design by contract • Idea • A program is correct if given correct input the program produces correct output. • Correct input → PROGRAM → correct output • A program is considered a kind of “black box” • Same idea applies to parts of a program • Methods, functions, etc. • Correct input → METHOD → correct output • Precondition • Specification of correct input • Postcondition • Specification of correct output Contract based programming 2
Contracts • A supplier (programmer) writes a class or method to be used by a client (another programmer) • The contract specifies • The public interface of the class / method • Pre-conditions • Post-conditions • Object invariants Contract based programming 3
Pre- and post-conditions • Pre-condition • What does the method expect? • Must be true before it makes sense to call the method • Methods should check this and throw appropriate exceptions • C# examples • Convert. To. Int 32(String str) • Assumes str is a string that contains number. • If not it throws Format. Exception, or Overflow. Exception • Post-condition • What does the method guarantee? • After the method has executed • Returned value, and/or change of object state Contract based programming 4
Object invariant • An invariant is a statement that is invariable true • Object invariant • Statement about the objects state between method invocations • Example: Class Student • Name != null, age >= 0 Contract based programming 5
C# language support In C# there is no direct language support for programming by contract Pre- and post-conditions, can be specified as comments to the methods Invariants can be specified as comments to the class. Pre-conditions and invariants must be checked in the beginning of all modifying methods, like set methods, etc. • The exception throw is often • • • Argument. Exception • Argument. Null. Exception, a sub-class of Argument. Exception • Argument. Out. Of. Range. Exception, a sub-class of Argument. Exception • Modern alternative: Code Contracts Contract based programming 6
Argument. Exception • Thrown if there is something wrong (according to the pre-condition) with the argument (parameter) to the method. • Some properties • Message • The error message: Should be readable to humans • Param. Name • Name of the parameter that caused the exception Some constructors • Inner. Exception • Different from null if the exception is chained • Some constructors • Argument. Exception() • No parameter: The catcher does not know what is wrong. • Don’t use … • Argument. Exception(String message) • The catcher can use the message to write to the user, etc. • Argument. Exception(String message, String param. Name) • Argument. Exception(String message, Exception inner. Exception) • Useful for exception chaining Contract based programming 7
Argument. Null. Exception • Thrown when null reference is thrown is passed to a method that does not accept it as a valid argument • http: //msdn. microsoft. com/enus/library/System. Argument. Null. Exception(v=vs. 110). aspx • Some properties • Like base class: Argument. Exception • Constructors • Argument. Null. Exception() • Default message, no parameter name set • Argument. Null. Exception(String param. Name) • Default message with parameter name. • Use this for most cases! • Argument. Null. Exception(String message, String param. Name) • Argument. Null. Exception(String message, Exception inner. Exception) Contract based programming 8
Argument. Out. Of. Range. Exception • Thrown if the actual value of an argument (aka parameter) is outside the legal range • Example: Teacher salary, legal range >= 0 • Some properties • Like base class Argument. Exception • Actual. Value: The actual value of the parameter • Some constructors • Argument. Out. Of. Range. Exception() • Do not use! No information about the problem. • Argument. Out. Of. Range. Exception(String param. Name) • Argument. Out. Of. Range. Exception(String param. Name, String message) • Argument. Out. Of. Range. Exception(String param. Name, Object actual. Value, String message) • Argument. Out. Of. Range. Exception(String message, Exception inner. Exception) • Useful for exception chaining Contract based programming 9
Some aliases • Contract based programming aka. • Bertrand Meier: The Eiffel programming language, 1986 • Design by contract aka. • Registered trademark (US) • Programming by contract aka. • Design-by-contract programming aka. • Code contracts [Microsoft terms] Contract based programming 10
References and further readings • Wikipedia Design by contract • http: //en. wikipedia. org/wiki/Design_by_contract • Microsoft Research Code Contracts • http: //research. microsoft. com/en-us/projects/contracts/ Contract based programming 11
- Slides: 11