Wednesday, February 16, 2011

Date Time you are doing it wrong again.

The other day I reading the greatest developer fallacy or the wisest words you'll ever hear and then today I found some code that I think illustrated his point that learning it when you need it isn't always the way to go. This code looks very much that way.  Although I think a quick Google search would have found better code then this even if you didn't know what words to look for.

System.Text.StringBuilder mxIsDt = new System.Text.StringBuilder();
string mxDate = mxIsDt.ToString();
Which converts into around 70 lines of IL and takes just 19160 milliseconds to complete.   Now my question is why wouldn't you use ToString on DateTime to do this. 

var d = DateTime.Now.ToString("yyyyMdhms");            
Which takes 8 lines or less in IL and takes 1213 milliseconds to complete. It's way less code easier to maintain and examples on how to do this can be found all over the web.  

No comments: