Debugging
- In Turkish
- Hata ayıklama
In short
Debugging is the process of finding out why a program misbehaves, locating the faulty code and fixing it, often with the help of a tool called a debugger.
What is debugging?
Debugging is the work of tracking down and fixing bugs, the mistakes that make software crash, produce wrong results or behave unexpectedly. It usually follows a loop: reproduce the problem reliably, narrow down where it happens, form a hypothesis about the cause, test it, apply a fix, and confirm that the problem is gone without breaking anything else.
Developers use several techniques. The simplest is adding print or log statements to see values as the program runs. A debugger is a tool that lets you pause a running program at a breakpoint, step through it line by line, inspect variables and the call stack, and watch values change; browsers, code editors and command-line tools like gdb and Python's pdb all provide one. Other approaches include reading error messages and stack traces carefully, explaining the code out loud to someone else, known as rubber duck debugging, and searching through commits with git bisect to find the change that introduced a bug.
Debugging is a lot like detective work: you gather evidence, rule out suspects and follow clues until one explanation fits every fact. The hardest bugs are the ones that don't reproduce consistently, such as race conditions and flaky tests, because observing the program can change its timing. Good logging, small reproducible test cases and version control make these much easier to track down.
Debugging is often confused with testing. Testing tells you that something is wrong by checking the program against expected results, while debugging figures out why it's wrong and where. A failing test is often the starting point for debugging, and after the fix, a new regression test makes sure the same bug doesn't come back.
Key takeaways
- Debugging means finding the cause of a bug and fixing it.
- Start by reproducing the problem reliably, then narrow down where it happens.
- Debuggers let you pause at breakpoints, step through code and inspect variables.
- Testing reveals that something is wrong; debugging explains why.
- Add a regression test after fixing a bug so it can't silently return.
Example
def average(values):
total = sum(values)
breakpoint() # pauses here and opens the pdb debugger
return total / len(values)
average([]) # ZeroDivisionError: division by zero
# Inside the debugger you can type commands such as:
# p values print a variable (shows [])
# n run the next line
# c continue runningReaders ask
Where does the word debugging come from?
Engineers used "bug" for hardware faults long before computers existed. A famous 1947 logbook entry from the Harvard Mark II team records a real moth found in a relay, taped in as the "first actual case of bug being found", which helped make the term popular in computing.
What is a breakpoint?
A breakpoint is a marker you set on a line of code that tells the debugger to pause the program when it reaches that line. While paused, you can inspect variables, step through the following lines and see exactly what the program is doing.
What is rubber duck debugging?
It's the practice of explaining your code, line by line, to another person or even an object like a rubber duck. Putting the logic into words often reveals the mistake without any tools.
See also
- ExceptionProgramming Fundamentals, p. 19An exception is an event that interrupts a program's normal flow when an error occurs, which code can catch and handle instead of letting the program crash.
- Unit TestTesting & Quality, p. 35A unit test is a small, automated check that verifies one function, method, or class behaves correctly in isolation from the rest of the program.
- Git BisectVersion Control, p. 11Git bisect is a Git command that uses binary search through commit history to find the exact commit that introduced a bug, testing only a few commits.
- Regression TestingTesting & Quality, p. 22Regression testing is the practice of re-running existing tests after a code change to make sure that features which used to work have not broken.
- LoggingDevOps & Cloud, p. 35Logging is the practice of recording timestamped messages about events in a running program, such as errors and requests, so people can investigate them later.
- ObservabilityDevOps & Cloud, p. 38Observability is the ability to understand what is happening inside a running software system by collecting and analyzing its logs, metrics, and traces.
- IDEProgramming Fundamentals, p. 26An IDE (integrated development environment) is an application that combines a code editor, a debugger, build commands and other developer tools in one place.
- Stack TraceProgramming Fundamentals, p. 52A stack trace is the list of function calls in progress when an error happened, from the line that failed back to where the program started.
Spotted a mistake or something missing on this page?Suggest an edit