CSE 374 Programming Concepts & Tools

Slides:



Advertisements
Similar presentations
Chapter 5 Errors Bjarne Stroustrup
Advertisements

Exceptions CSE301 University of Sunderland Harry Erwin, PhD.
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.
CMSC 202 Exceptions 2 nd Lecture. Aug 7, Methods may fail for multiple reasons public class BankAccount { private int balance = 0, minDeposit =
 Both System.out and System.err are streams—a sequence of bytes.  System.out (the standard output stream) displays output  System.err (the standard.
OOP #10: Correctness Fritz Henglein. Wrap-up: Types A type is a collection of objects with common behavior (operations and properties). (Abstract) types.
Options for User Input Options for getting information from the user –Write event-driven code Con: requires a significant amount of new code to set-up.
Computer Science 340 Software Design & Testing Design By Contract.
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.
Testing Michael Ernst CSE 140 University of Washington.
Testing CSE 140 University of Washington 1. Testing Programming to analyze data is powerful It’s useless if the results are not correct Correctness is.
CS 261 – Data Structures Preconditions, Postconditions & Assert.
Exceptions and assertions CSE 331 University of Washington.
Reasoning about programs March CSE 403, Winter 2011, Brun.
CSE 374 Programming Concepts & Tools Hal Perkins Fall 2015 Lecture 10 – C: the heap and manual memory management.
SWE 4743 Abstract Data Types Richard Gesick. SWE Abstract Data Types Object-oriented design is based on the theory of abstract data types Domain.
Programming & Debugging. Key Programming Issues Modularity Modifiability Ease of Use Fail-safe programming Style Debugging.
(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.
Effective Java, Chapter 9: Exceptions Items Last modified Fall 2012 Paul Ammann.
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.
Chapter 3 Lists, Stacks, Queues. Abstract Data Types A set of items – Just items, not data types, nothing related to programming code A set of operations.
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.
CSE 374 Programming Concepts & Tools
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,
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.
Chapter 6 CS 3370 – C++ Functions.
CSE 374 Programming Concepts & Tools
CSE 374 Programming Concepts & Tools
Logger, Assert and Invariants
CSE 374 Programming Concepts & Tools
Design David Evans cs205: engineering software
Topics: jGRASP editor ideosyncrasies assert debugger.
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 14 - Wednesday CS 121.
CSE 331 Software Design & Implementation
Testing UW CSE 160 Spring 2018.
Design by Contract Fall 2016 Version.
Preconditions, Postconditions & Assertions
Testing UW CSE 160 Winter 2016.
Chapter 12 Exception Handling
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.
[Chapter 4; Chapter 6, pp ] CSC 143 Linked Lists [Chapter 4; Chapter 6, pp ]
CSE341: Programming Languages Lecture 11 Type Inference
Hoare-style program verification
CMSC 202 Exceptions 2nd Lecture.
Effective Java, 3rd Edition Chapter 10: Exceptions
CMSC 202 Exceptions 2nd Lecture.
CSE 303 Concepts and Tools for Software Development
CSE 143 Java Exceptions 1/18/2019.
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.
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
CISC101 Reminders Assignment 3 due today.
Summary.
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.
Software Specifications
CSE 303 Concepts and Tools for Software Development
Presentation transcript:

CSE 374 Programming Concepts & Tools Hal Perkins Winter 2017 Lecture 17 – Specifications, error checking & assert UW CSE 374 Winter 2017

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”? UW CSE 374 Winter 2017

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” UW CSE 374 Winter 2017

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. UW CSE 374 Winter 2017

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 UW CSE 374 Winter 2017

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? UW CSE 374 Winter 2017

assert in C #include <assert.h> 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 UW CSE 374 Winter 2017

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 UW CSE 374 Winter 2017

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 UW CSE 374 Winter 2017

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 UW CSE 374 Winter 2017