Geeks With Blogs
My Blog geek yapping October 2009 Entries
Apply DeMorgan's law refactoring
After reading up on some refactorings after a few hours of refactoring today, I thought it might be nice to share this refactoring with everyone. I ran into some code today, a simple equality comparison method, except it was comparing about 7 properties, so even worse than this: public bool Compare(item x, item y) { return !(x.a != y.a || x.b != y.b || x.c != y.c); } Even though it doesn't take long to figure out what it is doing, it takes more time to figure out than if we applied DeMorgan's law ......

Posted On Thursday, October 22, 2009 11:04 PM

Just wanted to share a quick extension method, it's really simple yes but it's power is in reducing lines of code. If you have ever wanted to apply an order by clause to a collection of items and conditionally do this based on a direction, you know that the only choices available are different methods OrderBy and OrderByDescending. It really is too bad because the internal OrderedEnumerable has a boolean flag for direction that would have been nice as a parameter, but better yet why not make it fluent ......

Posted On Monday, October 5, 2009 5:09 PM

ForEach or ForEachCopyIntoNewList?
All of us have desired a ForEach extension method in .Net for a while now, after being spoiled with all the new syntactic sugar with lambdas and linq in c#. We've no doubt all implemented our own, here is the one I use: public static void ForEach<T>(this IEnumerable<T> source, Action<T> action) { foreach (var item in source) { action(item); } } My only issue with this and the foreach loop itself, is that you cannot modify the original collection with your action. There are plenty ......

Posted On Friday, October 2, 2009 4:09 PM

Copyright © Wes McClure | Powered by: