Why is it that diff programs work on a line-by-line basis instead of a hierarchal one?

All code can be expressed in a hierarchy, even though it's not immediately apparent.

Most of the data we work with is hierarchal as well.

What are the potential issues with building a piece of software that can diff based on hierarchy?

  • 1
    What are you going to use the diff for? Source control? Something else? – user40980 May 22 '13 at 4:33
  • Ideally it could be used anywhere where two versions of the same file exist. Source control is a prime example of this. Being able to see the differences between two JSON responses in a typical REST response would be good as well. – Brad May 22 '13 at 4:41
  • I think you have the beginnings of a decent question, but it could use some work to build it up. 1) The article you link doesn't really draw the conclusion that "all code can be expressed in a hierarchy." That's your supposition from the article. 2) Are you asking "how to build" or are you asking "why don't diff's do XYZ"? You've got two orthogonal questions there. Please edit and focus on one question and consider moving the other aspect to its own question. And please add some supporting research / effort on your part. – user53019 May 22 '13 at 13:13
  • See stackoverflow.com/a/3829587/120163 – Ira Baxter Jun 17 '13 at 8:48
  1. Because usually, diffs are created to be able to compare any file, not only hierarchical-organized source code or data.

  2. Because in order to obtain a tree from a source code, one needs to parse it first. Reading lines - every app can do that. Being able to parse C++, Ada, Java, COBOL, Haskell and hundreds of programming languages and non-programming languages is not so easy.

  3. Because showing some code as a tree will be extremely ugly. Imagine PHP code mixed with HTML with a deep hierarchy (including PHP code in HTML attributes).

But in some particular contexts when we are sure to have a limited set of languages, like Visual Studio, it would be nice to have a tree-based diff, indeed (as an option, with a choice between text-based and tree-based diff).

| improve this answer | |

Not the answer you're looking for? Browse other questions tagged or ask your own question.