Contract based programming Using pre- and post-conditions, and object invariants Contract based programming1.

Slides:



Advertisements
Similar presentations
Chapter 17 Failures and exceptions. This chapter discusses n Failure. n The meaning of system failure. n Causes of failure. n Handling failure. n Exception.
Advertisements

Exceptions CSE301 University of Sunderland Harry Erwin, PhD.
11-Jun-14 The assert statement. 2 About the assert statement The purpose of the assert statement is to give you a way to catch program errors early The.
A software specification indicates the task (or some aspect of the task) that is supposed to be performed when software executes. Types of Specifications.
Code Documentation. Important!  It’s for you  It’s for others.
Lecture 23 Input and output with files –(Sections 2.13, 8.7, 8.8) Exceptions and exception handling –(Chapter 17)
Exceptions Ensuring program reliability. Program correctness The term program correctness refers to a program’s working as advertised; that is, it produces.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 15: Exception Handling.
Chapter 16: Exception Handling C++ Programming: From Problem Analysis to Program Design, Fifth Edition.
 Both System.out and System.err are streams—a sequence of bytes.  System.out (the standard output stream) displays output  System.err (the standard.
Microsoft VB 2005: Reloaded, Advanced Chapter 5 Input Validation, Error Handling, and Exception Handling.
1 Design by Contract Building Reliable Software. 2 Software Correctness Correctness is a relative notion  A program is correct with respect to its specification.
Chapter 8 Designing Classes. Assignment Chapter 9 Review Exercises (Written)  R8.1 – 8.3, 8.5 – 8.7, 8. 10, 8.11, 8.13, 8.15, 8.19, 8.20 Due Friday,
Object Oriented Design An object combines data and operations on that data (object is an instance of class) data: class variables operations: methods Three.
11-Jun-15 Exceptions. 2 Errors and Exceptions An error is a bug in your program dividing by zero going outside the bounds of an array trying to use a.
Copyright W. Howden1 Lecture 13: Programming by Contract.
1 Advanced Material The following slides contain advanced material and are optional.
Chapter 11: Classes and Data Abstraction
Computer Science 340 Software Design & Testing Design By Contract.
1 Abstraction  Identify important aspects and ignore the details  Permeates software development programming languages are abstractions built on hardware.
Testing Especially Unit Testing. V-model Wikipedia:
07 Coding Conventions. 2 Demonstrate Developing Local Variables Describe Separating Public and Private Members during Declaration Explore Using System.exit.
Exceptions Syntax, semantics, and pragmatics Exceptions1.
Exceptions Handling Exceptionally Sticky Problems.
How to Design Error Steady Code Ivaylo Bratoev Telerik Corporation
Chapter 11: Classes and Data Abstraction. C++ Programming: Program Design Including Data Structures, Fourth Edition2 Objectives In this chapter, you will:
Low-Level Detailed Design SAD (Soft Arch Design) Mid-level Detailed Design Low-Level Detailed Design Design Finalization Design Document.
CS 261 – Data Structures Preconditions, Postconditions & Assert.
Chapter 14: Exception Handling. Objectives In this chapter, you will: – Learn what an exception is – Learn how to handle exceptions within a program –
Programming with Assertions © Allan C. Milne v
Pre- and postconditions, Using assertions and exceptions 1 Pre- and postconditions Using assertions and exceptions.
Cs205: engineering software university of virginia fall 2006 David Evans Substitution Principle.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
Chapter 10: Classes and Data Abstraction. Objectives In this chapter, you will: Learn about classes Learn about private, protected, and public members.
(3-1) Functions II H&K Chapter 3 Instructor - Andrew S. O’Fallon CptS 121 (September 9, 2015) Washington State University.
Computer Science 209 Software Development Handing Errors and Creating Documentation.
Exceptions in C++. Exceptions  Exceptions provide a way to handle the errors generated by our programs by transferring control to functions called handlers.
ANU COMP2110 Software Design in 2003 Lecture 10Slide 1 COMP2110 Software Design in 2004 Lecture 12 Documenting Detailed Design How to write down detailed.
L13: Design by Contract Definition Reliability Correctness Pre- and post-condition Asserts and Exceptions Weak & Strong Conditions Class invariants Conditions.
Chapter 15: Exception Handling C++ Programming: Program Design Including Data Structures, Fifth Edition.
SWE 4743 Abstract Data Types Richard Gesick. SWE Abstract Data Types Object-oriented design is based on the theory of abstract data types Domain.
CSE 143 Lecture 4 More ArrayIntList : Pre/postconditions; exceptions; testing reading: slides created by Marty Stepp and Hélène Martin
Defensive Programming CNS 3370 Copyright 2003, Fresh Sources, Inc.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 12: Classes and Data Abstraction.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 05: Classes and Data Abstraction.
Microsoft Code Contracts How to program Pre-conditions, Post-conditions, and Object Invariants Microsoft Code Contracts1.
Chapter 2 Comments, Conditions, Assertions Comments Preconditions Postconditions Assertions.
Chapter 7 Programming by contract: preconditions and postconditions.
Chapter 10: Classes and Data Abstraction. Classes Object-oriented design (OOD): a problem solving methodology Objects: components of a solution Class:
Defining Classes I Part B. Information hiding & encapsulation separate how to use the class from the implementation details separate how to use the class.
DBC NOTES. Design By Contract l A contract carries mutual obligations and benefits. l The client should only call a routine when the routine’s pre-condition.
Defensive Programming. Good programming practices that protect you from your own programming mistakes, as well as those of others – Assertions – Parameter.
2.4 Exceptions n Detects try { //code that may raise an exception and/or set some condition if (condition) throw exceptionName; //Freq. A string } n Handles.
Chapter 6 CS 3370 – C++ Functions.
Logger, Assert and Invariants
Handling Exceptionally Sticky Problems
Topics: jGRASP editor ideosyncrasies assert debugger.
Software Development Handing Errors and Creating Documentation
Chapter 3: Using Methods, Classes, and Objects
Syntax, semantics, and pragmatics
Chapter 14: Exception Handling
Methods The real power of an object-oriented programming language takes place when you start to manipulate objects. A method defines an action that allows.
Exception Handling Chapter 9.
Exception Handling Chapter 9 Edited by JJ.
Part B – Structured Exception Handling
Programming in C# Lesson 5. Exceptions..
Go to pollev.com/cse143.
Assertions References: internet notes; Bertrand Meyer, Object-Oriented Software Construction; 4/25/2019.
Handling Exceptionally Sticky Problems
Unit Testing.
Presentation transcript:

Contract based programming Using pre- and post-conditions, and object invariants Contract based programming1

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 programming2

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 programming3

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.ToInt32(String str) Assumes str is a string that contains number. If not it throws FormatException, or OverflowException Post-condition What does the method guarantee? After the method has executed Returned value, and/or change of object state Contract based programming4

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 programming5

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 ArgumentException ArgumentNullException, a sub-class of ArgumentException ArgumentOutOfRangeException, a sub-class of ArgumentException Contract based programming6

ArgumentException 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 ParamName Name of the parameter that caused the exception Some constructors InnerException Different from null if the exception is chained Some constructors ArgumentException() No parameter: The catcher does not know what is wrong. Don’t use … ArgumentException(String message) The catcher can use the message to write to the user, etc. ArgumentException(String message, String paramName) ArgumentException(String message, Exception innerException) Useful for exception chaining Contract based programming7

ArgumentNullException Thrown when null reference is thrown is passed to a method that does not accept it as a valid argument us/library/System.ArgumentNullException(v=vs.110).aspx us/library/System.ArgumentNullException(v=vs.110).aspx Some properties Like base class: ArgumentException Constructors ArgumentNullException() Default message, no parameter name set ArgumentNullException(String paramName) Default message with parameter name. Use this for most cases! ArgumentNullException(String message, String paramName) ArgumentNullException(String message, Exception innerException) Contract based programming8

ArgumentOutOfRangeException 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 ArgumentException ActualValue: The actual value of the parameter Some constructors ArgumentOutOfRangeException() Do not use! No information about the problem. ArgumentOutOfRangeException(String paramName) ArgumentOutOfRangeException(String paramName, String message) ArgumentOutOfRangeException(String paramName, Object actualValue, String message) ArgumentOutOfRangeException(String message, Exception innerException) Useful for exception chaining Contract based programming9

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 programming10

References and further readings Wikipedia Design by contract Microsoft Research Code Contracts 1ce455f ce455f66970 Contract based programming11