Presentation is loading. Please wait.

Presentation is loading. Please wait.

Why care about debugging? How many of you have written a program that worked perfectly the first time? No one (including me!) writes a program that works.

Similar presentations


Presentation on theme: "Why care about debugging? How many of you have written a program that worked perfectly the first time? No one (including me!) writes a program that works."— Presentation transcript:

1

2 Why care about debugging? How many of you have written a program that worked perfectly the first time? No one (including me!) writes a program that works correctly the first time The most important skill you can develop for writing code is not how all the different structures work, but how to figure out how to fix your errors

3 Debugging versus Testing Testing: determining whether there are errors in your program Need to run test cases to see if your program behaves correctly Relatively easy Debugging: once an error has been identified, finding and fixing the error Need to determine what portion of the code is resulting in the error, then figure out how to correct the issue More difficult than testing

4 Why Debugging is Hard There may be no obvious relationship between the external manifestation(s) of an error and its internal cause(s) Symptom and cause may be in remote parts of the program Changes (new features, bug fixes) in program may mask (or modify) bugs Symptom may be due to human mistake or misunderstanding that is difficult to trace Bug may be triggered by rare or difficult to reproduce input sequence, program timing (threads) or other external causes Bug may depend on other software/system state, things others did to your systems weeks/months ago

5 Test-Debug Cycle Continue the process until testing discovers no more errors Testing Debugging Discovers Errors Fixes Errors

6 Debugging Techniques Understanding errors Commenting out sections Stepping through a program Running and checking individual sections

7 Understanding Errors Types of errors: Syntax errors: problems with your MATLAB syntax Forgetting parenthesis Forgetting mathematical operators Wrong number/type of input arguments for a function Logic (runtime) errors: your program will run but does not function the way you expect it to Incorrect mathematical expressions Incorrect conditions Infinite loops Accessing array indices that do not exist

8 Understanding Errors One of the first things you need to do before you can run your program is eliminate any syntax errors These are the errors you get when you try to run the program Luckily, unlike many runtime errors, MATLAB will give you some help in eliminating syntax errors

9 Understanding Errors Errors are displayed in two ways: At the command prompt when you run the code Clicking on the underlined portion will take you to the location where the error is occurring Where the error occurred Reason for the error

10 Understanding Errors Errors are listed in two places: On the right-hand side of the code editor Red indicates a syntax error Dash shows location of error; hover mouse for explanation

11 Understanding Syntax Errors Common errors: Unexpected MATLAB expression – you likely forgot to include a mathematical operator, accidently started a variable name with a number (or other non-allowed symbol), or forgot to use parentheses with a function Expression or statement is incorrect – possibly unbalanced (, {, or [ – you have a mismatched parenthesis or bracket At least one END is missing – you have forgotten to place and END with a structure (if, switch, loop, function) Illegal use of reserved keyword “xxx” – you have either forgotten a part of a MALTAB structure (i.e. else without an if first) or you are trying to use a keyword as a variable name

12 Understanding Logic Errors Common errors: Attempted to access variable(index); index out of bounds because… – you are trying to access a value in a vector or array at an index which does not exist Undefined function ‘xxxx' for input arguments of type ‘xxxx'.– you are trying to provide an input argument to a function of incorrect type (i.e. passing a string to sqrt) Undefined function or variable ‘xxxx’ – you are trying to use a variable before it has been given a value, or you have a spelling error in your variable name somewhere in your code I understand you have a logic problem. A logic problem, indeed. A logic problem is confirmed. Oh be quiet, all of you!

13 Debugging Techniques Commenting out lines or sections of code: Place the comment % indicator in front of a line of code to keep it from executing Highlight a section of code and select “Text -> Comment” or press Ctrl+R Allows you to: test to see whether certain lines of code are problematic test different versions of code within the same program without deleting items or lines

14 Debugging Techniques Stepping through a program: Allows you to execute your program one line at a time Follow the path of execution Monitor the value of variables Most powerful technique available for debugging Built in features in MATLAB to help you step through your program

15 Debugging Features in MATLAB Breakpoint: Sets a point in the code that will pause the execution of the program and enter Debugging Mode Create a breakpoint by left-clicking on the dash next to the line number on the left side of the code window

16 Stepping in MATLAB Once you have stopped at a breakpoint, you can then step through the script using the buttons shown below: Step Into – goes inside a function and steps through the code Step – executes the current line and moves on to the next line Step Out – when inside a function, completes the execution and returns to the next higher level Exit debug mode – aborts execution of the code Continue – runs the program until another breakpoint is encountered Run to Cursor – runs the program to the line where the cursor currently sits

17 Stepping in MATLAB As you’re debugging a program, the green arrow will show what line of code is about to be executed

18 Watching the Values of Variables One of the benefits of entering debug mode is that you can watch the values of variables as your program executes There are two ways to view the values of variables: While debugging, holding the mouse over a variable will display the value of the variable

19 Watching the Values of Variables One of the benefits of entering debug mode is that you can watch the values of variables as your program executes There are two ways to view the values of variables: While debugging, holding the mouse over a variable will display the value of the variable Look at the value of the variables in the workspace All variables for the current code will be shown Scripts  variables will remain after execution Functions  variables will remain while you are executing the current function

20 Debugging Techniques Checking sections of code: Pull out a section of code or function and place it in a new program Make sure that section of code operates correctly Insert it back into the whole program Especially helpful for very long and complicated code Pull out sections one at a time to make sure each section works correctly Once each section works correctly, run the whole program to make sure each section works collectively

21 Test Your Understanding


Download ppt "Why care about debugging? How many of you have written a program that worked perfectly the first time? No one (including me!) writes a program that works."

Similar presentations


Ads by Google