IMPORTANT NOTE Some parts of these section slides deal with null ints. This was a mistake, as primitives cannot be null. These issues have been corrected.

Slides:



Advertisements
Similar presentations
Exceptions CSE301 University of Sunderland Harry Erwin, PhD.
Advertisements

Karlstad University Computer Science Design Contracts and Error Management Design Contracts and Errors A Software Development Strategy Eivind J. Nordby.
Data Abstraction II SWE 619 Software Construction Last Modified, Spring 2009 Paul Ammann.
Error Management with Design Contracts Karlstad University Computer Science Error Management with Design Contracts Eivind J. Nordby, Martin Blom, Anna.
1 Design by Contract Building Reliable Software. 2 Software Correctness Correctness is a relative notion  A program is correct with respect to its specification.
Component-Based Software Engineering Components and Interfaces Paul Krause.
Software Testing and Quality Assurance
Object-Oriented Analysis and Design Lecture 8 State Space, Behavior, and Type Conformance.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 8: Classes and Objects.
Karlstad University Computer Science Design Contracts and Error Management Design Contracts and Errors A Software Development Strategy (anpassad för PUMA)
CSC 395 – Software Engineering Lecture 21: Overview of the Term & What Goes in a Data Dictionary.
Eiffel Language and Design by Contract Contract –An agreement between the client and the supplier Characteristics –Expects some benefits and is prepared.
Computer Science 340 Software Design & Testing Design By Contract.
Component-Based Software Engineering Components and Interfaces Paul Krause.
Procedure specifications CSE 331. Outline Satisfying a specification; substitutability Stronger and weaker specifications - Comparing by hand - Comparing.
Contract based programming Using pre- and post-conditions, and object invariants Contract based programming1.
SWE 619 © Paul Ammann Procedural Abstraction and Design by Contract Paul Ammann Information & Software Engineering SWE 619 Software Construction cs.gmu.edu/~pammann/
Utilities (Part 2) Implementing static features 1.
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.
© Paul Ammann, 2008 Design by Contract Paul Ammann CS/SWE 332.
90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. 1 Lecture 1: Introduction Data.
1  lecture slides online
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
Building Java Programs Chapter 15 Lecture 15-2: testing ArrayIntList; pre/post conditions and exceptions reading:
Karlstad University Computer Science Design Contracts and Error Management External and internal errors and their contracts.
CSSE501 Object-Oriented Development. Chapter 10: Subclasses and Subtypes  In this chapter we will explore the relationships between the two concepts.
Chapter 7 Programming by contract: preconditions and postconditions.
Cs2220: Engineering Software Class 12: Substitution Principle Fall 2010 University of Virginia David Evans.
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.
Effective Java, Chapter 9: Exceptions Items Last modified Fall 2012 Paul Ammann.
CSE 374 Programming Concepts & Tools Hal Perkins Fall 2015 Lecture 17 – Specifications, error checking & assert.
Testing Verification and the Joy of Breaking Code
Design by Contract Jim Fawcett CSE784 – Software Studio
Design by Contract Jim Fawcett CSE784 – Software Studio
Topics: jGRASP editor ideosyncrasies assert debugger.
CSE 374 Programming Concepts & Tools
Software Development Handing Errors and Creating Documentation
CS1101: Programming Methodology Recitation 7 – Exceptions
CSE 143 Error Handling [Section 2.8] 3/30/98 CSE 143.
Preconditions precondition: Something your method assumes is true at the start of its execution. Often documented as a comment on the method's header:
Design by Contract Fall 2016 Version.
SWE 332 Last Modified Spring 2010 Paul Ammann
Preconditions, Postconditions & Assertions
Lecture 15: Concurring Concurrently CS201j: Engineering Software
Introduction to Data Structures
Announcements General rules about homeworks
Defining New Types of Objects, part 3
Object initialization: constructors
Section 6: Midterm Review
Effective Java, 3rd Edition Chapter 10: Exceptions
Defining Classes and Methods
Data Structures and Algorithms for Information Processing
Section 1: Code Reasoning
CSE 143 Lecture 4 More ArrayIntList:
Announcements Homework 1 will be assigned this week,
Warmup ["hip","hip"] = Hip Hip Array! ???.
Go to pollev.com/cse143.
CSE 143 Lecture 5 More ArrayIntList:
Announcements General rules about homeworks
Assertions References: internet notes; Bertrand Meyer, Object-Oriented Software Construction; 4/25/2019.
SWE 619 Last modified Fall 2007 Saket Kaushik, Paul Ammann
Effective Java, Chapter 9: Exceptions
Lecture 13: Subtyping Rules Killer Bear Climber
Computer Science 340 Software Design & Testing
Software Specifications
CIS 110: Introduction to Computer Programming
Design Contracts and Errors A Software Development Strategy
Presentation transcript:

IMPORTANT NOTE Some parts of these section slides deal with null ints. This was a mistake, as primitives cannot be null. These issues have been corrected.

Section 1: Intro and Specifications CSE 331 AUT18 Section 1: Intro and Specifications

IntelliJ Setup Homework will be posted later today Instructions for setup are posted If you try to follow them but run into problems, please come to office hours!

Welcome to section! We meet once a week on Thursdays Different TAs teach section each week Section is not optional! Section is a supplement to lecture It gives you a chance to practice the material and make sure you understand it Sometimes, section may contain material that is not in lectures

What is a specification? How you tell the client what your code does A “contract” between the developer and the user The developer promises to fulfill the specification The user agrees to only rely on functionality defined in the specification

Why do we need Specifications? Other people use your code! Is it a good idea to share all of your source code with everyone who uses it? You don’t have a perfect memory Can you remember the details of a program you wrote 6 months ago? They encourage easy and understandable code

Format of a Specification /** *@spec.requires *@spec.modifies *@spec.effects *@return *@throws **/ methodName {…

Format of a Specification PRECONDITION What your method requires to be true before it is called If the precondition is not met, there are no guarantees on the method’s behavior /** *@spec.requires *@spec.modifies *@spec.effects *@return *@throws **/ methodName {… POSTCONDITION Guarantees the implementor makes about the program state after the method is called If the preconditions are satisfied, the postconditions must hold

Format of a Specification PRECONDITION What your method requires to be true before it is called If the precondition is not met, there are no guarantees on the method’s behavior /** *@spec.requires *@spec.modifies *@spec.effects *@return *@throws **/ methodName {… POSTCONDITION Guarantees the implementor makes about the program state after the method is called If the preconditions are satisfied, the postconditions must hold

Format of a Specification PRECONDITION What your method requires to be true before it is called If the precondition is not met, there are no guarantees on the method’s behavior /** *@spec.requires – Spells out properties the client must satisfy before the method is called *@spec.modifies *@spec.effects *@return *@throws **/ methodName {… POSTCONDITION Guarantees the implementor makes about the program state after the method is called If the preconditions are satisfied, the postconditions must hold

Format of a Specification PRECONDITION What your method requires to be true before it is called If the precondition is not met, there are no guarantees on the method’s behavior /** *@spec.requires – Spells out properties the client must satisfy before the method is called *@spec.modifies – Lists objects that may be altered by the method *@spec.effects – Gives guarantees on how objects are modified by the method *@return – Gives what the method returns *@throws – Lists exceptions thrown by the method **/ methodName {… POSTCONDITION Guarantees the implementor makes about the program state after the method is called If the preconditions are satisfied, the postconditions must hold

Satisfying Specifications An implementation M satisfies a specification S if: When all the preconditions of S are met, All the postconditions of S are satisfied by M after it executes

Satisfying Specifications /** Computes the area of a rectangle with *width w and length l *@returns the area of a rectangle with width *w and length l **/ Public int area(int w, int l) { if (w < 0 || l < 0) { throw new IllegalArgumentException(); } return l * w; Does the implementation satisfy this specification?

Satisfying Specifications /** Computes the area of a rectangle with *width w and length l *@returns the area of a rectangle with width *w and length l **/ Public int area(int w, int l) { if (w < 0 || l < 0) { throw new IllegalArgumentException(); } return l * w; Does the implementation satisfy this specification? No! – The specification is violated when w and/or l are negative

Satisfying Specifications /** Computes the area of a rectangle with *width w and length l *@returns the area of a rectangle with width *w and length l *@throws IllegalArgumentException if w < 0 or l < 0 **/ Public int area(int w, int l) { if (w < 0 || l < 0) { throw new IllegalArgumentException(); } return l * w; Does the implementation satisfy this specification?

Satisfying Specifications /** Computes the area of a rectangle with *width w and length l *@returns the area of a rectangle with width *w and length l *@throws IllegalArgumentException if w < 0 or l < 0 **/ Public int area(int w, int l) { if (w < 0 || l < 0) { throw new IllegalArgumentException(); } return l * w; Does the implementation satisfy this specification? Yes! – The specification matches the implementation

Satisfying Specifications /** Computes the area of a rectangle with *width w and length l *@spec.requires w >= 0 and l >= 0 *@returns the area of a rectangle with width *w and length l **/ Public int area(int w, int l) { if (w < 0 || l < 0) { throw new IllegalArgumentException(); } return l * w; Does the implementation satisfy this specification?

Satisfying Specifications /** Computes the area of a rectangle with *width w and length l *@spec.requires w >= 0 and l >= 0 *@returns the area of a rectangle with width *w and length l **/ Public int area(int w, int l) { if (w < 0 || l < 0) { throw new IllegalArgumentException(); } return l * w; Does the implementation satisfy this specification? Yes! – Even though we didn’t document the IllegalArgumentException in the @throws tag, since we require w and l to be non-negative, we aren’t obligated to specify what happens when the user enters a negative w or l

Stronger vs Weaker Specifications A specification R is stronger than another specification S if: Every implementation that satisfies R also satisfies S R has a weaker precondition and/or a stronger postcondition Both of these definitions are equivalent!

Stronger vs Weaker Preconditions A precondition is weaker when it requires less from the user: Less requirements in the @spec.requires tag

Stronger vs Weaker Preconditions A precondition is weaker when it requires less from the user: Less requirements in the @spec.requires tag Which specification has a weaker precondition? /** *@spec.requires x > 0 *@return x **/ /** *@return x if x > 0, -x if x <= 0 **/

Stronger vs Weaker Preconditions A precondition is weaker when it requires less from the user: Less requirements in the @spec.requires tag Which specification has a weaker precondition? /** *@spec.requires x > 0 *@return x **/ /** *@return x if x > 0, -x if x <= 0 **/

Stronger vs Weaker Postconditions A postcondition is stronger when it makes more guarantees on the final program state after execution

Stronger vs Weaker Postconditions A postcondition is stronger when it makes more guarantees on the final program state after execution Less objects in the @spec.modifies tag @spec.effects is harder to satisfy @returns is harder to satisfy Use a subtype of an exception in@throws

Stronger vs Weaker Postconditions A postcondition is stronger when it makes more guarantees on the final program state after execution Less objects in the @spec.modifies tag @spec.effects is harder to satisfy @returns is harder to satisfy @throws Which specification has the stronger postcondition? /** *@spec.requires x > 0 *@return x **/ /** *@return x if x > 0, -x if x <= 0 **/

Stronger vs Weaker Postconditions A postcondition is stronger when it makes more guarantees on the final program state after execution Less objects in the @spec.modifies tag @spec.effects is harder to satisfy @returns is harder to satisfy Throw a more specific exception (a subtype) in @throws Which specification has the stronger postcondition? /** *@spec.requires x > 0 *@return x **/ /** *@return x if x > 0, -x if x <= 0 **/

Stronger vs Weaker Specifications Which specification is stronger? /** *@return x *@throws IllegalArgumentException if x <= 0 **/ /** *@return x if x > 0, -x if x <= 0 **/

Stronger vs Weaker Specifications Which specification is stronger? /** *@return x *@throws IllegalArgumentException if x <= 0 **/ /** *@return x if x > 0, -x if x <= 0 **/ Both have different behavior for x <= 0. They are both incomparable.

Worksheet