A View Inside My Head

Jason's Random Thoughts of Interest


CodeRush: Anonymous Method Refactoring Fun

I recently had to work with some code that I wrote a few years ago, and stumbled upon this:

Code showing an anonymous method being used as an Action delegate in Array.ForEach
(Not that it's relevant to the rest of this blog post, but this code was responsible for deleting a Role in a custom membership provider, but due to foreign key relationships without cascading deletes, all of the User/Role assignments had to be deleted first)

Now, in all fairness to myself, there's really nothing wrong with this code.  Array.ForEach<T> requires an Action<T> delegate, and this anonymous method certainly qualifies as one.

However, since the time that I wrote this particular piece of code, I started using Lambda expressions more often.  To be consistent with the rest of the project's code, I should change this anonymous method into a Lambda expression.

Fortunately, CodeRush makes this a breeze!  Simply put your cursor within with anonymous method itself, and then hit your Refactor Key and choose the "Compress to Lambda Expression" refactoring:

Compress to Lambda Expression refactoring
As simple as that, the wordy "delegate" syntax of the anonymous method is compressed into the terse "=>" syntax of a Lambda expression:

Array.ForEach with a Lambda Expression as the Action<T> delegate
But, suppose that you are just learning Lambda expressions, and come across one in code that you don't fully understand.  CodeRush allows you to go the other way, too, and expand a Lambda expression into an anonymous method, which may be easier to understand.  Similar to above, put your cursor on the Lambda expression, hit the Refactor Key, and then choose "Expand Lambda Expression":

Expand Lambda Expression refactoring
Anonymous methods were introduced as a way to inline functionality that otherwise had to exist in its own named method.  This is really only good if that functionality is used one time.  But, what if you discover that the functionality is needed in multiple places?

CodeRush provides a refactoring for anonymous methods called "Name Anonymous Method" that will extract the logic into a named method that can then be called from other code:

Name Anonymous Method refactoring
Activating this refactoring will first display the Target Picker (red arrow and line) so that you can select where to add the new method.  Then, the new method's name can be set by editing the text in the green box that will appear.

Finally, suppose that the opposite is true: you have a named method that is only used in one place, and would prefer to just inline that code.  Or, perhaps you would like to create a one-off modification to an existing method, and don't need the new functionality to be in its own named method.

CodeRush has a refactoring called "Inline Delegate" that replaces a delegate referring to a named method with an anonymous method.  If there is only a single reference to that named method, then this refactoring will also delete the named method altogether.

Inline Delegate refactoring

Since I was already familiar with the anonymous method syntax, CodeRush played an instrumental part in my learning Lambda expressions because I could write an anonymous method and then have it transformed into an equivalent Lambda for me.  But, once I knew Lambdas, I found that I wanted to update my older code in order to replace anonymous methods with the newer syntax, and CodeRush made it a trivial task to do this.

Happy Coding!