April 25, 2014
New Dropbox sync for TeX Writer

Once I worked on a project, I spotted a code section like this:

[text replaceOccurrencesOfString:@"&" withString:@"&"
    options:NSLiteralSearch range:NSMakeRange(0, text.length)];
[text replaceOccurrencesOfString:@"<" withString:@"&lt;"
    options:NSLiteralSearch range:NSMakeRange(0, text.length)];
[text replaceOccurrencesOfString:@">" withString:@"&gt;"
    options:NSLiteralSearch range:NSMakeRange(0, text.length)];

Then I thought -- without much thinking -- that we should get rid of repetitious code, so it became

NSRange fullRange = NSMakeRange(0, text.length);
[text replaceOccurrencesOfString:@"&" withString:@"&amp;"
    options:NSLiteralSearch range:fullRange];
[text replaceOccurrencesOfString:@"<" withString:@"&lt;"
    options:NSLiteralSearch range:fullRange];
[text replaceOccurrencesOfString:@">" withString:@"&gt;"
    options:NSLiteralSearch range:fullRange];

Of course this is a horrible mistake, because the length of text changes over  time. It's basically like saying because x + x = 2x, so that 1 + 1 = 21. Carelessly refactoring working code is both dangerous and expensive, let alone code rewriting. I was fully aware of the cost, at the beginning of April, when I decided to rewrite Dropbox sync code in TeX Writer from scratch. It's a tough decision to make, because you had to throw away all the tests and fixes that went along with the original code. However, two negative reviews in the App Store (they lost changes after sync) helped me make the final decision. We need a solution that will never overwrite people's work no matter what happens, and the previous solution just isn't strong enough. Today, the rewrite is finished and we have done various tests, especially when conflict happens. We hope to release it soon, but at the same time, if you want to help beta test, please let us know!