Quantcast
Channel: Developer Express Inc.
Viewing all articles
Browse latest Browse all 2370

New CodeRush Plugin – Extract Method and Inline literals

$
0
0

You know what it’s like. Every now and then you spot that you’re performing the same sequence of tasks over and over again, and you find yourself wishing that there was a way to automate what you’re doing.

Well you’re not alone. Just recently I had one such situation.

The pattern in question is a relatively simple one.

It usually starts with a method which itself calls several other methods.

ExtractAndInlineStart

In the middle of that code, you realise there are a few lines which you’d like to be able to reuse a little easier.

Naturally you reach for your trusty Extract Method refactoring. Highlight the code and extract away.

However what you’re left with isn’t quite what you want.

ExtractAndInlineSimple 

What you really want is to have those literal values passed in as parameters.

Your ultimate goal is more like:

ExtractAndInlineImplemented

There are 2 ways to go about this with CodeRush.

Option 1 – Method First

In this first case, you…

- Extract your Target Code as shown in the first example. (Extract Method x 1)
- Promote each literal to a parameter of the new method. (Promote Parameter x L)

For a hypothetical method with 3 parameters, this will result in 4 operations. (3 x Promote Parameter + 1 x Extract Method)

Option 2 – Params First (and Last)

This second version is more complicated, but suits the way some people think a little better.

- Extract each of the literal values in your Target Code (Introduce Local x L)
- Move the resultant locals up above the portion of code you’re looking to extract. (Copy and Paste x L)
- Extract your Target Code (Extract Method x 1)
- Re-Inline the literals you created in the first step. (Inline Temp x L)

For the same hypothetical method (with 3 params), this solution requires 10 operations. (3 x Introduce Local + 3 Copy\Paste + 1 Extract Method + 3 Inline Temp)

Each option suits different people’s approach, but both are a few more steps more than I’d like to have to perform

It’s lucky then that CodeRush is so Extensible Open-mouthed smile

Enter a new plugin

The new plugin provides an additional refactoring (Extract Method And Inline Temp) which sits in the usual smart Tag menu, and makes itself available alongside Extract Method.

All that’s required is a single click, and CodeRush will whisk you passed all of those tedious (and potentially error ridden) manual steps.

ExtractAndInlineRefactoring

As usual, this project is Now available on github with full source code and VSIX Install.

In a future post I’ll walk you through how this plugin works.

For now, just look for the latest release, and away you go Smile


Viewing all articles
Browse latest Browse all 2370

Trending Articles