← Back to Guided Projects

Module 2 Guided Project

Debugging Subscribe and Save Application

In this guided project, you'll learn how to apply debugging techniques to identify and fix issues in a subscription-based application. You'll practice using breakpoints, inspecting variables, and stepping through code execution.

The Scientific Method for Debugging

In this project, we'll apply the scientific method to systematically debug a Subscribe and Save application:

  1. Reproduce the bug with a small, repeatable test case
  2. Study the available data including error messages, logs, and stack traces
  3. Form a hypothesis about what might be causing the issue
  4. Experiment to test your hypothesis
  5. Repeat steps 2-4 until you've localized and fixed the bug

Bookstore Application Overview

We're working on a Bookstore application, focusing on the code that handles book deliveries, book orders, and checks on a particular title's quantity in stock. Our employees have reported a few odd behaviors of the system, so we want to check it out!

The key classes you'll be working with include:

  • StoreInventory: Contains current inventory of books
  • InventoryEntry: Represents current inventory of a single book

Debugging Techniques We'll Use

  • IDE Debugger: We'll set breakpoints at strategic locations to pause execution and examine program state
  • Variable Inspection: We'll inspect variable values during program execution to identify inconsistencies
  • Step Execution: We'll step through the code line by line to understand how it's being executed
  • Code Reading: We'll carefully examine the code to identify logical errors
  • Regression Testing: After fixing bugs, we'll verify the fixes don't introduce new issues

Example Bug: Incorrect Order Processing

// Sample code with a bug
public boolean processOrder(String bookTitle, int quantity) {
    // Check if we have enough books in stock
    if (hasInventory(bookTitle, quantity)) {
        // Bug: This line doesn't actually update the inventory!
        // It should be: removeInventory(bookTitle, quantity);
        checkInventory(bookTitle);
        
        // Create and save the order
        Order order = new Order(bookTitle, quantity);
        orderRepository.save(order);
        return true;
    }
    return false;
}
                

Project Steps

  1. Set up the debugging environment

    Clone the project repository and configure your IDE's debugging tools. Make sure you're comfortable with setting breakpoints and stepping through code.

  2. Understand the application structure

    Review the codebase to understand how the Subscribe and Save application works. Pay particular attention to the inventory management system and order processing logic.

  3. Identify the reported issues

    Read through the reported bugs and reproduce them with simple test cases. Take note of the expected vs. actual behavior.

  4. Set strategic breakpoints

    Place breakpoints at key locations in the code where you suspect the issues might be occurring. Focus on inventory update methods and order processing logic.

  5. Debug using the scientific method

    Run the application in debug mode, inspect variables, and step through code execution. Form hypotheses about what's causing each issue and test them systematically.

  6. Fix the identified bugs

    Implement fixes for each bug you identify. Be sure to document what you changed and why.

  7. Verify your fixes

    Re-run the application with your test cases to ensure your fixes resolved the issues. Also test other functionality to ensure you haven't introduced new bugs.