
Every programmer spends a large part of their time finding and fixing bugs. What separates professionals from beginners is not that they make fewer mistakes, but that they find them faster, because they follow a method instead of changing code at random and hoping. Debugging is essentially the scientific method applied to software: observe, form a hypothesis, run an experiment, and repeat until the cause is proven.
The word itself has a famous story. In 1947, engineers working on the Harvard Mark II computer found a moth trapped in a relay and taped it into their logbook with the note "First actual case of bug being found." Grace Hopper, a pioneer of programming who worked on the machine, liked to tell the story, and the logbook is now held by the Smithsonian. The word "bug" for a technical fault is older still; Thomas Edison used it in the 1870s, but the moth made it legendary.
You cannot fix what you cannot see. The first task is to find exact steps that make the bug appear every time. Note the input, the environment (browser, operating system, versions), the data and the sequence of actions. Then reduce the case: remove everything that is not necessary until you have the smallest example that still fails. Often this step alone reveals the cause.
Bugs that appear only sometimes, called intermittent or flaky bugs, usually point to timing, concurrency, caching, randomness or differences between environments. For those, collect as much information as possible each time the bug occurs, through logs, timestamps and the state of the system, until a pattern emerges.
Beginners often glance at an error and immediately start editing. Professionals read the whole message. It usually tells you three things: what went wrong (the error type, such as TypeError or NullPointerException), where it happened (file and line number), and how the program got there.
That last part is the stack trace: the list of function calls that were active when the error occurred. Find the first line that belongs to your own code rather than a library; that is usually where to look. Remember that the line where the program crashed is not always the line with the mistake. If a variable is unexpectedly empty, the real error is wherever it should have been filled.
If the message is unclear, search for it, removing details specific to your project such as file names. Official documentation and long-established answers on Stack Overflow often explain the most common causes.
State a specific guess: "The total is wrong because the discount is applied twice." Then design a quick experiment that would prove or disprove it, such as checking the value before and after the discount function. Change only one thing at a time. If you change three things and the bug disappears, you do not know which change fixed it, or whether one of them created a new problem.
Question your assumptions. Many long debugging sessions end with discovering that something "obviously" true was false: the file you were editing was not the file being run, the configuration was not loaded, the function was never called. Check the obvious things first.
Printing values with print() or console.log() is the oldest debugging technique and still useful for a quick look. Label your output, for example "total before discount: 120", so you can tell lines apart. In real applications, use a proper logging library with levels such as debug, info, warning and error. Good logs are your only eyes into programs running on servers, where you cannot attach a debugger.
A debugger lets you pause a running program and inspect it. Every major editor and browser has one. The essential features are:
Learning your debugger properly is one of the best investments a developer can make. It answers in seconds questions that print statements take many runs to answer.
For web development, the browser tools show the console, the network requests with their status codes and responses, the live HTML and CSS of the page and performance recordings. Many "JavaScript bugs" turn out to be a failed request visible immediately in the Network tab.
When you do not know where the bug is, cut the search space in half repeatedly, the way you find a word in a dictionary. Comment out half of a suspicious block, or check the data at the midpoint of a process. If the data is correct there, the bug is in the second half; if not, in the first.
The same idea works across history. If the code worked last week and fails today, git bisect automates the search: you mark one good commit and one bad commit, and Git checks out the commit in the middle for you to test. Even with a thousand commits between them, about ten tests are enough to find the exact change that introduced the bug, because each step halves the range.
Rubber duck debugging, described in the book The Pragmatic Programmer, means explaining your code line by line to someone, or something, that knows nothing about it. Saying out loud what each line is supposed to do forces you to notice where your belief and the code disagree. It works surprisingly often, and it is the reason a bug is frequently solved halfway through describing it to a colleague.
Once you find the problem, ask why it happened, not just where. Wrapping a crash in a try-catch block or adding a special case for one bad value may make the error disappear while leaving the real cause in place, ready to appear somewhere else. Ask whether the same mistake could exist in other parts of the code, and fix those too.
Before writing the fix, write an automated test that reproduces the bug and fails. Then fix the code and watch the test pass. This regression test stays in the test suite and guarantees the bug cannot quietly come back after a future change. Run the whole test suite too, to make sure the fix did not break anything else. In the commit message or bug report, write down the cause and the fix; the next person to meet a similar problem will thank you.
Grade 10 Math: Functions and Reasoning - Quiz
1170 questions