I've been thinking about Microsoft's proposed new language features for C# and I'm a little uncomfortable with anonymous methods. They're something that I always thought C++ needed when you were dealing with the STL's algorithm classes but they're not as needed in C#. You save a grand total of one line of code and make your code a bit easier to understand, but at a cost to testability. How do you unit test an anonymous method? You're committing yourself to a much larger testing effort. Consider an event that triggers when something happens in your code that is hard to trigger, or isn't a good idea to trigger such as shutting down Windows. If you don't use anonymous methods your unit test can just call this function to test the event being triggered, if you do use them then you can't and have to actually trigger the event.
I'm not saying that they're a bad idea, they're going to get a lot of use, but they are going to have to be used carefully if you're unit testing your code.
On the other hand generics are a great help to unit tests because they allow you to use mock objects in a new way. This makes testing UI code a lot easier as long as you can get around the constraints system they're adding. C++ lets you drop any class you want as the type and it'll only complain if you try and call a member that isn't available on that type. This means you can drop in a mock object that implements the members that you need and it'll not complain. It seems that constraints will enforce interfaces and so if you want to drop in a mock ListBox then you'll have to do a bit more in order to pull this off. If you can derive from the class then you can do it that way, but if not then you’re going to have a problem.
What other blogs are saying about this post.
11:01:58 PM
|