What is meta? ×
Meta Stack Exchange is where users like you discuss bugs, features, and support issues that affect the software powering all 133 Stack Exchange communities.

I made a few spacing edits to a question, changing some lines with 8 leading spaces into 4 leading spaces.

So why does the question's revision history say that, instead of removing 4 spaces, I removed 5 spaces and added 1? This is rather nonsensical at best and outright confusing at worst. Why can't it just display a removal of 4 spaces?

share|improve this question
5  
I don't know how diffs actually work under the hood but I imagine that it uses a lexer and clumps certain characters together into a single group (the spaces) and treats and changes among them as one. It saw a group of 5 spaces and saw you changed it to a "group" of 1 space. If you're familiar with compiler theory at all, you'll know that handling all possible corner cases like this is not a trivial thing to do. I suspect whoever coded it went for simplicity over getting every corner case perfect. This is just not an important issue either, it's not wrong that it is interpreted this way. –  Jeff Mercado Nov 7 '11 at 19:12

1 Answer 1

up vote 16 down vote accepted

For the most part, Jeff Mercado's comment on your question is correct; not everything in the diff will always make the most sense to a human in all edge cases.

For example, if you fix a typo to turn "wheather" into "weather", you're just removing a single letter – however, the diff will show that you removed one word and added another. While in that case, to a human it may actually be more obvious to just show a single removed letter, in many cases doing a character-by-character diff would just make things worse:

   char diff

So that's why we're doing word-by-word diff. Not only that; we're also considering trailing spaces part of the word; otherwise, you'd have this:

   word diff

But in most cases, the most readable diff is actually this:

   word diff including trailing spaces

– and the "most cases" thing is obviously what we're optimizing for.

That said, I've made some improvements to the whitespace handling in the diffs. One thing you noticed was even an actual bug: The reason that you seemed to be inserting one space and removing five was the fact that under certain circumstances, breaking the text into tokens for diffing created empty tokens, causing a a "nothing" was removed here, and a "nothing" was added there diff.

Since a "nothing" is invisible, and we're replacing invisible changes by a space (saying "you can't see it, but something changed here), this caused the 5/1 instead of 4/0 diff you observed.

Both this bugfix and the whitespace improvements will be in the next build.

share|improve this answer
    
Not that this is necessarily worth it, but there is (I think) a relatively simple way to detect most useful one or two letter changes like 'wheather' to 'weather'. In the majority of cases, this is a spelling correction, in which case it would be possible to simply check if the new word is a spelling correction for the previous one, and if the previous one was misspelled. Again, not that that would be worth it. –  HodofHod May 17 '12 at 5:00

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .