September 2008 Entries

What’s Wrong With This Code? (#20)

Mike had to model answers. Yes or no answers, date and time answers - all sorts of answers. One catch was that any answer could be “missing” or could be “empty”. Both values had distinct meanings in the domain. An interface definition fell out of the early iterative design work:public interface IAnswer { bool IsMissing { get; } bool IsEmpty { get; } } Mike was prepared to implement a DateTimeAnswer class, but first a test:[TestMethod] public void Can_Represent_Empty_DateTimeAnswer() { DateTimeAnswer emptyAnswer = new DateTimeAnswer(); Assert.IsTrue(emptyAnswer.IsEmpty); } After a little work, Mike had a class...

Stupid LINQ Tricks

Over a month ago I did a presentation on LINQ and promised a few people I’d share the code from the session. Better late than never, eh? We warmed up by building our own filtering operator to use in a query. The operator takes an Expression<Predicate<T>>, which we need to compile before we invoking the predicate inside.public static class MyExtensions { public static IEnumerable<T> Where<T>( this IEnumerable<T> sequence, ...