Rather Than Rewriting Code, Sometimes You’ll Find It Easier to Swap Variable Values
You may not need to use this technique very often but when you do, it can save you a lot of time.
Calculating Years, Months, Days Problem
I wrote the HowLong(FromDay,ToDay) function to calculate the difference between two dates in years, months, and days. The way the function operates it starts at the first date (FromDay) and works its way forward to the second date (ToDay). If the first date occurs after the second date, the function yields bad results.
Originally, I added a trap to notify the user when the first date occurred after the second date—offering an opportunity to try again. However, I eventually realized that I only needed to reverse order of the dates to yield the right answer. Rather than rewriting a ton of code to create a backward-looking function, I left the original logic untouched by swapping the two date values when in reverse chronological order. Continue reading