OPEN SOURCESHIPPED

Open Source: Apache Commons Imaging

Complexity analysis and test-coverage contribution to Apache's pure-Java imaging library — cyclomatic complexity reduced from 20 to 8, branch coverage of getImageInfo raised from 83% to 96%.

ROLEContributor
TEAMApache Commons community
TIMELINEJan 2026 — Mar 2026
CATEGORYOpen Source

20 → 8

CYCLOMATIC COMPLEXITY

83% → 96%

BRANCH COVERAGE

20 (by hand)

DECISION POINTS COUNTED

Overview

Apache Commons Imaging is a pure-Java image I/O library used across the Java ecosystem. As part of KTH's software engineering coursework on real-world codebases, I went after two unglamorous but high-value targets: complexity and test coverage in the PNG handling code.

What I did

  • Analyzed cyclomatic complexity rigorously — manually counting all 20 decision points of a target function and reconciling the discrepancy between the lizard tool's output and the theoretical CCN using an exit-point-aware calculation. (Tools disagree; understanding why they disagree is the actual skill.)
  • Refactored the function by extracting helper methods and replacing inline logic with named constants, cutting cyclomatic complexity from 20 to 8 without changing observable behaviour.
  • Raised branch coverage of getImageInfo from 83% to 96% by writing targeted JUnit tests for previously untested error and edge-case branches: empty chunks, duplicate PHYS/SCAL chunks, and missing palettes.

What I learned

Legacy code in a 20-year-old library earns its complexity one bugfix at a time. The discipline is to refactor with a coverage harness around you — write the tests for the branches first, then simplify, then prove nothing moved.

APPENDIX A // MEDIA

IMAGE SLOT — AWAITING UPLOAD

FIG.01 — COVERAGE REPORT BEFORE/AFTER TARGETED JUNIT TESTS