0% found this document useful (0 votes)
3 views

Debugging Lecture

The document provides an overview of debugging techniques and tools, including pre-debugging steps, various approaches like brute force and backtracking, and the use of built-in debuggers. It emphasizes best practices such as planning code, using assertions, and modularizing functions to streamline the debugging process. Additionally, it highlights the importance of using modern tools and techniques to effectively identify and resolve errors in code.

Uploaded by

lidiyamkt
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Debugging Lecture

The document provides an overview of debugging techniques and tools, including pre-debugging steps, various approaches like brute force and backtracking, and the use of built-in debuggers. It emphasizes best practices such as planning code, using assertions, and modularizing functions to streamline the debugging process. Additionally, it highlights the importance of using modern tools and techniques to effectively identify and resolve errors in code.

Uploaded by

lidiyamkt
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

1

Debugging Lecture
A Bug’s Life
History’s first documented bug 3

Grace Hopper was the one to document the bug on a


notebook on the left. GHC is a big event now in celebration of
her contribution to the field.
Pre-Debugging Steps 4

● Study program code


○ As how did I get the unexpected result
○ Is it related to another part of your code
● Study available data
● Make a hypothesis
● Pick simplest input to test with and fail fast
● Generalize and plan your changes ( Tracking )
Note: Syntax errors are are easy to spot, while logic errors takes
the most time

1. Think and Plan before writing the code


2. Draw pictures
3. Explain the code execution flow to someone else
(interviewer) or a rubber ducky
Approaches 5

1. Brute Force method

● Least economical and similar to spreading out your print statements


strategically throughout your code
Approaches 6

2. Backtracking

● Start from the statement at which an error symptom has been discovered,
then derive backward till error is discovered. Also challenging as backward
methods might increase.
Approaches 7

3. Cause Elimination

● Listing causes that may presumably have contributed to the error


symptom and tests are conducted to eliminate every error.
Approaches 8

4. Bisection method

● Find approximately the halfway point in your code and print out some
relevant values in your code. If everything looks good, it’s code free, so the
next search area would be in the ¾ area of the code print and try to pinpoint
the place you are getting bad results.
● O(log n) time
Tools
Modern Tools for debugging 10

Depending on your IDE and your setup there are plenty tools built-in to help you debug your
code

1. Breakpoints and Debuggers [ref]


2. Print statements and stdout
3. Visualizers and code tracers [ref]
4. Bug reporting solutions Eg. Extensions, Packages, …
5. System logs
Internals of a Built-in Debugger 11

● In general, debugger is utility that runs target program in controlled


environment where you can control execution of program and see the state
of program when program is paused.

● Once you set breakpoint, you can start program in debug mode, it will pause
execution when program reaches the line where breakpoint is set.
Using Built-in Debuggers 12

● Breaking points: Stops the execution of the program

Now we can use stepping commands to execute program line by


line.

1. continue – Resume program execution until next breakpoint is


reached
2. step into – Execute program line by line stepping into function
3. step over – Execute program line by line but don’t go inside
function call
4. step out – Resume program execution until current function is
finished
Pros of Debug mode 13

● No more prints in production code


● Visualization of stack trace along with values and
variables
● Allows you to follow flow of execution, i.e. lines are
printed along with the stack frames
● Track the changes on the fly
● In recursive functions, you can visualize your states
and global variables easily
Assertions 14

● Statements that you can use to


set sanity checks during the
development process. Assertions
allow you to test the correctness of
your code by checking if some
specific conditions remain true,
which can come in handy while
you're debugging code.

● This will make the program fail if


conditions are not met before the
online judge runs its test.
Best Practices
Steps to follow 16

● Plan out your code ahead of time

● Decide what you are going to name the variables you will use over and over again

● Decide how you can best reuse code

● If your solution looks unnecessarily complicated, invest time in a simpler one before typing it out.
17

How to cut down your implementation/debugging time

Don’t Do

● Debug every line in your code ● Write a function / modularize


● Don’t step into every function ● Test the function, debug the function (Unit Test)
● Debug entire program ● Then do integration test / regression testing

Change code without a note Backup code


Remember bug exists Write potential bug in a comment
Change and Test code Test code
Forget where the bug was Compare versions of your code
18

Quote of the day

Programming allows you to think about thinking, and while


debugging you learn learning.

~ Nicholas Negroponte

You might also like