Pre- and postconditions, Using assertions and exceptions 1 Pre- and postconditions Using assertions and exceptions.

Slides:



Advertisements
Similar presentations
Exceptions CSE301 University of Sunderland Harry Erwin, PhD.
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.
Rigorous Software Development CSCI-GA Instructor: Thomas Wies Spring 2012 Lecture 8.
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.
A software specification indicates the task (or some aspect of the task) that is supposed to be performed when software executes. Types of Specifications.
The Fundamental Rule for Testing Methods Every method should be tested in a program in which every other method in the testing program has already been.
Lecture 23 Input and output with files –(Sections 2.13, 8.7, 8.8) Exceptions and exception handling –(Chapter 17)
An Introduction to Java Programming and Object- Oriented Application Development Chapter 8 Exceptions and Assertions.
Exception Handling and Format output
 Both System.out and System.err are streams—a sequence of bytes.  System.out (the standard output stream) displays output  System.err (the standard.
Exceptions1 Syntax, semantics, and pragmatics. Exceptions2 Syntax, semantics, pragmatics Syntax –How it looks, i.e. how we have to program to satisfy.
Variable types We have already encountered the idea of a variable type. It is also possible to think about variables in terms of their kind, namely: 1)
Copyright © 2006 Addison-Wesley. All rights reserved.1-1 ICS 410: Programming Languages Chapter 3 : Describing Syntax and Semantics Axiomatic Semantics.
ISBN Chapter 3 Describing Syntax and Semantics.
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.
Software Engineering and Design Principles Chapter 1.
ESC Java. Static Analysis Spectrum Power Cost Type checking Data-flow analysis Model checking Program verification AutomatedManual ESC.
OOP #10: Correctness Fritz Henglein. Wrap-up: Types A type is a collection of objects with common behavior (operations and properties). (Abstract) types.
Describing Syntax and Semantics
Computer Science 340 Software Design & Testing Design By Contract.
Component-Based Software Engineering Components and Interfaces Paul Krause.
Ranga Rodrigo. Class is central to object oriented programming.
CS 240 Week 3. List’Em java Grep [-r] directoryName fileSelectionPattern substringSelectionPattern Run Demo java LineCount [-r] directoryName fileSelectionPattern.
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.
Contract based programming Using pre- and post-conditions, and object invariants Contract based programming1.
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.
Design by Contract in Java Concept and Comparison.
Chapter 25 Formal Methods Formal methods Specify program using math Develop program using math Prove program matches specification using.
CS 261 – Data Structures Preconditions, Postconditions & Assert.
Types in programming languages1 What are types, and why do we need them?
Semantics In Text: Chapter 3.
Programming with Assertions © Allan C. Milne v
COP4020 Programming Languages Introduction to Axiomatic Semantics Prof. Robert van Engelen.
90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. 1 Lecture 1: Introduction Data.
13 Aug 2013 Program Verification. Proofs about Programs Why make you study logic? Why make you do proofs? Because we want to prove properties of programs.
Exceptions and Assertions Chapter 15 – CSCI 1302.
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.
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.
“Discipline is the refining fire by which talent becomes ability.” – Roy L. Smith Thought for the Day.
CSC3315 (Spring 2009)1 CSC 3315 Languages & Compilers Hamid Harroud School of Science and Engineering, Akhawayn University
1 Exceptions When the Contract is Broken. 2 Definitions A routine call succeeds if it terminates its execution in a state satisfying its contract A routine.
Defensive Programming. Good programming practices that protect you from your own programming mistakes, as well as those of others – Assertions – Parameter.
Throw, Throws & Try-Catch Statements Explanations and Pictures from: Reference:
MT311 Java Application Development and Programming Languages Li Tak Sing( 李德成 )
CSE 374 Programming Concepts & Tools Hal Perkins Fall 2015 Lecture 17 – Specifications, error checking & assert.
Winter 2006CISC121 - Prof. McLeod1 Stuff Midterm exam in JEF234 on March 9th from 7- 9pm.
GC101 Introduction to computer and program
Logger, Assert and Invariants
Topics: jGRASP editor ideosyncrasies assert debugger.
CSE 143 Error Handling [Section 2.8] 3/30/98 CSE 143.
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.
Design by Contract Fall 2016 Version.
Introduction to Data Structures
Programming in Java Assertion.
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.
Semantics In Text: Chapter 3.
Go to pollev.com/cse143.
Java Modeling Language (JML)
Assertions References: internet notes; Bertrand Meyer, Object-Oriented Software Construction; 4/25/2019.
Computer Science 340 Software Design & Testing
COP4020 Programming Languages
Defensive Programming
Presentation transcript:

