Microsoft Code Contracts How to program Pre-conditions, Post-conditions, and Object Invariants Microsoft Code Contracts1.

Slides:



Advertisements
Similar presentations
Program Verification Using the Spec# Programming System ETAPS Tutorial K. Rustan M. Leino, Microsoft Research, Redmond Rosemary Monahan, NUIM Maynooth.
Advertisements

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.
The Substitution Principle SWE 332 – Fall Liskov Substitution Principle In any client code, if subtype object is substituted for supertype object,
Microsoft VB 2005: Reloaded, Advanced Chapter 5 Input Validation, Error Handling, and Exception Handling.
Formal Methods of Systems Specification Logical Specification of Hard- and Software Prof. Dr. Holger Schlingloff Institut für Informatik der Humboldt.
K. Rustan M. Leino Microsoft Research, Redmond, WA, USA with Mike Barnett, Robert DeLine, Manuel Fahndrich, and Wolfram Schulte Toward enforceable contracts.
Page 1 Building Reliable Component-based Systems Chapter 6 - Semantic Integrity in Component Based Development Chapter 6 Semantic Integrity in Component.
Declaring and Checking Non-null Types in an Object-Oriented Language Authors: Manuel Fahndrich K. Rustan M. Leino OOPSLA’03 Presenter: Alexander Landau.
Unit testing C# classes “If it isn’t tested it doesn’t work” Unit testing C# classes1.
ASP.NET Validating user input Validating user input on the client and/or server side 1ASP.NET Validating User Input.
1 Chapter 8 Scope, Lifetime, and More on Functions Dale/Weems/Headington.
Subclasses and Subtypes CMPS Subclasses and Subtypes A class is a subclass if it has been built using inheritance. ▫ It says nothing about the meaning.
Live Tiles Yvan Ngneunmeu, Cameroon Microsoft Student Partners Lead
Computer Science 340 Software Design & Testing Design By Contract.
Ranga Rodrigo. Class is central to object oriented programming.
Web Site Navigation How to get around in an ASP.NET web application 1Web Site Navigation.
Computer Programming and Basic Software Engineering 9 Building Graphical User Interface Working with Unmanaged Code.
Events in C# Events in C#.
Events in C# MHA Delegates vs. Events Delegates can be used as events Example CountDownTimerEvent -> CountDownDelegate But have certain problems.
Lecture 1 Programming in C# Introducing C# Writing a C# Program.
Chapter 12: Using Controls. Examining the IDE’s Automatically Generated Code A new Windows Forms project has been started and given the name FormWithALabelAndAButton.
Exceptions1 Syntax, semantics, and pragmatics. Exception create If (some error){ throw new SomeException(”some message”); } Exceptions2.
Database Programming Dr. John Abraham. Data Sources Data source specifies the source of the data for an application. Click on Data from Menu Choose show.
11 Web Services. 22 Objectives You will be able to Say what a web service is. Write and deploy a simple web service. Test a simple web service. Write.
Contract based programming Using pre- and post-conditions, and object invariants Contract based programming1.
Program documentation Using the Doxygen tool Program documentation1.
Program documentation using the Javadoc tool 1 Program documentation Using the Javadoc tool.
1 Assertions. 2 assertions communicate assumptions about the state of the program, and stop processing if they turn out to be false very often comments.
Utilities (Part 2) Implementing static features 1.
Visual Studio 2010 and.NET Framework 4 Training Workshop.
Interfaces 1. Interfaces are (parts of) contracts Interfaces are contracts between implementers and consumers Consumers: Programmers using a class implementing.
FEN UCN T&B - PBA/CodeContract- Intro 1 Code Contract Introduction Specification of a Person Class.
Code Contracts Parameterized Unit Tests Tao Xie. Example Unit Test Case = ? Outputs Expected Outputs Program + Test inputs Test Oracles 2 void addTest()
CSE 425: Data Types I Data and Data Types Data may be more abstract than their representation –E.g., integer (unbounded) vs. 64-bit int (bounded) A language.
Using Microsoft Visual Studio 2005 Original by Suma Rao Revised by John G. McMahon ( 9/6/2008 )
Joe Hummel, PhD Dept of Mathematics and Computer Science Lake Forest College
Programming with Visual Studio 2005.NET A short review of the process.
Visual C# 2012 How to Program © by Pearson Education, Inc. All Rights Reserved.
Programming with Visual Studio.NET A short review of the process.
Visual C# 2012 How to Program © by Pearson Education, Inc. All Rights Reserved.
Pre- and postconditions, Using assertions and exceptions 1 Pre- and postconditions Using assertions and exceptions.
Observer design pattern A closer look at INotifyPropertyChanged, INotifyPropertyChanging and ObservableCollection Observer design pattern1.
1 Programming Environment and Tools VS.Net 2012 First project MSDN Library.
Spec# Andreas Vida. Motivation Correct and maintainable software Correct and maintainable software Cost effective software production Cost effective software.
 Asserting Expectations. Introduction -Observation alone is not enough for debugging as it can be a burden for a programmer. -One must compare observed.
