I recently read an article by Mark Needham on how we tend to duplicate LINQ filters in our code. He makes a good point – for example, you might see the following code in your application.
While this is a pretty simple line of code we might end up duplicating this filter in a number of places. In his example he uses a simple property to avoid the duplication – I usually write my own filters.
The original code now also becomes much more readable.
Another bonus here is that we can write unit tests for our filters!
Less duplication, improved readability and testability. This trick is especially useful if you are using complex filters. If you’re wondering about my use of SystemTime – this is a trick I learnt from Ayende Rahien.