Pre- and postconditions, Using assertions and exceptions 1 Pre- and postconditions Using assertions and exceptions

Pre- and postconditions, Using assertions and exceptions 2 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

Pre- and postconditions, Using assertions and exceptions 3 Pre- and postconditions Statement that evaluates to true or false Many Java methods have a precondition –Class.forName(String className) Assumes className != null –Integer.parseInt(String s) Assumes s contains a number –If you don’t respect the precondition the methods will most likely throw some kind of (runtime) exception NullPointerException NumberFormatException –A subclass of IllegalArgumentException In Java pre- and postconditions are not part of method signatures –You can specify pre- an postconditions as comments

Pre- and postconditions, Using assertions and exceptions 4 Pre- and postconditions used with method overriding class S { // pre: A; post: B method(int p) { … } } class T extends S { // pre: X; post: Y method(int p) { … } } X can be less strong the A Y can be stronger than B Example –A: p > 0disallows 0 –X: p ≥ 0allows 0 –B result is true or false –Y result is always true

Pre- and postconditions, Using assertions and exceptions 5 Invariant An invariant is a statement that is invariably true. Class invariant –Statement about the objects state between method invocation Loop invariant –Statement about the state of variables in a loop

Pre- and postconditions, Using assertions and exceptions 6 Proof of post condition The idea of introducing pre- and postconditions is to formally (mathematically) prove the postcondition from the preconditions –Postcondition & program implies post condition An invariant may help doing the proof. –However, often the proof can be quite hard to do. Usually only done in critical systems –Controlling hospital equipment, satellites, etc. Usually more errors in the proof than in the program –But gives you a chance to rethink you program.

Pre- and postconditions, Using assertions and exceptions 7 Assertions in Java Assertions is a relatively new feature if Java –Since Java 1.4 –Prior to Java 1.4 programmers had to program their own assertion facility. That is no longer necessary or advisable. New keyword: assert –assert booleanExpression; –Assert booleanExpression : errorMessage; Example –boolean isMale(char sex) { – assert sex == ‘M’ || sex == ‘F’ : “Illegal sex: “ + sex; – return sex == ‘M’ –}

Pre- and postconditions, Using assertions and exceptions 8 Enabling assertions The Java compiler must be explicitly informed that you have assertions in your program –javac –source 1.4 SomeClass.java You tell the compiler that your source code is Java version 1.4 –Reason Assert is a new keyword. Some older program may have used assert to name a variable or a method. The Java virtual machine (JVM) must be explicitly informed to check assertions –java –ea SomeClass -ea means “enable assertions” –Reason Checking assertions take time. You only want to check assertions during testing and debugging, not when the program is running at the customers site.

Pre- and postconditions, Using assertions and exceptions 9 Enabling assertions in NetBeans

Pre- and postconditions, Using assertions and exceptions 10 When not to use assertions Checking parameters to public methods –Don’t use assertions to check parameters on public methods –Assertion checking disabled when the program is running at the customers site. –You cannot otherwise control the validity of parameters to public methods. –Check (using an if-statement) that the parameters are valid. If not throw NullPointerException IllegalArgumentException, or one of its subclasses

Pre- and postconditions, Using assertions and exceptions 11 When to use assertions Checking parameters to private methods –You control who calls private methods. –When things work (you are no longer debugging) then you can safely disable assertion checking. Checking conditions in methods –Where parameters have been checked without using exceptions.

Pre- and postconditions, Using assertions and exceptions 12 References Sun Microsystems Programming with Assertions – /assert.htmlhttp://java.sun.com/j2se/1.4.2/docs/guide/lang /assert.html Qusay H. Mahmoud Using Assertions in Java Technology, Sun Microsystems 2005 – s/JavaLP/assertions/ s/JavaLP/assertions/