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:
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
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
"name": "What Does an Error Return.",
"description": "(cont.) With exceptions your variable is not a number
Database Example Data: PIDdisplayOrder 1 5 3 2 2 7 9 6 Select * From myTable Order By displayOrder
Form Example (cont.) Page2.cfm 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?
Your consent to our cookies if you continue to use this website.