9.1 How Bugs Appear
Debugging software is the process of finding and fixing errors or bugs in a program. It's one of the most exciting parts of software development, helping to ensure programs work correctly.
History of the Term "bug"
The term "bug" in the context of computer programs was first used in the 1940s. Even before computers, the concept of "bugs" existed, referring to mechanical failures in various devices.
The first documented use of the term "bug" in computer technology was with a team working on the Mark II computer at Harvard University in 1947(!). The team found that a relay wasn't working because a moth had gotten inside. They removed the insect and logged it as the "first actual case of bug being found."
Although this wasn't the first use of the term "bug" to describe an error, this incident became famous and helped popularize the term.

Program Debugging
Debugging is the process of finding, diagnosing, and fixing bugs in software. The debugging process involves several steps:
- Error Detection: identifying symptoms that indicate an error in the program.
- Error Reproduction: creating conditions under which the error occurs again to better understand its nature.
- Problem Diagnosis: using various tools and methods to analyze the code and locate the cause of the error.
- Error Fixing: making changes to the code to resolve the error.
- Testing: verifying the fix and testing the program to ensure the error is resolved and no new issues arise.
So, a bug is a general term for any errors in programs, and debugging is the process of finding and fixing those errors.
9.2 Debugging Tools and Methods
Logging
Adding logs to the program code allows tracking its execution and getting information about the state of variables and operations at various points. For logging in JavaScript, there's a great library called winston.
Example of using logging:
const { createLogger, format, transports } = require('winston');
// Create a logger with debug level
const logger = createLogger({ level: 'debug' });
// Division function
function divide(a, b) {
logger.debug(`Dividing ${a} by ${b}`);
if (b === 0) {
logger.error("Attempt to divide by zero!");
return null;
}
return a / b;
}
// Example of using the function
const result = divide(10, 2);
console.log(result); // Outputs: 5
Using Debuggers
Debuggers are tools that allow developers to execute programs step-by-step, set breakpoints, inspect variable values, and modify them during program execution.
Popular Debuggers for JavaScript:
- Chrome DevTools: built-in debugger in Google Chrome browser
- Firefox Developer Tools: built-in debugger in Mozilla Firefox browser
- Node.js Debugger: built-in debugger for server-side JavaScript in Node.js
- Visual Studio Code Debugger: debugger built into the Visual Studio Code editor
Example of using a debugger in Node.js:
// Import built-in debug module
const { inspect } = require('util');
// Example of a function with an error
function faultyFunction(a, b) {
debugger; // Set a breakpoint
let result = a / b;
return result;
}
// Call the function with an error
console.log(faultyFunction(10, 0));
When the program hits debugger, it stops, and you can use debugger commands like step over, step into, step out, check variable values, and more.
We will learn debugging programs using the debugger built into Intellij IDEA (you'll need the Ultimate Edition).
9.3 Using Debug
In general, debugging involves actively using debugging tools and practices to identify and fix errors.
Debugging Steps:
- Set Breakpoints: set breakpoints in the code to stop the program execution at points of interest.
- Step Execution: execute the program step by step, observing changes in the program state and variable values.
- Variable Analysis: examine variable and expression values at various stages of program execution to identify incorrect data or logical errors.
- Error Fixing: make changes to the code to resolve the identified errors.
- Retesting: after fixing errors, test the program again to ensure the error is resolved and no new issues have arisen.
Sounds simple, right? In the next lecture, we'll go through each stage in detail :)
GO TO FULL VERSION