Re: [threeten-develop] What does this code do?
Status: Alpha
Brought to you by:
scolebourne
From: Yishai H. <yh...@ka...> - 2012-10-03 13:06:00
|
Although I agree with your argument - primarily because I think the benefits will likely get lost due to these bugs, where in effect something that thought it was calendar agnostic won't work because the developer only really knew, thought about and tested the calendar(s) with which they were familiar, I would point out that the second example (with GregorianCalendar) isn't an issue concerning multiple calendar systems, just a naïve understanding of one calendar system. -----Original Message----- From: Stephen Colebourne [mailto:sco...@jo...] Sent: Wednesday, October 03, 2012 2:56 AM To: thr...@li... Subject: [threeten-develop] What does this code do? What does this code do? public void checkDates(Calendar calendar1, Calendar calendar2) { if (calendar1.get(Calendar.YEAR) > calendar2.get(Calendar.YEAR)) { assert calendar1.after(calendar2); } } Answer: It depends on the parameters. Calendar c1 = new BuddhistCalendar(); Calendar c2 = new GregorianCalendar(); c2.add(Calendar.DAY_OF_MONTH, 1); checkDates(c1, c2); // throws an assert exception Current Buddhist year is 2555, Gregorian year is 2012. But instant of c2 is after c2. Thus it throws an exception. So, we fix it by insisting on GregorianCalendar: What does this code do? public void checkDates(GregorianCalendar calendar1, GregorianCalendar calendar2) { if (calendar1.get(Calendar.YEAR) > calendar2.get(Calendar.YEAR)) { assert calendar1.after(calendar2); } } Answer: It depends on the parameters. Calendar c1 = new GregorianCalendar(); Calendar c2 = new GregorianCalendar(); c1.set(Calendar.YEAR, 2013); c1.set(Calendar.ERA, 0); checkDates(c1, c2); // throws an assert exception Code only checks year, ignoring the era. 2013 is after 2012, but 2013BC is not after 2012AD. Thus it throws an exception. Moral of the story. Inheritance of multiple calendar systems opens up a whole can of worms and a whole class of bugs. Like all software design decisions, it is a trade off. In this case the errors that can result are subtle and unexpected for the vast majority of developers who are not date/time or I18N experts. Given a trade off between a rarely used feature and a whole class of bugs, my answer is clear. Repeating the errors of Calendar and Joda-Time is simply not acceptable. Stephen ------------------------------------------------------------------------------ Don't let slow site performance ruin your business. Deploy New Relic APM Deploy New Relic app performance management and know exactly what is happening inside your Ruby, Python, PHP, Java, and .NET app Try New Relic at no cost today and get our sweet Data Nerd shirt too! http://p.sf.net/sfu/newrelic-dev2dev _______________________________________________ threeten-develop mailing list thr...@li... https://lists.sourceforge.net/lists/listinfo/threeten-develop |