liveBook · Manning

Code analysis

As explained in Troubleshooting Java: Read, debug, and optimize JVM applications

The examination of source code to understand program behavior or investigate a possible cause of a problem.

Code analysis is the investigation of a software capability’s behavior. You might use it to find why a result differs from what you expected, understand logic before extending it, or learn how a dependency works. Investigation can also help locate the causes of slowness or an application stopping unexpectedly. section 1.1 section 1.2

Reading code is rarely a straight trip from top to bottom. You follow instructions into other methods and back again while tracking their effects on data. Names can suggest what a method does, but when its behavior matters, inspect the logic rather than relying on the name alone. section 2.1

Listing 2.1 An example of a method to debug
public class Decoder {
 
  public Integer decode(List<String> input) {
    int total = 0;
    for (String s : input) {
      var digits = new StringDigitExtractor(s).extractDigits();
      total += digits.stream().collect(Collectors.summingInt(i -> i));
    }
 
    return total;
  }
}

Use this Java method to practice identifying what you understand and what needs a closer look. section 2.1

Following an unfamiliar method

The decoder loops through input strings, extracts digits, and adds their values to a total. But the call to StringDigitExtractor leaves questions: does its constructor do anything beyond creating an object, and what does extractDigits() return? Following those operations helps replace assumptions with observations. section 2.1 section 2.2

When reading alone is not enough, use a debugger to make the behavior visible:

  • Identify the logic you want to investigate and the first instruction you do not understand. If you do not know where to start, use another investigation technique to locate it first. section 2.2
  • Set a breakpoint at that instruction and run the application with the debugger. Execution pauses there so you can proceed at your own pace. section 2.2
  • Inspect variables and their values, follow the execution path, and evaluate expressions as needed. These observations help you check what the code actually does. section 2.2

A debugger is one investigation tool, not a synonym for the entire process. Depending on the problem, profiling, logging, memory snapshots, or a combination of techniques may be more useful. Reproducing a problem in an environment you can study also helps you find its cause and confirm a fix. section 1.1 section 1.3 section 1.4

How Troubleshooting Java: Read, debug, and optimize JVM applications covers it

How deep it goes
Explains non-linear code reading and its uncertainties, then outlines debugger steps and when other investigation techniques may be needed.
Examples
Uses a Java decoder that extracts and sums digits to identify uncertainties in a constructor and method call.
What it assumes
Assumes Java language fundamentals and a known starting instruction for debugger use; suggests other techniques when the relevant logic is unknown.
What you can do after
Diagnose/debug with a debugger by selecting a known instruction, pausing execution, and inspecting the path and variable values.
How it connects
Connects code analysis to debugging, profiling, logging, memory snapshots, and problem reproduction.

Also explained in

Compare how 5 books explain code analysis →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Troubleshooting Java: Read, debug, and optimize JVM applications. The section links point to the parts of the book it draws on.