Monday, May 15, 2006

Now everything is a nail

Diffs are something that I used only a couple times before starting at Microsoft, and now I use them each and day at work, and keep finding other uses for them in my home life. A diff of two files is essentially a composite view that shows what is different between two files. It's commonly used between an earlier version and the current version of a file.

The common usage scenario for a diff is this: say you have a file that contains the code for a class in your program. Something about that file changed since last Monday. What changed? Call up a diff between the current version and the version that existed last Monday. (Since you have access to a source control system, you have access to every version of every file in the product.) Now you know exactly what changed. A line toward the bottom was removed, so it's highlighted. An entire new method was added, so it's highlighted. Also, a couple lines were commented out, and a couple were reordered. You can easily tell what changed last week. It turns out that that one of those lines that was commented out is the problem—it should have been the line above.

Here's a screenshot that should illustrate things to the unfamiliar.

I knew diffs existed many years ago, but having rarely used them, problems that can easily be solved by them were usually solved in a less efficient way. The most recent time that I used them at home was when I was tweaking Oblivion's settings for better performance. I made a backup copy of my configuration file, and then edited the real one. One of the settings I used made the graphics look pretty terrible, and I didn't remember which one. So, I called up a diff between the backup copy and the live file, and saw exactly what I had changed. It took a couple seconds. In the past, I would have made a backup copy, and then kept a separate file listing everything I changed, hoping that I remembered to update the list.

When all you have is a hammer, everything looks like a nail. But, some of the things that I thought were screws before really were nails all along. I just forgot I had a hammer.

One of the diff tools that I use is the free and open-source WinMerge. (At work I mostly use two unreleased internal tools that have more features.)


Steven said...

I started using diffs last summer about the same time I started using Subversion to keep backups of my code. Diffs are damn handy.

I haven't ever used it for anything non-codish, such as an Oblivion config file (which makes me smile). I don't think it has ever occured to me to use it for such a task, but now I'll remember it exists.

I don't know if you can download Subverion's diff program without the full client, but it is definately softer on the eyes than WinMerge. TortoiseMerge also has a sidebar on the left that represents the whole document with colors indicating changes, additions, and deletions so that you can just go straight to them.

Anonymous said...

Most people start using diffs about the same time the exit software developer pre-pubescence.

Welcome to manhood gentlemen.

Travis said...

Yes. They're pretty useless for code in your own little personal projects because you're the only one working on them. But, they can be useful for other things besides code (the point of the post—many readers are not software developers and may never otherwise learn of diffs), and when you're working with code that someone else wrote or changed.

Anonymous said...

but Spomey, I thought you were a software developer (I know, VB probably doesn't count), and so my comment was more out of utter shock that you so recently discovered the usefulness of diff.

Its applicability in other areas is implicit ... or at least, it should be for anyone who has used it.

Travis said...

That was the point that I was trying to get across. I had used diffs in the past. Not very often, but a few times. But I had forgotten they existed by the time I started working at Microsoft. Their non-coding uses indeed appear only sporadically, as handy as a diff may be when they appear.

I forget things like that all the time. I forgot until recently that Remote Desktop can be set up to map the client machine's drives to drives on the server. I never remember that Windows+D immediately hides all of your windows. I forget that I own a Sidewinder Strategic Commander, a strange peripheral Microsoft designed for strategy games. I forget that Visual Studio has a call browser to show you which functions call which other functions.

Now I'm sure I'll never forget about diffs, because I use them all the time. But, it's easy to forget about tools you haven't used much. And, there are certainly readers of this blog who had never heard of them before.

Luke said...

Yes, diffs are quite useful. Right now, I am having new developers send me diffs and I just ok them before they get committed. That way, its an instant code review, but only of what changed, and you avoid an extraordinary amount of bugs that way.

Travis said...

And that would be the quintessential primary usage of a diff. :)