Generics in C# 1. Generics List vs. non-generic ArrayList Generic List Namespace System.Collections.Generic List list = new List (); List.add(”Anders”);
L13: Design by Contract Definition Reliability Correctness Pre- and post-condition Asserts and Exceptions Weak & Strong Conditions Class invariants Conditions.
SWE 4743 Abstract Data Types Richard Gesick. SWE Abstract Data Types Object-oriented design is based on the theory of abstract data types Domain.
This is how you invoke the Microsoft Visual Studio 2010 Software. All Programs >> Microsoft Visual Studio 2010.
PROGRAMMING PRE- AND POSTCONDITIONS, INVARIANTS AND METHOD CONTRACTS B MODULE 2: SOFTWARE SYSTEMS 13 NOVEMBER 2013.
Web Development in Microsoft Visual Studio 2013 / 2015.
Installing Microsoft C++ Microsoft Visual Studio 6.0.
Open project in Microsoft Visual Studio → build program in “Release” mode.
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.
David Evans CS201j: Engineering Software University of Virginia Computer Science Lecture 10: Programming Exceptionally.
Visual Basic.NET BASICS Lesson 14 Menus and Printing.
CPSC 252 ADTs and C++ Classes Page 1 Abstract data types (ADTs) An abstract data type is a user-defined data type that has: private data hidden inside.
Chapter 6 CS 3370 – C++ Functions.
Delegates/ Anders Børjesson
Program documentation
Quick Start Guide for Visual Studio 2010
Code Contracts and Pex Peli de Halleux, Nikolai Tillmann
1. Open Visual Studio 2008.
Exercise 11.1 Write a code fragment that performs the same function as the statement below without using the crash method Toolbox.crash(amount < 0,
Generics in C# / Anders Børjesson
Lab 1 Introduction to C++.
Creating Your First C Program Using Visual Studio 2010
Creating Your First C Program Using Visual Studio 2010
Double click Microsoft Visual Studio 2010 on the Computer Desktop
Java Modeling Language (JML)
Presentation transcript:

Microsoft Code Contracts How to program Pre-conditions, Post-conditions, and Object Invariants Microsoft Code Contracts1

Specifying Object Invariants Annotate a method [ContractInvariantMethod] Inside the method you specify the object invariants [ContractInvariantMethod] private void ObjectInvariant() { Contract.Invariant(Salary >= 0); // Salary is a property Contract.Invariant(!String.IsNullOrEmpty(Name)); } If you write “hard” object invariants you may have to make an explicit constructor to initialize properties to legal values In case the default values are not legal Microsoft Code Contracts2

The class Contract Specifying pre- and post-conditions Namespace System.Diagnostics.Contracts us/library/system.diagnostics.contracts.contract(v=vs.110).aspx us/library/system.diagnostics.contracts.contract(v=vs.110).aspx Some interesting static methods Requires(Boolean expression) Used to specify pre-condition Comes in 4 variations With / without message With / without specific exception The default exception is ContractException. This exception is not public, so you cannot catch it! Normal exceptions are handy when you Unit test Ensures(Boolean expression) Used to specify post-condition Comes in 4 variations … (like Requires) EnsuresOnThrow (…) Microsoft Code Contracts3

Static vs. run-time analysis Static analysis Conditions and invariants are checked at compile-time Violations shown in Visual Studio (underlining) Run-time analysis Conditions and invariants are checked at run-time. Violations reported as messages or exceptions Microsoft Code Contracts4

Configuration: Plugin needed Code Contracts can be configured in many ways To do this you need a plugin for Visual Studio Code Contracts for.NET msdn.microsoft.com/1ec 7db c9-851f- 1ce455f msdn.microsoft.com/1ec 7db c9-851f- 1ce455f66970 In Visual studio right click a project and you’ll see an extra menu item “Code Contracts” Microsoft Code Contracts5

References and further readings Nagel et al. Professional C# 5.0 and.NET 4.5.1, Wrox 2014 Code Contracts, page Microsoft Research: Code Contracts MSDN Code Contracts MSDN Contract Class us/library/system.diagnostics.contracts.contract(v=vs.110).aspx us/library/system.diagnostics.contracts.contract(v=vs.110).aspx Code Contracts for.NET Plugin for Visual Studio 1ce455f ce455f66970 Microsoft Code Contracts6