September 14, 2005

Bad code bites in an unexpected way

I've always known bad code bites you back in a number of ways, but this one was new. See the following code snippet:

string lastHeading = DateTime.MinValue.ToShortDateString();

foreach (Item i in GetItems()) {
  if (i.Date.ToShortDateString() != lastHeading) {
    lastHeading = i.Date.ToShortDateString();
    Console.WriteLine(lastHeading);
  }
  Console.WriteLine("  " + i.Text);
}

So what does it do? Quite nicely, it takes care of printing date headers for printed items in date order. That is, under the heading "2005-09-14" are printed the entries related to that particular date.

Now, you could ask, "What's wrong with that code then?" Yes, that's not entirely obvious. The issue here is that comparisons related to the "lastHeading" are string comparisons when in fact it's DateTimes being compared. Thus, this example violates the rule of "Always use the most correct and exact data type". However, the current approach could also be argued for by saying it's actually the "heading semantics" that rule here, thus making strings the correct data type - but I won't be going there. Not now.

Well, why did this sort of structure from somebody's code fail on me today? Believe it or not, it was because of the initialization to DateTime.MinValue.ToShortDateString(). Now, there's nothing fundamentally wrong with that - it's a surefire way to force the header to be printed (although setting the string to empty or null would be more logical here if we consider this to be purely string data).

Well, we got an ArgumentNullException with an interesting error message. Why's that? Because somebody ran the software using Arabic culture (ar-SA), which uses Hijri calendar system. Unfortunately Hijri calendar doesn't have a way to format anything as low as DateTime.MinValue. Uh... What a really awkward reason to fail.

The moral of the story: Date operations (DateTime, TimeSpan et al.) are by nature culture independent. Display formatting for them rarely is, so never assume too much. And use the correct data types.

Posted by Jouni Heikniemi at September 14, 2005 07:00 PM
.net
Comments

If the DateTime class supports localization, why doesn't MinValue respect the locale? I see nothing wrong in assuming a supposedly locale-aware class to return meaningful values for the selected locale.

Posted by: Juha-Mikko at September 15, 2005 11:42 PM

It is somewhat debatable whether or not the DateTime class should be locale-aware by its value conventions. Probably it shouldn't - in essence, it _is_ a "universal" data type similar to int etc., which are useful even in a culture whose numeric system couldn't represent negative values (just as an example).

Also, the concept of "DateTime.MinValue" isn't particularly useful in a calendar-specific concept - its most reasonable application is indicating some sort of a "null" or "empty" value and thus, any formatting should be special-cased anyway.

If DateTime value range was limited to the calendar representation range of the current thread culture, it would pose some interesting questions. For example, what would happen to a DateTime that's valid in culture A when switching the current thread culture to B, under which the given date wouldn't validate?

Posted by: Jouni at September 16, 2005 07:28 AM

