PRAGMATIC PARANOIA Steven Hadfield & Anthony Rice.

Slides:



Advertisements
Similar presentations
Chapter 17 Failures and exceptions. This chapter discusses n Failure. n The meaning of system failure. n Causes of failure. n Handling failure. n Exception.
Advertisements

Copyright W. Howden1 Programming by Contract CSE 111 6/4/2014.
Exceptions CSE301 University of Sunderland Harry Erwin, PhD.
Lectures on File Management
Topics Introduction Types of Errors Exceptions Exception Handling
Control Structures Ranga Rodrigo. Control Structures in Brief C++ or JavaEiffel if-elseif-elseif-else-end caseinspect for, while, do-whilefrom-until-loop-end.
Exceptions: when things go wrong. Various sources of error public static doSomething() { int i = 3.0; while(!done); { int i = false } ) Syntactic errors.
Exception Handling Chapter 15 2 What You Will Learn Use try, throw, catch to watch for indicate exceptions handle How to process exceptions and failures.
Software Construction 1 ( ) First Semester Dr. Samer Odeh Hanna (PhD) Office: IT 327.
Chair of Software Engineering Software Architecture Prof. Dr. Bertrand Meyer Lecture 6: Exception Handling.
1 Design by Contract Building Reliable Software. 2 Software Correctness Correctness is a relative notion  A program is correct with respect to its specification.
Software Engineering and Design Principles Chapter 1.
11-Jun-15 Exceptions. 2 Errors and Exceptions An error is a bug in your program dividing by zero going outside the bounds of an array trying to use a.
-5- Exception handling What is an exception? “An abnormal event” Not a very precise definition Informally: something that you don’t want to happen.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 17 Exceptions and.
16-Jun-15 Exceptions. Errors and Exceptions An error is a bug in your program dividing by zero going outside the bounds of an array trying to use a null.
Exceptions in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Exceptions. Errors and Exceptions An error is a bug in your program –dividing by zero –going outside the bounds of an array –trying to use a null reference.
OOP #10: Correctness Fritz Henglein. Wrap-up: Types A type is a collection of objects with common behavior (operations and properties). (Abstract) types.
Information Hiding and Encapsulation
Eiffel Language and Design by Contract Contract –An agreement between the client and the supplier Characteristics –Expects some benefits and is prepared.
1 © Wolfgang Pelz Design by Contract Design by Contract™ Based on material drawn from: Bertrand.
Exceptions. Many problems in code are handled when the code is compiled, but not all Some are impossible to catch before the program is run  Must run.
Computer Science 340 Software Design & Testing Design By Contract.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 18 Exception Handling.
COMPUTER PROGRAMMING 2 Exceptions. What are Exceptions? Unexpected events that happen when the code is executing (during runtime). Exceptions are types.
University of Palestine software engineering department Testing of Software Systems Fundamentals of testing instructor: Tasneem Darwish.
CMSC 202 Exceptions. Aug 7, Error Handling In the ideal world, all errors would occur when your code is compiled. That won’t happen. Errors which.
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?
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.
SWE 619 © Paul Ammann Procedural Abstraction and Design by Contract Paul Ammann Information & Software Engineering SWE 619 Software Construction cs.gmu.edu/~pammann/
Chapter 25 Formal Methods Formal methods Specify program using math Develop program using math Prove program matches specification using.
How to Design Error Steady Code Ivaylo Bratoev Telerik Corporation
1 Software Construction and Evolution - CSSE 375 Exception Handling - Principles Steve Chenoweth, RHIT Above – Exception handling on the ENIAC. From
Testing. 2 Overview Testing and debugging are important activities in software development. Techniques and tools are introduced. Material borrowed here.
CS 261 – Data Structures Preconditions, Postconditions & Assert.
Exceptions and assertions CSE 331 University of Washington.
Reasoning about programs March CSE 403, Winter 2011, Brun.
Exception Handling Programmers must deal with errors and exceptional situations: User input errors Device errors Empty disk space, no memory Component.
Design by Contract Ranga Rodrigo based on Mark Priestley's Lectures.
David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington Debugging COMP T1.
1 Devon M. Simmonds, Computer Science Department Design by Contract Devon M. Simmonds Computer Science Department University of North Carolina, Wilmington.
CSCI1600: Embedded and Real Time Software Lecture 28: Verification I Steven Reiss, Fall 2015.
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.
1 Exceptions. 2 Syntax Errors, Runtime Errors, and Logic Errors syntax errors, runtime errors, and logic errors You learned that there are three categories.
Exceptions Lecture 11 COMP 401, Fall /25/2014.
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.
AVCE ICT – Unit 7 - Programming Session 12 - Debugging.
While loops. Iteration We’ve seen many places where repetition is necessary in a problem. We’ve been using the for loop for that purpose For loops are.
SENG521 (Fall SENG 521 Software Reliability & Testing Fault Tolerant Software Systems: Techniques (Part 4a) Department of Electrical.
Design by Contract. The Goal Ensure the correctness of our software (correctness) Recover when it is not correct anyway (robustness) Correctness: Assertions.
Introduction to Exceptions in Java CS201, SW Development Methods.
Winter 2006CISC121 - Prof. McLeod1 Last Time Reviewed class structure: –attributes –methods –(inner classes) Looked at the effects of the modifiers: –public.
CSE 374 Programming Concepts & Tools Hal Perkins Fall 2015 Lecture 17 – Specifications, error checking & assert.
Chapter 6 CS 3370 – C++ Functions.
Design by Contract Jim Fawcett CSE784 – Software Studio
Design by Contract Jim Fawcett CSE784 – Software Studio
Logger, Assert and Invariants
Software Construction
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.
Exceptions 25-Apr-19.
SWE 619 Last modified Fall 2007 Saket Kaushik, Paul Ammann
Exceptions 10-May-19.
Computer Science 340 Software Design & Testing
CMSC 202 Exceptions.
Exception Handling.
Software Construction
Design by Contract – Exceptions
Presentation transcript:

PRAGMATIC PARANOIA Steven Hadfield & Anthony Rice

You can’t write perfect software  Accept it  Nobody else writes it either  Be defensive about other people’s code  Play it safe

Design by Contract  Developed by Bertrand Meyer (Eiffel)  Use contracts to force both parties into an agreement: caller and routine  Each party is expected to uphold their end of the deal  Preconditions, postconditions, and class invariants

Preconditions  What the caller guarantees will be passed to the routine  The routine’s requirements  It’s up to the calling code to make sure that the requirements are kept

Postconditions  What the routine guarantees to return  How the world should be after the routine is done  Lazy code: require a lot, promise little in return

Class invariants  Conditions that should always be true from caller’s perspective  Routine possibly allowed to change it temporarily while working, but should return to previous state  Things that the routine is not allowed to change  Applies to all methods in a class

Designing with Contracts  It’s a design technique  Directly supported by some languages  Handy if compiler can do it, but not necessary  Assertions – partially emulate DBC  Use preprocessors for languages without  Messy and not as good, but helpful  Can be dynamically generated  Rejected and/or negotiated

Invariants  Also applies at lower levels  Loop invariant  Making sure something is true before and during a loop  Semantic invariants  Central to the purpose of a task  Should be clear and unambiguous

Dead Programs Tell No Lies  If a program is going to crash, do it early  Crash with class  Provide useful information  If the impossible happens, die as soon as possible  Everything after the impossible happens is suspect

Assertive Programming  This can never happen…  “This code will not be used 30 years from now, so two- digit dates are fine”  “This application will never be used abroad, so why internationalize it?”  “count can’t be negative”  “This printf can’t fail”

Assertions  If it can’t happen, use assertions to ensure that it won’t  Don’t use assertions as error handling, they should just be used to check for things that should never happen.  Void writeString(char *string){ assert(string != NULL);  Never put code that should be executed into as assert.

Leave Assertions Turned On  Misunderstanding:  Since they check for things that should never happen, the are only triggered by a bug in the code. They should be turned off when shipped to make the code run faster.

Leave Assertions Turned On  You cannot assume that testing will find all the bugs.  Your program runs in a dangerous world.  Your first line of defense is checking for any possible error, and your second is using assertions to try to detect those you missed.

Assertions and Side Effects  Instead of:  While (iter.hasMoreElements()) { Test.ASSERT(iter.nextElement() != null); Object obj = iter.nextElement();  Do:  While (iter.hasMoreElements()) { Object obj = iter.nextElement(); Test. ASSERT(obj != null);

When to Use Exceptions  Checking for every possible error can lead to some pretty ugly code.  If the programming language supports exceptions, you can use try catch loops to make the code much easier to read.

What is Exceptional?  Exceptions should be reserved for unexpected events.  They should rarely be used as the programs normal flow.

Use Exceptions for Exceptional Programs  An exception represents an immediate, nonlocal transfer of control.  Using exceptions as part of normal processing will give you all the readability and maintainability problems of spaghetti code.

Error Handlers Are an Alternative  Error Handlers are routines that are called when an error is detected.  These routines can handle a specific category of errors.

How to Balance Resources  Finish What You Start  Many developers have not consistent plan for dealing with resource allocation and deallocation.  The routine or object that allocates a resource should be responsible for deallocating it.

Nest Allocations  The basic pattern for resource allocation can be extended for routines that need more than one resource at a time.  Deallocate resources in the opposite order in which you allocate them.  When allocating the same set of resources in different places in your code, always allocate them in the same order.

Objects and Exceptions  Encapsulation of resources in classes  Instantiate that class when you need a particular resource type.

When You Can’t Balance Resources  Commonly found in programs that use dynamic data structures.  When you deallocate the top-level data structure:  Top-level structure responsible for substructures.  Top-level structure is simply deallocated.  Top-level structure refuses to deallocate itself if it contains substructures.  Choice depends on individual data structure.

Checking the Balance  Produce wrappers for each resource, and keep track of all allocations and deallocations.  When program logic says resources will be in a certain state you can use the wrappers to check it.