While doing some view updates, I ran into two issues: first: Remember that DateTime? needs to be cast to DateTime (or .Value needs to be retrieved) to do any DateAdd functions (the reasons for this are apparent when you think about them, but there you go...). Second, I really wish Microsoft would just bow to reality and implement a C# Language version of IEnumerable
I've read Eric Lippert's post on the issue and, while I understand what he's saying, I largely disagree. It may be "philosophical" to say that ".ForEach" would just provide "a side effect," but how many times do you find yourself writing this code:
//Stuff is an IEnumerable
foreach(var thingy in Stuff) { if(thingy.parameter == value) doSomething(thingy); }
or this:
Stuff.ToList().ForEach(t => {if(t.parameter == value) doSomething(t)});
Now, to my mind, neither of these is particularly hard to read, but, in both cases, I'm having to type more than I'd like to. My preferred method would be:
Stuff.ForEach(t => {if(t.parameter == value) doSomething(t)});
Even more useful, to my mind, is a way to handle "null" elegantly. I work with user input on a fairly regular basis, and sometimes users end up entering null values. Now- let it be said- the correct thing to do is to check to see if something is null BEFORE adding it to a collection. However, that doesn't always happen, and we should be able to handle it. So, I implemented my own SafeForEach as an extension method for IEnumerable
Finally, we come to another minor annoyance: Percentages- especially "of" 0. In the Issue Tracker, I'll be providing a Snap-Shot that provides a statistic for "Percent of Issues Closed in One Week Or Less." However, right now, I have no issues closed- so how can there be a percentage of 0? Now, I could define my percentage as closedInOneWeekOrLess/totalIssues, but that's not the metric I'm looking for (though it could be a useful metric to track, now that I think about it). I want the percentage of closed issues which were closed in one week or less. Until there are closed issues, however, there is no good way to provide that percentage.
So, on Double, I wrote another ExtensionMethod (also a good way to express exactly what you want better than just doing the math in code: .PercentageOf(double denominator). This is defined as "public static double PercentageOf(this double numerator, double denominator). It then does the math, and returns 0 instead of NaN when NaN would be provided.
The code for both of these methods will be provided either as an update/edit to this post, or in the main post which should come tonight or tomorrow.
No comments:
Post a Comment