Detecting Bugs Using Assertions Ben Scribner. Defining the Problem  Bugs exist  Unexpected errors happen Hardware failures Loss of data Data may exist.

Slides:



Advertisements
Similar presentations
Module 4: Statements and Exceptions. Overview Introduction to Statements Using Selection Statements Using Iteration Statements Using Jump Statements Handling.
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.
Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
COMPUTER PROGRAMMING I Essential Standard 5.02 Understand Breakpoint, Watch Window, and Try And Catch to Find Errors.
Annoucements  Next labs 9 and 10 are paired for everyone. So don’t miss the lab.  There is a review session for the quiz on Monday, November 4, at 8:00.
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.
Java Programming, 3e Concepts and Techniques Chapter 4 Decision Making and Repetition with Reusable Objects.
Program Correctness and Efficiency Chapter 2. Chapter 2: Program Correctness and Efficiency2 Chapter Objectives To understand the differences between.
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.
Conditional Operator (?:) Conditional operator (?:) takes three arguments (ternary) Syntax for using the conditional operator:
Introduction to ML - Part 2 Kenny Zhu. What is next? ML has a rich set of structured values Tuples: (17, true, “stuff”) Records: {name = “george”, age.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 4- 1.
Chapter 4 Making Decisions
Fall 2007CS 225 Program Correctness and Efficiency Chapter 2.
Form Handling, Validation and Functions. Form Handling Forms are a graphical user interfaces (GUIs) that enables the interaction between users and servers.
Unit Testing & Defensive Programming. F-22 Raptor Fighter.
Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting PHP Form Handling.
Testing. What is Testing? Definition: exercising a program under controlled conditions and verifying the results Purpose is to detect program defects.
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.
CPSC 252 Exception Handling Page 1 Exceptions and exception handling Client programmers can make errors using a class attempting to dequeue an item from.
Chapter 12: Exception Handling
UNIT 3 TEMPLATE AND EXCEPTION HANDLING. Introduction  Program errors are also referred to as program bugs.  A C program may have one or more of four.
CSCI 6962: Server-side Design and Programming Validation Tools in Java Server Faces.
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.
CSE 219 Computer Science III Testing. Testing vs. Debugging Testing: Create and use scenarios which reveal incorrect behaviors –Design of test cases:
The Java Programming Language
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
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.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 5 – Dental Payment Application: Introducing.
Chapter 4 Selection Structures: Making Decisions.
Exceptions Handling the unexpected. RHS – SWC 2 The Real World So far, most of our code has been somewhat näive We have assumed that nothing goes wrong…
Exceptions Handling Exceptionally Sticky Problems.
Introduction to Exception Handling and Defensive Programming.
Chapter 14: Exception Handling. Objectives In this chapter, you will: – Learn what an exception is – Learn how to handle exceptions within a program –
Chapter 4 Making Decision Csc 125 C++ programming language Fall 2005.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Selection Statements Selection Switch Conditional.
Exceptions cs1043. Program Exceptions When a program detects an error, what should it do? – Nothing, simply allow the program to fail. – Implement a course.
Programming with Assertions © Allan C. Milne v
C++ Basics. Compilation What does compilation do? g++ hello.cpp g++ -o hello.cpp hello.
Lecture 2 Conditional Statement. chcslonline.org Conditional Statements in PHP Conditional Statements are used for decision making. Different actions.
Java Programming, 2E Introductory Concepts and Techniques Chapter 4 Decision Making and Repetition with Reusable Objects.
Chapter 5 Selection Statements Mr. Dave Clausen La Cañada High School.
Chapter Making Decisions 4. Relational Operators 4.1.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 4 Making Decisions.
Exceptions, handling exceptions & message boxes Year 11 Information Technology.
Exceptions Chapter 16 This chapter explains: What as exception is Why they are useful Java exception facilities.
Exceptions in C++. Exceptions  Exceptions provide a way to handle the errors generated by our programs by transferring control to functions called handlers.
Exceptions and Assertions Chapter 15 – CSCI 1302.
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.
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.
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.
Defensive Programming. Good programming practices that protect you from your own programming mistakes, as well as those of others – Assertions – Parameter.
© 2011 Pearson Education, publishing as Addison-Wesley Chapter 1: Computer Systems Presentation slides for Java Software Solutions for AP* Computer Science.
Exceptions Exceptions are used to signal that an unexpected event has happened in a program C++ will generate exceptions for some errors in the program.
CS170 – Week 1 Lecture 3: Foundation Ismail abumuhfouz.
Logger, Assert and Invariants
Chapter 14: Exception Handling
Conditions and Ifs BIS1523 – Lecture 8.
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.
Chapter 3 Debugging Section 3.4
Computer Science 340 Software Design & Testing
Chapter 5 Selection Statements
CHAPTER 6 Testing and Debugging.
Defensive Programming
Presentation transcript:

Detecting Bugs Using Assertions Ben Scribner

Defining the Problem  Bugs exist  Unexpected errors happen Hardware failures Loss of data Data may exist but may be in the wrong format Complex code sometimes has unforeseen effects Users enter incorrect data

The Familiar Solution  Standard error checking Check if a file exists Make sure variables are defined Ensure that data is in the correct format  Ways of handling errors Exceptions Messages to the user Form Validation Method

Error Checking Limitations  Cannot check everything Already takes up a lot of code, using for every situation is impractical  Situations most people do not use error checking Function output Default case in switch statement Else in an if statement Pre / Post Conditions

To Sum Up the Problem  Programmers assume many things to be true  Often these assumptions are proven incorrect  Assumption errors can be difficult to catch  These errors can cause other modules to fail making the problem difficult to trace

Is There Another Method?  Assertions A Boolean expression that is true if an assumption is correct Could be a comment Often used as actual code to test conditions Strictly used for testing

What Does It Look Like? <cf_assert assertion = “ myAge as a: IsNumeric( |a| ); |a| GT 0; |a| LT 100 myName as b: IsDefined( ‘|b|’ ); Len( |b| ) GT 4”>

How Do I Put One Together? Assertion := Statement := any_variable as any_char: Boolean | Statement Statement ex: myVar as v Boolean := (statement returning true or false) | Boolean; Boolean Note: the character representing the variable is always referred to using pipes ex. |a|

How Is It Implemented?  cf_assert sends whole expression to assert.cfm assert.cfm is in the custom tags folder  It parses the expression and evaluates it If expression = true, assertion does nothing If expression = false, assertion throws an exception  Exceptions can be caught manually, or Coldfusion will catch them and display the errors

What Does an Error Return?  Without exceptions:  When I test the code: Error Occurred While Processing Request The assertion IsNumeric( myVar ) failed

What Does an Error Return? (cont.)  With exceptions your variable is not a number </cfcatch  When I test the code: your variable is not a number

Database Example Data: PIDdisplayOrder Select * From myTable Order By displayOrder

Database Example (cont.)

Switch Example myVar can be either 1 or 2, nothing else

Switch Example (cont.)  Sometimes it is assumed that there is no need for a default case. This is a good place for an assertion

Form Example Page1.cfm: Name: Date of Birth:

Form Example (cont.) Page2.cfm <cfmodule template=“/wwwAdmin/CF_Tags/Validate_Forms_XML.cfm” FDF_URL=“/benTest/page1.xml”> <cf_assert assertion = “FORM.name as n: IsDefined( ‘|n|’ ); Len( |n| ) GTE 3 FORM.dob as d: IsDefined( ‘|d|’ ); IsNumericDate( |d| )”> Hello #FORM.name#, you are #DateDiff(‘YYYY’, FORM.dob, Now())#.

Assertion Benefits  Can be used to: Catch logic errors Check results of an operation Detect errors soon after they occur Make statements about the code that are guaranteed to be true Serve much the same purpose as comments

How They Should Be Used  Solely for testing They are designed to help the programmer find bugs, not to aide the flow of execution  Should be turned off in production code assert.cfm can be an empty file on production, so there is no need to comment assertions out  Should be used in any situation where the programmer assumes something to be true

The Other Side  Assertion Limitations Errors in assertion logic can lead to misleading problems They can impact performance Some cases are difficult to test for, some assertions may never execute They take time to write

What Does All This Mean to Me?  The use of assertions can lead to well-tested, correct code  They self-document the code, allowing others to understand modules more efficiently  Programmers will better understand pre/post conditions Some bugs may be caught before the code is even tested

In Conclusion  Assertions are a great way to aide testing  They are by no means a “Silver Bullet” Using them will not eliminate all bugs  Questions?