buy lisinopril
[url=http://boinc.gorlaeus.net/view_profile.php?userid=15688]buy lisinopril [/url]
buy valtrex
[url=http://boinc.gorlaeus.net/view_profile.php?userid=15689]buy valtrex [/url]
buy chantix
[url=http://boinc.gorlaeus.net/view_profile.php?userid=15690]buy chantix [/url]
buy lortab
[url=http://boinc.gorlaeus.net/view_profile.php?userid=15691]buy lortab [/url]
buy cephalexin
[url=http://boinc.gorlaeus.net/view_profile.php?userid=15692]buy cephalexin [/url]
buy seroquel
[url=http://boinc.gorlaeus.net/view_profile.php?userid=15693]buy seroquel [/url]
buy zyrtec
[url=http://boinc.gorlaeus.net/view_profile.php?userid=15694]buy zyrtec [/url]
buy penicillin
[url=http://boinc.gorlaeus.net/view_profile.php?userid=15695]buy penicillin [/url]
buy phentermine
[url=http://boinc.gorlaeus.net/view_profile.php?userid=15696]buy phentermine [/url]
buy vioxx
[url=http://boinc.gorlaeus.net/view_profile.php?userid=15697]buy vioxx [/url]
buy prilosec
[url=http://boinc.gorlaeus.net/view_profile.php?userid=15698]buy prilosec[/url]
buy vytorin
[url=http://boinc.gorlaeus.net/view_profile.php?userid=15699]buy vytorin [/url]
buy nexium
[url=http://boinc.gorlaeus.net/view_profile.php?userid=15700]buy nexium [/url]
buy accutane
[url=http://boinc.gorlaeus.net/view_profile.php?userid=15701]buy accutane[/url]
buy lexapro
[url=http://boinc.gorlaeus.net/view_profile.php?userid=15702]buy lexapro [/url]
buy FolicAcid
[url=http://boinc.gorlaeus.net/view_profile.php?userid=15703]buy FolicAcid[/url]
buy lorazepam
[url=http://boinc.gorlaeus.net/view_profile.php?userid=15704]buy lorazepam [/url]
buy tramadol
[url=http://boinc.gorlaeus.net/view_profile.php?userid=15705]buy tramadol [/url]
buy cipro
[url=http://boinc.gorlaeus.net/view_profile.php?userid=15706]buy cipro [/url]
buy melatonin
[url=http://boinc.gorlaeus.net/view_profile.php?userid=15708]buy melatonin [/url]

Posted by: Ybsuqnnr at July 3, 2008 09:01 AM

online nexium
[url=http://burp.boinc.dk/view_profile.php?userid=9449]online nexium [/url]
online adderall
[url=http://burp.boinc.dk/view_profile.php?userid=9450]online adderall [/url]
online ativan
[url=http://burp.boinc.dk/view_profile.php?userid=9451]online ativan [/url]
online vytorin
[url=http://burp.boinc.dk/view_profile.php?userid=9452]online vytorin [/url]
online niacin
[url=http://burp.boinc.dk/view_profile.php?userid=9453]online niacin [/url]
online norvasc
[url=http://burp.boinc.dk/view_profile.php?userid=9454]online norvasc [/url]
online trazodone
[url=http://burp.boinc.dk/view_profile.php?userid=9455]online trazodone [/url]
online herceptin
[url=http://burp.boinc.dk/view_profile.php?userid=9456]online herceptin [/url]
online cephalexin
[url=http://burp.boinc.dk/view_profile.php?userid=9457]online cephalexin [/url]
online strattera
[url=http://burp.boinc.dk/view_profile.php?userid=9458]online strattera [/url]
online arimidex
[url=http://burp.boinc.dk/view_profile.php?userid=9459]online arimidex [/url]
online viagra
[url=http://burp.boinc.dk/view_profile.php?userid=9460]online viagra [/url]
online bactrim
[url=http://burp.boinc.dk/view_profile.php?userid=9461]online bactrim [/url]
online zoloft
[url=http://burp.boinc.dk/view_profile.php?userid=9462]online zoloft [/url]
online ultram
[url=http://burp.boinc.dk/view_profile.php?userid=9464]online ultram [/url]
online prednisone
[url=http://burp.boinc.dk/view_profile.php?userid=9463]online prednisone [/url]
online ciprofloxacin
[url=http://burp.boinc.dk/view_profile.php?userid=9465]online ciprofloxacin [/url]
online abilify
[url=http://burp.boinc.dk/view_profile.php?userid=9466]online abilify [/url]
online valium
[url=http://burp.boinc.dk/view_profile.php?userid=9467]online valium [/url]
online lorazepam
[url=http://burp.boinc.dk/view_profile.php?userid=9468]online lorazepam [/url]

Posted by: Mwlxnleb at July 4, 2008 06:03 AM

generic methocarbamol
[url=http://burp.boinc.dk/view_profile.php?userid=9529]generic methocarbamol [/url]
generic voltaren
[url=http://burp.boinc.dk/view_profile.php?userid=9530]generic voltaren [/url]
generic prilosec
[url=http://burp.boinc.dk/view_profile.php?userid=9531]generic prilosec [/url]
generic coumadin
[url=http://burp.boinc.dk/view_profile.php?userid=9532]generic coumadin [/url]

Posted by: Ygqotzyv at July 4, 2008 08:40 AM

no prescription zithromax
[url=http://burp.boinc.dk/view_profile.php?userid=9407]no prescription zithromax [/url]
no prescription botox
[url=http://burp.boinc.dk/view_profile.php?userid=9408]no prescription botox [/url]
no prescription fioricet
[url=http://burp.boinc.dk/view_profile.php?userid=9409]no prescription fioricet [/url]
no prescription seroquel
[url=http://burp.boinc.dk/view_profile.php?userid=9410]no prescription seroquel [/url]
no prescription levaquin
[url=http://burp.boinc.dk/view_profile.php?userid=9412]no prescription levaquin [/url]
no prescription hydrocodone
[url=http://burp.boinc.dk/view_profile.php?userid=9413]no prescription hydrocodone [/url]
no prescription ambien
[url=http://burp.boinc.dk/view_profile.php?userid=9414]no prescription ambien [/url]
no prescription adipex
[url=http://burp.boinc.dk/view_profile.php?userid=9415]no prescription adipex [/url]
no prescription topamax
[url=http://burp.boinc.dk/view_profile.php?userid=9416]no prescription topamax [/url]
no prescription doxycycline
[url=http://burp.boinc.dk/view_profile.php?userid=9417]no prescription doxycycline [/url]
no prescription cipro
[url=http://burp.boinc.dk/view_profile.php?userid=9418]no prescription cipro [/url]
no prescription magnesium
[url=http://burp.boinc.dk/view_profile.php?userid=9419]no prescription magnesium [/url]
no prescription prozac
[url=http://burp.boinc.dk/view_profile.php?userid=9420]no prescription prozac [/url]
no prescription augmentin
[url=http://burp.boinc.dk/view_profile.php?userid=9421]no prescription augmentin [/url]
no prescription calcium
[url=http://burp.boinc.dk/view_profile.php?userid=9422]no prescription calcium [/url]
no prescription ibuprofen
[url=http://burp.boinc.dk/view_profile.php?userid=9423]no prescription ibuprofen [/url]
no prescription meridia
[url=http://burp.boinc.dk/view_profile.php?userid=9424]no prescription meridia [/url]
no prescription toprol
[url=http://burp.boinc.dk/view_profile.php?userid=9425]no prescription toprol [/url]
no prescription effexor
[url=http://burp.boinc.dk/view_profile.php?userid=9426]no prescription effexor [/url]
no prescription alprazolam
[url=http://burp.boinc.dk/view_profile.php?userid=9427]no prescription alprazolam [/url]

Posted by: Jhjohhig at July 4, 2008 08:53 AM

order valium
[url=http://www.lonelyplanet.com/thorntree/profile.jspa?editMode=true&userID=678222]order valium[/url]
order adipex
[url=http://www.lonelyplanet.com/thorntree/profile.jspa?editMode=true&userID=678223]order adipex[/url]
order provigil
[url=http://www.lonelyplanet.com/thorntree/profile.jspa?editMode=true&userID=678225]order provigil[/url]
order coumadin
[url=http://www.lonelyplanet.com/thorntree/profile.jspa?editMode=true&userID=678226]order coumadin[/url]
order alli
[url=http://www.lonelyplanet.com/thorntree/profile.jspa?editMode=true&userID=678227]order alli[/url]
order pseudovent
[url=http://www.lonelyplanet.com/thorntree/profile.jspa?editMode=true&userID=678228]order pseudovent[/url]
order mobic
[url=http://www.lonelyplanet.com/thorntree/profile.jspa?editMode=true&userID=678229]order mobic[/url]
order yaz
[url=http://www.lonelyplanet.com/thorntree/profile.jspa?editMode=true&userID=678230]order yaz[/url]
order zyrtec
[url=http://www.lonelyplanet.com/thorntree/profile.jspa?editMode=true&userID=678232]order zyrtec[/url]
order testosterone
[url=http://www.lonelyplanet.com/thorntree/profile.jspa?editMode=true&userID=678234]order testosterone[/url]
order naprosyn
[url=http://www.lonelyplanet.com/thorntree/profile.jspa?editMode=true&userID=678235]order naprosyn[/url]
order valtrex
[url=http://www.lonelyplanet.com/thorntree/profile.jspa?editMode=true&userID=678236]order valtrex[/url]
order morphine
[url=http://www.lonelyplanet.com/thorntree/profile.jspa?editMode=true&userID=678237]order morphine[/url]
order xanax
[url=http://www.lonelyplanet.com/thorntree/profile.jspa?editMode=true&userID=678238]order xanax[/url]
order amitriptyline
[url=http://www.lonelyplanet.com/thorntree/profile.jspa?editMode=true&userID=678239]order amitriptyline[/url]
order levitra
[url=http://www.lonelyplanet.com/thorntree/profile.jspa?editMode=true&userID=678240]order levitra[/url]
order niacin
[url=http://www.lonelyplanet.com/thorntree/profile.jspa?editMode=true&userID=678241]order niacin[/url]
order zoloft
[url=http://www.lonelyplanet.com/thorntree/profile.jspa?editMode=true&userID=678242]order zoloft[/url]
order cialis
[url=http://www.lonelyplanet.com/thorntree/profile.jspa?editMode=true&userID=678243]order cialis[/url]
order lisinopril
[url=http://www.lonelyplanet.com/thorntree/profile.jspa?editMode=true&userID=678245]order lisinopril[/url]

Posted by: Xaawvzwz at July 4, 2008 06:37 PM

celexa
[url=http://mu.wordpress.org/forums/profile.php?id=699245]celexa [/url]
metronidazole
[url=http://mu.wordpress.org/forums/profile.php?id=699246]metronidazole [/url]
prozac
[url=http://mu.wordpress.org/forums/profile.php?id=699248]prozac [/url]
ibuprofen
[url=http://mu.wordpress.org/forums/profile.php?id=699250]ibuprofen [/url]
vytorin
[url=http://mu.wordpress.org/forums/profile.php?id=699251]vytorin [/url]
provigil
[url=http://mu.wordpress.org/forums/profile.php?id=699253]provigil [/url]
synthroid
[url=http://mu.wordpress.org/forums/profile.php?id=699256]synthroid [/url]
voltaren
[url=http://mu.wordpress.org/forums/profile.php?id=699260]voltaren [/url]
ativan
[url=http://mu.wordpress.org/forums/profile.php?id=699263]ativan [/url]
lexapro
[url=http://mu.wordpress.org/forums/profile.php?id=699265]lexapro [/url]
ultracet
[url=http://mu.wordpress.org/forums/profile.php?id=699266]ultracet [/url]
acyclovir
[url=http://mu.wordpress.org/forums/profile.php?id=699267]acyclovir [/url]
methamphetamine
[url=http://mu.wordpress.org/forums/profile.php?id=699270]methamphetamine [/url]
lamictal
[url=http://mu.wordpress.org/forums/profile.php?id=699274]lamictal [/url]
morphine
[url=http://mu.wordpress.org/forums/profile.php?id=699276]morphine [/url]
diazepam
[url=http://mu.wordpress.org/forums/profile.php?id=699278]diazepam [/url]
potassium
[url=http://mu.wordpress.org/forums/profile.php?id=699280]potassium [/url]
yaz
[url=http://mu.wordpress.org/forums/profile.php?id=699283]yaz [/url]

Posted by: Jkddkqne at July 4, 2008 06:44 PM

online nexium
[url=http://www.lonelyplanet.com/thorntree/profile.jspa?editMode=true&userID=678196]online nexium[/url]
online augmentin
[url=http://www.lonelyplanet.com/thorntree/profile.jspa?editMode=true&userID=678198]online augmentin[/url]
online synthroid
[url=http://www.lonelyplanet.com/thorntree/profile.jspa?editMode=true&userID=678199]online synthroid[/url]
online naproxen
[url=http://www.lonelyplanet.com/thorntree/profile.jspa?editMode=true&userID=678202]online naproxen[/url]
online paxil
[url=http://www.lonelyplanet.com/thorntree/profile.jspa?editMode=true&userID=678203]online paxil[/url]
online clomid
[url=http://www.lonelyplanet.com/thorntree/profile.jspa?editMode=true&userID=678204]online clomid[/url]
online toprol
[url=http://www.lonelyplanet.com/thorntree/profile.jspa?editMode=true&userID=678206]online toprol[/url]
online carisoprodol
[url=http://www.lonelyplanet.com/thorntree/profile.jspa?editMode=true&userID=678207]online carisoprodol[/url]
online ultracet
[url=http://www.lonelyplanet.com/thorntree/profile.jspa?editMode=true&userID=678208]online ultracet[/url]
online soma
[url=http://www.lonelyplanet.com/thorntree/profile.jspa?editMode=true&userID=678209]online soma[/url]
online potassium
[url=http://www.lonelyplanet.com/thorntree/profile.jspa?editMode=true&userID=678210]online potassium[/url]
online insulin
[url=http://www.lonelyplanet.com/thorntree/profile.jspa?editMode=true&userID=678211]online insulin[/url]
online lamictal
[url=http://www.lonelyplanet.com/thorntree/profile.jspa?editMode=true&userID=678212]online lamictal[/url]
online biaxin
[url=http://www.lonelyplanet.com/thorntree/profile.jspa?editMode=true&userID=678213]online biaxin[/url]
online gabapentin
[url=http://www.lonelyplanet.com/thorntree/profile.jspa?editMode=true&userID=678216]online gabapentin[/url]
online zithromax
[url=http://www.lonelyplanet.com/thorntree/profile.jspa?editMode=true&userID=678217]online zithromax[/url]
online vytorin
[url=http://www.lonelyplanet.com/thorntree/profile.jspa?editMode=true&userID=678218]online vytorin[/url]
online benadryl
[url=http://www.lonelyplanet.com/thorntree/profile.jspa?editMode=true&userID=678219]online benadryl[/url]
online propecia
[url=http://www.lonelyplanet.com/thorntree/profile.jspa?editMode=true&userID=678220]online propecia[/url]
online ambien
[url=http://www.lonelyplanet.com/thorntree/profile.jspa?editMode=true&userID=678221]online ambien[/url]

Posted by: Agdwvpvz at July 4, 2008 07:36 PM

cheap effexor
[url=http://www.imeem.com/people/TgQAdZA]cheap effexor[/url]
cheap thyroid
[url=http://www.imeem.com/people/bBk7lqM]cheap thyroid[/url]
cheap cialis
[url=http://www.imeem.com/people/YcsUv_6]cheap cialis[/url]
cheap vioxx
[url=http://www.imeem.com/people/PutmWm0]cheap vioxx[/url]
cheap ciprofloxacin
[url=http://www.imeem.com/people/U3GMGUL]cheap ciprofloxacin[/url]
cheap nexium
[url=http://www.imeem.com/people/elQD7j6]cheap nexium[/url]
cheap celebrex
[url=http://www.imeem.com/people/HtXT7lB]cheap celebrex[/url]
cheap coumadin
[url=http://www.imeem.com/people/ntpdqX1]cheap coumadin[/url]
cheap phentermine
[url=http://www.imeem.com/people/L7vynpm]cheap phentermine[/url]
cheap lisinopril
[url=http://www.imeem.com/people/K5_5Ppm]cheap lisinopril[/url]
cheap lexapro
[url=http://www.imeem.com/people/xRE4uUW]cheap lexapro[/url]

Posted by: Tytagqlm at July 5, 2008 01:42 AM
Post a comment









Remember personal info?