Abstract: Hunks generated by textual-differencing tools are often used for understanding code changes. However, in the side-by-side view, the match between the deleted and added lines of a hunk is sometimes inconsistent with actual changes to the corresponding syntax entities. This mismatch usually occurs in syntax coupled hunks, i.e. hunks that contain changes to multiple syntax entities. It makes the hunks incomprehensible and misleading.
Introduction
Standard code differencing tools, such as git diff, operate on a textual, line-based level. While efficient and language-agnostic, these tools are "syntax-blind," meaning they do not recognize that a line of code belongs to a specific method, class, or control structure. This limitation leads to syntax coupled hunks: single blocks of changes that contain edits to multiple distinct syntax entities. In a side-by-side view, this often results in "mismatches," where a deleted line from one function is incorrectly aligned with an added line from an unrelated function, obscuring the actual logic of the change.
Methodology: HASTDiff
To solve this, we propose HASTDiff (Hybrid Abstract Syntax Tree Differencing), a two-phase algorithm designed to "untangle" these complex diffs:
- Tree-Differencing Phase: The algorithm parses code into Abstract Syntax Trees (ASTs) to match similar nodes between versions. It identifies specific logical operations—insert, delete, and update—at the structural level (e.g., recognizing that a method's parameters have changed rather than seeing it as a deletion and a new addition).
- Layout Phase: These logical edits are mapped back to line numbers to generate a structurally aware side-by-side view. By using "update" operations and context lines as anchors, the tool ensures that deleted code on the left truly corresponds to the added code on the right.
Prevalence of Syntax Coupling
Our research analyzed 93,676 commits across 10 popular open-source Java projects, including elasticsearch, spring-framework, and hibernate-orm. We found that syntax coupling is not an edge case but a frequent hurdle in professional software development.
As illustrated in the data, approximately 15% of all commits containing code changes involve syntax-coupled hunks. The vast majority of this coupling (77%) occurs at the method level ($H_{mthod}$), typically when developers refactor one method while simultaneously modifying another nearby.
Experimental Results
We evaluated HASTDiff's performance against the diff engines used by GitHub (GH) and Mergely (MG) using a manually verified sample of 1,437 hunk groups.
HASTDiff achieved a 97% correct matching rate, significantly outperforming GitHub (84%) and Mergely (77%). The primary "aha moment" is that HASTDiff recognizes structural context—treating a curly brace as part of a specific if-statement rather than just a character—preventing it from being incorrectly matched as context for an unrelated change.
Conclusion and Future Work
By combining the efficiency of textual diffs with the structural awareness of ASTs, HASTDiff provides a significantly more comprehensible view of code changes. While currently optimized for Java, the approach demonstrates that structural awareness is key to reducing developer cognitive load during code reviews. Future work will focus on expanding language support and exploring how these improved diffs can automate change dependency analysis.
Summary updated