🐞 AP Computer Science Principles

3.4 Debugging and Testing

Identifying and Fixing Errors • Code Testing and Documentation

🎯 Learning Objectives

🧠 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:

Programmers spend a large amount of time debugging because even small mistakes can cause a program to fail.

🪲 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:

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:

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:

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:

🔬 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.

🧾 Documentation

Documentation is written information that explains how a program works.

Good documentation helps:

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:

3️⃣ User Instructions

These explain how someone should use the program.

Enter a number between 1 and 100

✅ Best Practices for Debugging and Testing

🏁 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.