Debugging
Отладка
It’s the process of finding out why software behaves differently from what you expect and checking that a fix works. A program can need debugging even if it runs without crashing.
Also known as: дебаг, debugger
What debugging is
A crash makes it obvious that something went wrong. A report that quietly drops one record can be harder to notice, but both call for the same question: where did the program first depart from the expected behavior? Debugging answers that question using evidence from an actual run. A debugger is a tool for inspecting a running program; using one is only one way to debug.
How to narrow down a bug
- Pin down the failure. Write down what happened and what should have happened. Record the input, application version, and relevant settings. Find the smallest case that still fails so you can run it repeatedly.
- Find the first wrong value or decision. If an exception occurred, read the stack trace to see the chain of calls. If the program returned a wrong answer, inspect intermediate results or compare a failing run with a successful one.
- Test one explanation at a time. Set a breakpoint before the suspect code and inspect the variables. Use Step Into to follow a function call or Step Over to run it without stepping through its body. A temporary log can be just as useful if you only need to see a value across several runs.
- Verify the fix. Run the original case again and check nearby edge cases. Where practical, keep the original case as a regression test that fails before the fix and passes afterward.
Example: the missing last reading
This Python function should sum the first count readings, where count is between zero and the length of the list:
def total_readings(readings, count):
total = 0
for i in range(count - 1):
total += readings[i]
return total
Calling total_readings([4, 7], 2) returns 4, not 11. A breakpoint inside the loop shows that i is 0 and never reaches 1. Python's range excludes its stop value, so subtracting one skips the last requested reading. Changing the loop to range(count) fixes the two-reading case. Also check that a count of 1 returns the first reading and a count of 0 returns zero.
Where it’s used and what can mislead you
Pausing a program is convenient on your laptop but can interrupt live requests in production. For a production issue, engineers usually start with logs and metrics, then use distributed traces if they need to follow a request across services. For a timing-sensitive bug, even pausing in a local debugger can change how threads are scheduled and make the problem disappear.
A failing test tells you which expectation was violated, not necessarily why. Likewise, the line that throws an exception may only be where bad data finally gets used. Check where that data came from before changing the line that failed.
In interviews
You may be given a failing test and asked to investigate it during a coding exercise. Say what you expect, walk through a small input, and explain which observation confirms or rules out your hypothesis. Once you have a fix, rerun the failing case and name a boundary case that could catch a related mistake.