Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Detecting Bugs Using Assertions Ben Scribner. Defining the Problem  Bugs exist  Unexpected errors happen Hardware failures Loss of data Data may exist."— Presentation transcript:

1 Detecting Bugs Using Assertions Ben Scribner

2 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

3 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

4 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

5 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

6 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

7 What Does It Look Like?

8 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|

9 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

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

11 What Does an Error Return? (cont.)  With exceptions your variable is not a number

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

13 Database Example (cont.)

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

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

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

17 Form Example (cont.) Page2.cfm Hello #FORM.name#, you are #DateDiff(‘YYYY’, FORM.dob, Now())#.

18 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

19 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

20 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

21 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

22 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?


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

Similar presentations


Ads by Google