1 / 11

Debugging

Debugging. Introduction to Computing Science and Programming I. Debugging. Debugging is the process of finding and correcting bugs (errors) in your program. We talked about this for a short while earlier in the course, but it is worth spending some time on.

jlavergne
Download Presentation

Debugging

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Debugging Introduction to Computing Science and Programming I

  2. Debugging • Debugging is the process of finding and correcting bugs (errors) in your program. • We talked about this for a short while earlier in the course, but it is worth spending some time on. • Just like programming in general debugging needs to be learned through experience.

  3. Debugging • “Prevention is the best medicine” applies to this situation too. It may be trite, but it is true. • Prevention • Prevent errors by programming correctly • Also, program in such a way that it is easier to locate and correct errors if they do occur.

  4. Debugging • Prevention • Plan ahead. Think about the structure of your program before you start coding. • Program and test small parts of the entire program. In a lot of cases you can just run your code after writing a few new lines whether or not it seems like a logical point to do so. • Follow good coding style. If you consistently use good style you are less likely to make mistakes and they will be easier to track down later.

  5. Debugging • Finding Errors • Syntax Errors • Python won’t let you run your code if you have syntax errors and tries to tell you where they are. However, in some cases Python may not point you to the right location, missing or extra parentheses, brackets, or quotes can cause this. • Runtime Errors • The program crashes and Python gives you a message describing the error including on what line it occurred. • However the cause of the error may be earlier in the code. • Semantic Errors • Harder to figure out as there isn’t a blatant error, the program just isn’t working correctly

  6. Debugging • Testing • Testing your program for errors is an important part of debugging. Otherwise, runtime and semantic errors can often go unnoticed. • Your program may function correctly with a general case, proper input, however all cases should be checked. • Special/Boundary cases • Improper input

  7. Debugging • Once you have an error things that you should try to follow. • Be methodical about it. If you start banging away at the problem without thinking, you are likely to cause more trouble for yourself. • Think backwards from the output. If the program gives bad output it is worth examining it to figure out what went wrong. • Take a break/get some sleep. After a certain point you just aren’t productive anymore.

  8. Debugging • Use print statements to see what is going on. • printing out messages can help you understand where in a program an error is taking place. • Adding print statements at the start/end of functions and before/after loops is a good place to start. • print statements are also good for seeing the values that variables are taking on at different points • Be descriptive when printing things out “Entering search function”, “Beginning for loop” and not “Hi”, “asdf” or some other useless message.

  9. Debugging • Break the program apart to locate the error. • Another strategy if you can’t locate the error is to simplify the code that is running. Go back to a point where you know things are still working and comment out the code afterwards. Slowly add the code back in until the error reappears. • Simplifying the problem may also help, test the program with small lists, simple cases first. • Simplify complex lines by breaking them apart into multiple lines that accomplish the same thing.

  10. Debugging • A note on the try-except structure • Put as little code in the try block as possible. Normally you use the try block to catch a specific error, but if an error you don’t expect occurs, Python handles it without printing out an error message.

  11. Debugging • After fixing the bug. • After locating and correcting a bug it is worthwhile to think about what happened and why it happened. A lot of bugs occur because of simple typos, but a lot are caused by the programmer’s strategy.

More Related