Effective Debugging Techniques in Programming

cs 115 lecture n.w
1 / 10
Embed
Share

Learn how to effectively debug your code using techniques such as reading and tracing, using a debugger, setting breakpoints, and single-stepping. Understand the importance of finding the root cause of bugs to improve code quality and efficiency.

  • Debugging
  • Programming
  • Techniques
  • Code Quality
  • Efficiency

Uploaded on | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. CS 115 Lecture Debugging Taken from notes by Dr. Neil Moore

  2. Debugging We ve seen how to write test cases to help you find bugs in your program. What should you do when you find something is wrong? Find the line that s wrong and fix it Which line is wrong? We know the symptoms, not the disease So our job is like that of a doctor Find out what s making the patient sick!

  3. Debugging Sometimes just reading and tracing the program is enough And once upon a time that was the only option But doctors can run tests, ask questions If we could interact with the program while it s running we might be able to see the bug as it happens, we need X-Ray vision

  4. The debugger The debugger is a tool for controlling and inspecting a program as it runs Part of most IDEs (WingIDE and IDLE for sure) It does not find the bugs for you Instead, it Slows down the program by letting you run it one line at a time (step through code) Shows you variables and their values as they change Shows what functions are currently running ( call stack )

  5. Breakpoints When using the debugger, you usually start by setting a breakpoint. This tells the debugger to pause the program just before it executes that line. In WingIDE, click next to the line number A red dot appears to show there is a breakpoint there. Click it again to turn it off Note: breakpoints are not saved with the program! In IDLE, right click the line of code and choose Set breakpoint . The line will be highlighted in yellow

  6. Breakpoints To run with the debugger in WingIDE, click the Debug icon, the program will start running In IDLE select Debug Debugger then run the program normally (using Run Module) The program will run full-speed until control reaches the breakpoint Then the program pauses, the IDE gives you control If you run the program without using Debug , breakpoints are ignored, the program runs normally. You may have to put in more than one breakpoint if you have branches or loops, to make sure execution actually reaches one of them

  7. Single-stepping Single stepping means running the next line of the program. In most IDEs there are 3 ways to single-step. Step Over ( Over in IDLE) Execute the current line, pausing again when it finishes If the line calls a function, the whole function s code will be executed without being seen

  8. Single-stepping Step Into ( Step in IDLE) Execute the current line, pausing at the start of the next line to be executed If the line calls a function, execution will pause at the start of that function s definition Step Out ( Out in IDLE) Executing the rest of this function, pausing after it returns Stepped into a function you didn t want to see? Use this to get back to the calling code

  9. Single-stepping More about the difference in the different modes when we cover functions These stepping functions only work when your program is paused by the debugger So only in debug mode And you must have a breakpoint set somewhere

  10. Debugging variables The watch window shows you the variables that exist before executing the current line In WingIDE, called Stack Data In IDLE, called Locals Shows variables that are in scope Already defined in this function Not destroyed yet (at end of the function) Also shows their values and types Watch this window when single-stepping That will show you which variables are changing and how Make sure they change as you expect them to!

More Related Content