Bug
A bug is an error in software: the program behaves differently from how it should. Bugs are found by tests, code reviews, and the debugger; good error messages and logs help with the search.
A bug is an error in software: the program behaves differently from how it should, for example because a value is calculated wrongly, a special case is missing, or an assumption does not hold. Fixing it is called debugging.
Where bugs come from
The cause rarely lies where the error shows. Common ones are:
- Wrong assumptions: a value can never be empty, until it is (in C#, a case for nullable reference types)
- Edge cases: the daylight saving time change, the end of the month, an empty list, the last free appointment of the day
- Timing and concurrency: errors that only occur sometimes because two processes overtake each other in an unfortunate order
- Systems working together: configuration that is right locally and missing in the cloud
- Unclear requirements: the code does exactly what was written, but not what was meant
Finding bugs: the debugger
The debugger stops a running program so that you can look at its state. Following Microsoft's guide, you set a breakpoint in the code, start with F5, and then see the values of variables and the call stack. The breakpoint pauses before the marked line. Conditional breakpoints only stop when an expression is true or a hit count is reached, such as String.IsNullOrEmpty(name). That saves time in loops. The debugger does not replace thinking, but it replaces guessing.
Preventing bugs and finding them again
- Tests: A unit test that reproduces the error first proves the fix and protects against a relapse. That is the core of test-driven development.
- Review: A second pair of eyes (code review) finds what the author misses.
- Honest error handling: Instead of swallowing errors, web APIs report them as ProblemDetails, and logs record what happened.
- A clear ticket: steps to reproduce, expected and actual behavior.
Common misconceptions
- A bug is not a feature request. The difference lies in what was agreed: if the behavior deviates from what was agreed, it is a bug.
- A bug is not the same as an exception. An exception is a runtime mechanism; it can point to a bug, but also to an expected event, such as an interface that cannot be reached.
- There is no software without errors. The goal is to find bugs early and fix them cheaply (quality assurance).
In the course
You already learn debugging in K1, module 1.6, lesson Debugging: finding bugs instead of guessing. How a web API reports errors honestly is shown in K2, module 2.3, lesson Errors as ProblemDetails. Tests that catch errors early start in K1, module 1.8, with Why tests.
Further reading on the blog
Sources
- Microsoft Learn, Tutorial: Debug a .NET console application using Visual Studio Code: https://learn.microsoft.com/dotnet/core/tutorials/debug-console-app
- Microsoft Learn, Get started with breakpoints in the Visual Studio debugger: https://learn.microsoft.com/visualstudio/debugger/get-started-with-breakpoints?view=visualstudio