I think the "while" condition should actually be
while ((days > 365 && !IsLeapYear(year)) || (days > 366 && IsLeapYear(year)))
(or perhaps some simplification that I'm too lazy to figure out right now)And then the inner "if" condition can be eliminated. Thus the final code could be:
while ((days > 365 && !IsLeapYear(year)) || (days > 366 && IsLeapYear(year)))
{
if (IsLeapYear(year))
days -= 366;
else
days -= 365;
year += 1;
}
This seems logical and easier to understand than the original too.Edit: if you want to get ternary-operator fancy:
while (days > (IsLeapYear(year) ? 366 : 365))
{
days -= IsLeapYear(year) ? 366 : 365;
year += 1;
} days_in_year = IsLeapYear(year) ? 366 : 365;
while (days > days_in_year) {
days -= days_in_year;
year += 1;
}#define diy(y) (IsLeapYear(y) ? 366 : 355)
while (days > diy(year)) { days -= diy(year); year += 1; }
But I wouldn't do it with a define but rather:
while (days > (days_in_year = IsLeapYear(year) ? 366 : 365)) { days -= days_in_year; year += 1; }
while (days > (days_in_year = IsLeapYear(year) ? 366 : 365)) {
days -= days_in_year;
year += 1;
} for (;;) {
int days_in_year = IsLeapYear(year) ? 366 : 365;
if (days <= days_in_year) break;
days -= days_in_year;
year += 1;
}Wrong. Completely ignoring all software engineering "best practices" was the problem. They could have reused a working library. They could have written unit tests tests. QA could have tested it. Other developers could have reviewed it, or written test cases without knowledge of how the code worked.
Basically, they ignored every rule for writing good code, and then ended up with a serious bug. Well, yeah. The whole "unit testing" "fad" exists for a reason -- it greatly improves the quality of software.
Many people take software development seriously enough to not let one brain fart cause a piece of software to brick thousands of devices.
This should work if that was a >= sign. And people at work don't believe me when I say how insanely careful I have to be when I write any code, especially code that deals with people's payroll and vacation time. One missing equals symbol and every Zune in the world froze. One missing equals symbol and every employee could lose 1/12 of their accumulated vacation time.
The ability to pay attention to "mundane details" is just as important as the ability to envision the whole software application from the project manager point-of-view, especially when working in smaller companies without additional oversight.