Skip to content
Structural Diff
step 1/5

Reading — step 1 of 5

Read

~1 min readProduction

Structural Diff

Line-based diff struggles with code:

Original:
  if (x > 0) {
    foo();
  }

After "improvement":
  if (x > 0) {
      foo();
  }

That's pure whitespace change. Line diff shows it as 1 deleted + 1 added line. Useless.

Structural diff (difft, gumtree, semantic diff tools) parses both files as AST and diffs the trees:

Original AST:                 New AST:
  IfStmt                        IfStmt
    >                              >
      x  0                          x  0
    Block                          Block        <-- different indentation
      Call(foo)                       Call(foo) <-- but same code structure

The diff highlights only meaningful changes.

Similarly for refactors: renaming a function shows as one node "rename foo -> bar" instead of git diff's thousands of line changes if the function is used everywhere.

Future of diff is structural. Until then, use git diff -w to ignore whitespace, and tooling like difftastic for code review.

Discussion

Ask a question, share an insight, or help someone who’s stuck.

Sign in to post a comment or reply.

Loading…