Friday, March 29, 2013

Resharper as a coding instructor

I've been using Resharper a lot more lately. It's evolved nicely; while still a little annoying and intrusive the productivity increase is dramatic. Many of the features are things Microsoft should have added into Visual Studio years ago (they've been in Eclipse for what, 10 years?).

One of the neat things is that it is constantly suggesting better ways of coding or of structuring your code. I find myself learning about attributes of C# that I'd either forgotten, or hadn't know about.

I have a list of transactions that I want to add to the budget month container. Traditionally we might have done something like the following.

foreach (var transactionItem in items)
{
 month.AddTransaction(transactionItem);
}

This simply iterates over the collection of items, and for each one it calls the .AddTransaction method on the month.

My more modern method might be more like this:

items.ForEach(i => month.AddTransaction(i));

Resharper, though had a suggestion. Why not simplify that line even further? I'm game - so I clicked the suggestion. After all, there is always undo. This is the line;

items.ForEach(month.AddTransaction);

Simple, concise, and clear. I will confess that I had to run the unit test it in the debugger and inspect month's transactions just to make sure that the items had actually made it in there. Yup. Sweet.

No comments: