Computer Science 340 Software Design & Testing

Slides:



Advertisements
Similar presentations
Design by Contract.
Advertisements

Carlos D. Rivera February 28, 2007 Design-by-Contract.
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.
1. Define the concept of assertions. 1 Explain the use of assertions. 2 Create Java program using assertions. 3 Run Java program using assertions. 4 2.
Detecting Bugs Using Assertions Ben Scribner. Defining the Problem  Bugs exist  Unexpected errors happen Hardware failures Loss of data Data may exist.
1 CSE 403 Design by Contract Reading: Pragmatic Programmer Ch. 4, Object-Oriented Design and Patterns, Ch. 3 (Horstmann) These lecture slides are copyright.
Design by Contract. Design by contract is the process of developing software based on the notion of contracts between objects, which are expressed as.
Data Abstraction II SWE 619 Software Construction Last Modified, Spring 2009 Paul Ammann.
An Introduction to Java Programming and Object- Oriented Application Development Chapter 8 Exceptions and Assertions.
 Both System.out and System.err are streams—a sequence of bytes.  System.out (the standard output stream) displays output  System.err (the standard.
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,
The Java Assert Statement. 2 Assert A Java statement in JDK 1.4 & newer Intent: enables code to test assumptions. E.g., a method that calculates a particle’s.
Software Testing and Quality Assurance
Static and Dynamic Contract Verifiers For Java Hongming Liu.
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.
Lecturer: Dr. AJ Bieszczad Chapter 76-1 Software engineering standards Standards for you Standards for others Matching design with implementation.
Computer Science 340 Software Design & Testing Design By Contract.
Ranga Rodrigo. Class is central to object oriented programming.
Unit Testing & Defensive Programming. F-22 Raptor Fighter.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 18 Exception Handling.
Assertions Program correctness. Assertions Java statement – enables you to assert an assumption about your program. – An assertion contains a Boolean.
07 Coding Conventions. 2 Demonstrate Developing Local Variables Describe Separating Public and Private Members during Declaration Explore Using System.exit.
Errors And How to Handle Them. GIGO There is a saying in computer science: “Garbage in, garbage out.” Is this true, or is it just an excuse for bad programming?
Overview of Formal Methods. Topics Introduction and terminology FM and Software Engineering Applications of FM Propositional and Predicate Logic Program.
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.
CS 261 – Data Structures Preconditions, Postconditions & Assert.
Introduction to Exception Handling and Defensive Programming.
Software Documentation Section 5.5 ALBING’s Section JIA’s Appendix B JIA’s.
Programming with Assertions © Allan C. Milne v
Pre- and postconditions, Using assertions and exceptions 1 Pre- and postconditions Using assertions and exceptions.
Design - programming Cmpe 450 Fall Dynamic Analysis Software quality Design carefully from the start Simple and clean Fewer errors Finding errors.
(c) University of Washington13-1 CSC 143 Java Specifications: Programming by Contract Reading: Ch. 5.
Exceptions and Assertions Chapter 15 – CSCI 1302.
Class Design I Class Contracts Readings: 2 nd Ed: Section 9.5, Advanced Topic nd Ed: Section 8.5, Advanced Topic 8.2 Some ideas come from: “Practical.
ANU COMP2110 Software Design in 2003 Lecture 10Slide 1 COMP2110 Software Design in 2004 Lecture 12 Documenting Detailed Design How to write down detailed.
SWE 4743 Abstract Data Types Richard Gesick. SWE Abstract Data Types Object-oriented design is based on the theory of abstract data types Domain.
Defensive Programming CNS 3370 Copyright 2003, Fresh Sources, Inc.
PROGRAMMING PRE- AND POSTCONDITIONS, INVARIANTS AND METHOD CONTRACTS B MODULE 2: SOFTWARE SYSTEMS 13 NOVEMBER 2013.
Programming & Debugging. Key Programming Issues Modularity Modifiability Ease of Use Fail-safe programming Style Debugging.
The Java Assertion. 2 Assertion A Java statement in JDK 1.4 & newer Intent: enables code to test assumptions. E.g., a method that calculates the a particle’s.
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.
Effective Java, Chapter 9: Exceptions Items Last modified Fall 2012 Paul Ammann.
Object Design More Design Patterns Object Constraint Language Object Design Specifying Interfaces Review Exam 2 CEN 4010 Class 18 – 11/03.
Defensive Programming. Good programming practices that protect you from your own programming mistakes, as well as those of others – Assertions – Parameter.
Design by Contract. The Goal Ensure the correctness of our software (correctness) Recover when it is not correct anyway (robustness) Correctness: Assertions.
Reasoning and Design (and Assertions). How to Design Your Code The hard way: Just start coding. When something doesn’t work, code some more! The easier.
Data Abstraction: The Walls
Chapter 6 CS 3370 – C++ Functions.
Used to help understand requirements more completely
Logger, Assert and Invariants
The Software Development Cycle
Chapter 8 – Software Testing
Topics: jGRASP editor ideosyncrasies assert debugger.
CSE 374 Programming Concepts & Tools
CSE 143 Error Handling [Section 2.8] 3/30/98 CSE 143.
Design by Contract Fall 2016 Version.
Introduction to Data Structures
CSC 143 Error Handling Kinds of errors: invalid input vs programming bugs How to handle: Bugs: use assert to trap during testing Bad data: should never.
Effective Java, 3rd Edition Chapter 10: Exceptions
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
CS-1020 and Exception Handling
Contract-Based Programming with/without Ada 2012
Programming Languages 2nd edition Tucker and Noonan
Software Specifications
The Software Development Cycle
Defensive Programming
Presentation transcript:

Computer Science 340 Software Design & Testing Design By Contract

Abstract Data Types Object-oriented design is based on the theory of abstract data types Domain and implementation concepts are modeled in software as ADTs Interfaces capture the essence of an ADT Examples: NetworkDevice, Stack Classes provide implementations of ADTs

Abstract Data Types Even if a class doesn’t implement a pre-defined interface, it still defines an ADT as embodied in its public interface In this case, the ADT definition is combined with its implementation Explicit interfaces are used if we expect to have multiple implementations of the ADT interface List, class ArrayList, class LinkedList, … If we expect an ADT to have only one implementation, we often combine the ADT definition and implementation in the same class Example: Matrix

Defining ADTs ADT = syntax + semantics Each ADT operation has: name, parameter list, return type ADT clients must conform to this syntax, or they will fail to compile Each ADT operation also has semantics: What is the meaning of the operation? What does it do? Classes that implement ADTs must faithfully adhere to operation semantics as well as syntax What would happen if ArrayStack’s push implementation fires a nuclear missile rather than pushing a value on the stack?

Defining ADTs Source code precisely defines ADT syntax Compilers enforce ADT syntax Source code does not precisely define semantics Compilers cannot enforce semantics ADT semantics are typically defined by comments in the code, if they’re defined at all Comment each operation explaining what it does Example: Stack, Matrix

Defining ADTs Imprecise or incomplete definitions of ADT semantics lead to reliability problems: Clients and implementers have different ideas of what an operation does Differing assumptions lead to defects Reliability is even more important in object-oriented programming than elsewhere. Why?

Design By Contract DBC is a technique for more precisely defining ADT semantics, thus preventing misunderstandings DBC is based on the real-world notion of a legal contract A contract involves a “client” and a “supplier” Each side has obligations and expected benefits, which are precisely defined in the contract If a party performs their obligations, they are guaranteed to receive the promised benefits

Defining operation semantics: Pre-conditions & Post-conditions An ADT is a contract between client and supplier Each operation has Pre-conditions and Post-conditions Pre-conditions are the client’s obligations Post-conditions are the supplier’s obligations The pre-conditions and post-conditions define the semantics of the operation Examples: Stack, Matrix

Defining operation semantics: Pre-conditions & Post-conditions If a client invokes an operation having satisfied all pre-conditions, the supplier must ensure that all post-conditions are met upon return If the client did not satisfy all pre-conditions, the supplier is under no obligation to satisfy the post-conditions. The supplier could: Return an error or throw an exception Just fail (crash, return bad results, etc.)

Exceptions What if the caller satisfied the pre-conditions, but for some reason the supplier is unable to satisfy the post-conditions? The supplier throws an exception or returns an error Why might a supplier fail to satisfy the post-conditions? External factors beyond supplier’s control (hard disk crash, Internet down, etc.) If the client fails to meet pre-conditions, the supplier can throw an exception or return an error if it wants to, but isn’t required to

Exceptions If an operation could throw an exception or return an error, the contract should explicitly define the possible exceptions/errors so clients can handle them

Class Invariants Pre and Post-conditions apply only to single operations Some supplier obligations apply to every operation on the ADT Class Invariants are post-conditions that apply to every operation Examples: Stack, Matrix

Class Invariants What is a constructor’s job? Constructors must establish all class invariants When a constructor completes, all class invariants must be satisfied If a constructor cannot establish the class invariants, it should throw an exception In addition to their post-conditions, public operations must also ensure that all class invariants are satisfied upon return I.E., the class invariants are ANDed with the post-conditions of every public operation Class invariants may be temporarily violated while a public operation is executing, but they must be reestablished before the operation returns

Testing How do you test? Guidelines If statements Assertions Guidelines Do not use assertions to test parameters on public methods Do not use assertions to do any work your program requires for correct operation Use for Internal invariants, control flow invariants Pre, post, invariants for private methods By default, assertions are disabled Use the JVM –ea flag to enable assertions (i.e. java –ea <ClassName>)

Java Assertion Syntax assert expression1; expression1 must evaluate to a boolean and will result in an AssertionError being thrown if false assert expression1 : expression2; Expression2 (if present) becomes the message in the AssertionError

DBC vs. Defensive Programming Defensive Programming says: Operation implementations should be bullet-proof Check all parameters for validity before using them Return error or throw exception if parameters are invalid, but never crash Puts heavy burden on the supplier Results in lots of parameter checking code Client and supplier often have redundant checks Results in more code (harder to maintain) Slows programs down (too much redundant checking)

DBC vs. Defensive Programming DBC says: Ensuring that pre-conditions are met is the client’s job Operation implementations need not contain code to verify that pre-conditions were met (e.g., no parameter checking code) If pre-conditions are not met and something unseemly occurs, it is the client’s fault, and they got what they deserved Suppliers must throw an exception if post-conditions cannot be met Puts more burden on clients Results in less and more efficient code As a debugging tool, operation implementations may include assert statements to verify that pre-conditions were met, but this is optional, and all assertions should be turned off in the final release of the software

DBC vs. Defensive Programming Designers must make a conscious choice between Defensive Programming and DBC

Documenting ADTs with javadoc Interfaces and classes Header comment @invariant (custom tag) Operations @pre (custom tag) @post (custom tag) @param @returns (this is really a post-condition) @throws

Documenting ADTs with javadoc javadoc will generate HTML documentation for your interfaces and classes Running javadoc: javadoc -tag invariant:t:"Class Invariants:" -tag pre:cm:"Pre-Conditions:" -tag post:cm:"Post-Conditions:" MyClass.java