1 of 14

Debugging

2 of 14

What is Debugging?

  • In olden times, it was literally removing the bugs from computer
    • Insects shorted out the electronics
  • Today it means to find the errors in your code and remove them

3 of 14

Types of Errors

  • There are three main types of errors that can occur when programming a solution to a problem

  1. Syntax Errors
  2. Run-time Errors
  3. Logic Errors

4 of 14

Syntax Errors

  • Syntax means the rules of the language
    • i.e. The English language says there should be punctuation at the end of a sentence
  • A syntax error is therefore a line or command that’s written incorrectly
  • These are the easiest errors to fix, because we usually have them pointed out to us by the IDE

5 of 14

Run-time Errors

  • Run-time errors occur when the program is running
  • They don’t show up while we’re writing the code
  • May be caused by the user of our program
  • These are harder to fix than logic errors, but usually the error we see gives us a clue about how is was caused (and how we might fix it)
  • Types include:
    • Undefined variables
    • Division by 0
    • Data type mismatch

6 of 14

Run-time Error Examples

7 of 14

Logic Errors

  • So your program runs without crashing (no syntax or run-time errors), but still does the wrong thing?
    • You have a logic error!
  • Logic errors are the most difficult types of errors to find and fix
  • You need to have a very good understanding of how the code functions

8 of 14

Logic Error Example

9 of 14

Debugging Strategy 1 - Old School Paper and Pencil

  • Get a piece of paper and a pencil
  • Make a chart showing all of your variables
  • Go through your code line by line
  • As each variable is given a value, update it in your chart
  • This allows you to see how your variable values are changing over time, which will hopefully help you spot the error
  • Example

10 of 14

Debugging Strategy 2 - Print Statements

  • Add print statements to strategic parts of your code
    • Inside loops to see how variables change
    • Inside if statements to see if they’re executing
  • These print statements would then get removed when the error is located

11 of 14

Debugging Strategy 3 - Rubber Duck Debugging

  • Step 1: Go get a rubber duck
  • Step 2: Explain your code line by line to the duck
  • Step 3: Realize your error as you’re explaining what your code should be doing
  • This is a real strategy!
    • It doesn’t have to be a rubber duck though
    • Other options:
      • Your programming teacher
      • A friend
      • A rubber python

12 of 14

Debugging Strategy 4 - Use an IDE with a Debugger

  • Most IDE’s have some type of debugging tool built in
  • With Wing, we can execute the code one line at a time, and watch variable values

  • This can be a little tricky to get the hang of at first, but does provide a really powerful way to find errors

13 of 14

Debugging - Final Thoughts

  • Debugging can be very frustrating
    • Try not to smash anything
    • Take a break!
  • Debugging code is a skill
  • The more you practice you get the better you’ll be!

14 of 14

More Info