🎯 Learning Objectives
- Identify different types of programming errors
- Use debugging strategies to fix problems in code
- Test programs to ensure they work correctly
- Understand the importance of documentation in programs
🧠 What is Debugging?
Debugging is the process of finding and fixing errors, also called bugs, in a computer program.
A bug is any mistake in a program that causes it to:
- Crash
- Produce incorrect output
- Behave unexpectedly
🪲 Why Are They Called Bugs?
The word bug has been used for many years to describe problems in machines.
In 1947, engineers working on the Harvard Mark II computer found a moth trapped in the machine. They recorded it as a real “bug” in the system. Today, the term refers to any error in a program.
⚠️ Types of Programming Errors
1️⃣ Syntax Errors
A syntax error happens when the code breaks the rules of the programming language. The program cannot run until the syntax error is fixed.
Incorrect Java Code:
System.out.println("Hello World")
Correct Java Code:
System.out.println("Hello World");
Common syntax errors:
- Missing semicolons
- Missing brackets or parentheses
- Misspelled keywords
- Incorrect capitalization
system.out.println("Hello"); // incorrect
System.out.println("Hello"); // correct
2️⃣ Runtime Errors
A runtime error occurs while the program is running. The code may start, but it crashes during execution.
Examples of runtime errors:
- Dividing by zero
- Trying to use an invalid array index
- Opening a file that does not exist
int result = 10 / 0;
This causes a divide-by-zero runtime error.
3️⃣ Logic Errors
A logic error happens when the program runs without crashing, but it gives the wrong answer.
These are often harder to find because the program appears to work.
Incorrect logic:
int total = price * taxRate;
Correct logic:
int total = price + (price * taxRate);
🔎 Debugging Strategies
1️⃣ Read Error Messages Carefully
Error messages often tell you:
- The line where the problem occurred
- The type of error
- A possible reason for the problem
2️⃣ Use Print Statements
Print statements help track what the program is doing and what values variables hold during execution.
System.out.println("Value of total: " + total);
3️⃣ Test Small Sections of Code
Instead of trying to debug the whole program at once, test one part at a time. This makes it easier to find the problem.
4️⃣ Rubber Duck Debugging
This means explaining your code step-by-step out loud, sometimes even to an object like a rubber duck. Explaining the code often helps you discover mistakes.
🧪 What is Testing?
Testing is the process of checking whether a program works correctly under different conditions.
Testing helps programmers make sure a program:
- Produces correct output
- Handles unusual input properly
- Does not crash unexpectedly
🔬 Types of Testing
1️⃣ Normal Testing
Uses expected or typical input values.
Example:
Input: 10
Expected Output: 100
2️⃣ Boundary Testing
Tests values at the edge of the allowed input range.
If the valid age range is 0 to 120, good boundary test values are:
- 0
- 120
- -1
- 121
3️⃣ Extreme Testing
Tests unusual or unexpected values.
- Very large numbers
- Blank input
- Special characters
🧾 Documentation
Documentation is written information that explains how a program works.
Good documentation helps:
- Other programmers understand the code
- Future updates and improvements
- Debugging and maintenance
Types of Documentation
1️⃣ Comments in Code
// Calculate the total cost including tax
double total = price + (price * taxRate);
2️⃣ Program Description
A short summary that explains:
- What the program does
- What inputs it uses
- What outputs it produces
3️⃣ User Instructions
These explain how someone should use the program.
Enter a number between 1 and 100
✅ Best Practices for Debugging and Testing
- Write small sections of code at a time
- Test your program often
- Use meaningful variable names
- Add comments where needed
- Keep your code neat and organized
🏁 Summary
Debugging and testing are important skills in computer science. Programmers need to find errors, fix them, test their code carefully, and document how their programs work. These steps help create programs that are more reliable, easier to understand, and easier to improve later.