CSE 374 Programming Concepts & Tools Hal Perkins Fall 2015 Lecture 17 – Specifications, error checking & assert.

Slides:



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

Rigorous Software Development CSCI-GA Instructor: Thomas Wies Spring 2012 Lecture 8.
Semantics Static semantics Dynamic semantics attribute grammars
Reasoning About Code; Hoare Logic, continued
CSE 331 Software Design & Implementation Dan Grossman Winter 2014 Lecture 2 – Reasoning About Code With Logic 1CSE 331 Winter 2014.
OOP #10: Correctness Fritz Henglein. Wrap-up: Types A type is a collection of objects with common behavior (operations and properties). (Abstract) types.
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.
Procedure specifications CSE 331. Outline Satisfying a specification; substitutability Stronger and weaker specifications - Comparing by hand - Comparing.
Software Engineering Prof. Dr. Bertrand Meyer March 2007 – June 2007 Chair of Software Engineering Static program checking and verification Slides: Based.
1 Debugging and Testing Overview Defensive Programming The goal is to prevent failures Debugging The goal is to find cause of failures and fix it Testing.
SWE 619 © Paul Ammann Procedural Abstraction and Design by Contract Paul Ammann Information & Software Engineering SWE 619 Software Construction cs.gmu.edu/~pammann/
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.
Exceptions and assertions CSE 331 University of Washington.
Cs2220: Engineering Software Class 6: Defensive Programming Fall 2010 University of Virginia David Evans.
Reasoning about programs March CSE 403, Winter 2011, Brun.
Pre- and postconditions, Using assertions and exceptions 1 Pre- and postconditions Using assertions and exceptions.
(c) University of Washington13-1 CSC 143 Java Specifications: Programming by Contract Reading: Ch. 5.
ANU COMP2110 Software Design in 2003 Lecture 10Slide 1 COMP2110 Software Design in 2004 Lecture 12 Documenting Detailed Design How to write down detailed.
CSE 374 Programming Concepts & Tools Hal Perkins Fall 2015 Lecture 10 – C: the heap and manual memory management.
Testing CSE 160 University of Washington 1. Testing Programming to analyze data is powerful It’s useless (or worse!) if the results are not correct Correctness.
SWE 4743 Abstract Data Types Richard Gesick. SWE Abstract Data Types Object-oriented design is based on the theory of abstract data types Domain.
Understanding ADTs CSE 331 University of Washington.
(c) University of Washington10-1 CSC 143 Java Errors and Exceptions Reading: Ch. 15.
David Evans CS201j: Engineering Software University of Virginia Computer Science Lecture 10: Programming Exceptionally.
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.
Winter 2006CISC121 - Prof. McLeod1 Stuff Midterm exam in JEF234 on March 9th from 7- 9pm.
Insertion sort Loop invariants Dynamic memory
Class Invariants Class invariants are logical conditions to ensure the correct working of a class. Class invariants must hold true when an object is created,
Chapter 6 CS 3370 – C++ Functions.
CSE 374 Programming Concepts & Tools
CSE 374 Programming Concepts & Tools
Logger, Assert and Invariants
CSE 331 Software Design and Implementation
Topics: jGRASP editor ideosyncrasies assert debugger.
CSE 374 Programming Concepts & Tools
Testing and Debugging.
Testing UW CSE 160 Winter 2017.
Reasoning About Code.
Reasoning about code CSE 331 University of Washington.
CSE 143 Error Handling [Section 2.8] 3/30/98 CSE 143.
Week 7 Part 2 Kyle Dewey.
CSE 331 Software Design & Implementation
Testing UW CSE 160 Spring 2018.
Design by Contract Fall 2016 Version.
Preconditions, Postconditions & Assertions
CSE 331 Software Design and Implementation
Testing UW CSE 160 Winter 2016.
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.
CISC101 Reminders Assn 3 due tomorrow, 7pm.
Hoare-style program verification
CMSC 202 Exceptions 2nd Lecture.
CMSC 202 Exceptions 2nd Lecture.
CSE 331 Software Design & Implementation
CSE 303 Concepts and Tools for Software Development
ECE 103 Engineering Programming Chapter 56 Runtime Errors
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.
Go to pollev.com/cse143.
CMSC 202 Exceptions 2nd Lecture.
CSC 143 Java Errors and Exceptions.
Assertions References: internet notes; Bertrand Meyer, Object-Oriented Software Construction; 4/25/2019.
Effective Java, Chapter 9: Exceptions
Computer Science 340 Software Design & Testing
CMSC 202 Exceptions 2nd Lecture.
Functions Students should understand the concept and basic mechanics of the function call/return pattern from CS 1114/2114, but some will not. A function.
Programming Languages 2nd edition Tucker and Noonan
Software Specifications
Design Contracts and Errors A Software Development Strategy
Presentation transcript:

