On Validating Input and Assertions
3 May, 2009 § 1 Comment
Can you assume that all months have at least 28 days? How about all minutes will have 60 seconds?
Actually, in 1752, the month of September had only 19 days (of which, I am lucky enough to say that my birthday still occurred). As to the latter question, leap minutes can have 61 or 62 seconds. What does this mean for a programmer?
It means you cannot assume that all data is what you expect it to be. What happens to your software if a user says that an event in history occurred on September 25, 1752? Does your program accept it and move on? What about calculating the number of seconds since 1970? It seems that with leap seconds you could be incorrectly calculating this.
This topic can also point to the need to rely on existing libraries and reinforce the need to not try and roll your own implementation since it is very easy to miss details like these.
Tip 33 of The Pragmatic Programmer says that, “If it can’t happen, use assertions to ensure that it won’t.” I’m almost half way through with reading the book and I like the book’s high level discussions on code quality and programming methodologies.