Guessing is bad (sometimes)
by pstradomski at 2009-04-07 22:00
“Be strict with what you generate, be permissive with what you accept” is a quite popular opinion when it comes to input/output from libraries, network protocols etc. But is it such a good idea? Because of it we are stuck with web as it is now, with bad markup everywhere – because it works (mostly). And yet even more problems arise.
A week ago I was hit by similar situation, but at a library API level. Zend_Date constructor accepts string representation of date and a format specifier (optional). Without explicitly specified format, it tries to guess the correct one.
Thus it interpreted '2009-03-31' correctly as 31st March. But the very next day unit tests for one piece of code broke. Zend_Date interpreted '2009-04-01' as 4th January instead of 1st April.
To quote the Zen of Python:
In the face of ambiguity, refuse the temptation to guess.
Comments
streser (not registered) at 2009-05-02 17:41: Problem with date interpretation is a standart unsolveable problem. Unfortunetly, that is one from many places where we must make validation and force user to type date in our format/formats. Unfortunetly becouse world/web could be better if everybody could do what they want, and how they want.
pstradomski at 2009-05-02 20:59: @streser — the date in question wasn't even typed in by the user, it was created programatically.