CSE 374 Programming Concepts & Tools Hal Perkins Fall 2015 Lecture 17 – Specifications, error checking & assert

Where we are Talked about testing, but not what (partially) correct was What does it mean to say a program is “correct”? How do we talk about what a program should “do”? What do we do when it “doesn’t”? 2

Specifying code? We made a big assumption, that we know what the code is supposed to do! Often, a complete specification is at least as difficult as writing the code. But: –It’s still worth thinking about –Partial specifications are better than none –Checking specifications (at compile-time and/or run-time) is great for finding bugs early and “assigning blame” 3

Full specification Often tractable for very simple stuff: “Given integers x,y>0, return their greatest common divisor.” What about sorting a doubly-linked list? –Precondition: Can input be NULL ? Can any prev and next fields be NULL ? Can the list be circular or not? –Postcondition: Sorted (how to specify?) And there’s often more than “pre” and “post” – time/space overhead, other effects (such as printing), things that may happen in parallel Specs should guide programming and testing! Should be declarative (“what” not “how”) to decouple implementation and use. 4

Pre/post conditions and invariants Pre- and post-conditions apply to any statement, not just functions –What is assumed before and guaranteed after Because a loop “calls itself” its body’s post-condition better imply the loop’s precondition –A loop invariant Example: find max (next slide) Meaning of “correct”: a segment of code is correct if, when it begins execution in a state where its precondition is true, it is guaranteed to terminate in a state in which the postcondition is true 5

Pre/post conditions and invariants // pre: arr has length len; len >= 1 int max = arr[0]; int i=1; while(i<len) { if(arr[i] > max) max = arr[i]; ++i; } // post: max >= all arr elements loop-invariant: For all j =arr[j]. to show it holds after the loop body, must assume it holds before loop body loop-invariant plus !(i<len) after body, enough to show post 6

Partial specifications The difficulty of full specifications need not mean abandoning all hope Useful partial specs: –Can args be NULL ? –Can args alias? –Are pointers to stack data allowed? Dangling pointers? –Are cycles in data structures allowed? –What is the minimum/maximum length of an array? –... Guides callers, callees, and testers 7

Beyond testing Specs are useful for more than “things to think about while coding” and testing and comments Sometimes you can check them dynamically, e.g., with assertions (all examples true for C and Java) –Easy: argument not NULL –Harder but doable: list not cyclic –Impossible: Does the caller have other pointers to this object? 8

assert in C #include void f(int *x, int*y) { assert(x!=NULL); assert(x!=y);... } assert is a macro; ignore argument if NDEBUG defined at time of #include, else evaluate and if zero (false!) exit program with file/line number in error message Watch Out! Be sure that none of the code in an assert has side effects that alter the program’s behavior. Otherwise you get different results when assertions are enabled vs. when they are not 9

assert style Many guidelines are overly simply and say “always” check everything you can., but: –Often not on “private” functions (caller already checked) –Unnecessary if checked statically “Disabled” in released code because: –executing them takes time –failures are not fixable by users anyway –assertions themselves could have bugs/vulnerabilities Others say: –Should leave enabled; corrupting data on real runs is worse than when debugging 10

assert vs exceptions; error checking Suppose a condition should be true at a given point in the program, but it’s not. What do we do? Common practice: –If the condition involves preconditions of a public interface (x  0, list not full, p not NULL,…), treat a failure as an error and throw an exception or terminate with an error code Don’t trust client code you don’t control! –If the condition is an internal matter, a failure represents a programming error (bug). Check this with assert 11

Static checking A stronger type system or other code-analysis tool might take a program and examine it for various kinds of errors –Plusses: earlier detection (“coverage” without running program), faster code –Minus: Potential “false positives” (spec couldn’t ever actually be violated, but tool can’t prove that) Deep CS theory fact: Every code-analysis tool proving a non-trivial fact has either false positives (unwarranted warning) or false negatives (missed bug) or both Deep real-world fact: That doesn’t make them unuseful 12