4

I am dissatisfied with the file comparison tools I have worked with so far. While no tool would be able to always predict the line matchings that make best sense, at least current tools should be able to do a better job when changes mostly consist of added and removed indentation. But the tools have I used end up matching braces in isolation and separating actually related code, and do other seemingly obvious mistakes.

An example will speak volumes, let us take this:

// We also have to add any specified prefix and (pre-period) suffix.

if (prefix) [spriteName insertString:prefix atIndex:0];

if (suffix)
{
   NSRange pos = [spriteName rangeOfString:@"."];
   if (pos.location == NSNotFound)
   {
      // No point, it's a base sprite, so just append.
      [spriteName appendString:suffix];
   }
   else
   {
      // Append where the period was found, just before it.
      [spriteName insertString:suffix atIndex:pos.location];
   }
}

and assume it gets changed to this:

// We also have to add any specified prefix and (pre-period) suffix.

if (prefix) [spriteName insertString:prefix atIndex:0];

while (we_need_to_do_stuff)
{
   if (suffix)
   {
      NSRange pos = [spriteName rangeOfString:@"."];
      if (pos.location == NSNotFound)
      {
         // No point, it's a base sprite, so just append.
         [spriteName appendString:suffix];
      }
      else
      {
         // Append where the period was found, just before it.
         [spriteName insertString:suffix atIndex:pos.location];
      }
   }
}

I don't want to namedrop too much, but what does FileMerge (the graphical diff/merge utility that ships with Xcode) do? This: Screenshot of FileMerge doing barely useful matching

…how thoughtful, FileMerge, you made sure to match the braces because they were at the same indentation level, and separated the related code. Couldn't have asked for more.

The usual solution in that case is to find the option to consider changes in the amount of whitespace as being not significant, and try again. But then I want to make sure I have not fumbled the indentation either, because it is something I am going to commit, so I go back to the options, disable the option, try again. Repeat for all the changes I want to review.

There has got to be a better way. Is it just me using my tools badly? Is it theoretically possible to improve this workflow? And this is a simple case, have trickier ones been solved in the abstract? How should I look for better tooling in this domain, given that this is hard to express as a search engine query?

1
  • 3
    Let us try to avoid answering with just specific tool recommendations, or if you have to do so, try to explain how and how well it improves the situation. Also, I routinely work with C, Makefiles, shell scripts, Python, Perl, PHP as well as two domain specific languages, plus configuration files, so a language-aware diff is not going to cut it. Apr 17, 2013 at 20:55

1 Answer 1

1

Here are some options you might want to consider.

  1. Some diff tools have an option to input regular expressions as part of the comparison process and some, I believe, have active people in the forums tweaking the expressions for a given language.

  2. Write your own diff tool that compares the syntax and tweak the code. I started this but you spend more time writing a tool than the time it saves.

  3. Since you are committing the code, generate an abstract syntax tree and compare the abstract syntax tree. I don't know of any diff tools that can do this, are free, and open source, but it has been years since I last did a good search for one.

My suggestion would be to take option one as number two takes more time than it saves and number three if you really want to start a new company and make some money. Also if you do write one, realize the GUI takes more time than the diff part, unless you are willing to settle for a text only output.

1
  • Thanks for the ideas. And yes, I do share the concern that solving this problem requires integrated work over all applicative layers up to the user interface, of which the nitty gritty of the matching algorithm would only be the tip of the iceberg. Apr 21, 2013 at 14:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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