Inline Method

At least three times now, I've wished for a simple "Inline Method"
refactoring. During the course of other refactoring, a given method becomes
essentially trivial (containing just one or two lines of code), an is called
in so few places, that it makes no sense being a separate method any more...
it'd be nice to be able to just "inline" the method ... make the method go
away, and replace all invocations of the method with the method body. It's
just the opposite of "Extract method". Any possibility of this refactoring
being added?


1 comment
Comment actions Permalink

Hello,

Paul Bradshaw wrote:

At least three times now, I've wished for a simple "Inline Method"
refactoring. During the course of other refactoring, a given method becomes
essentially trivial (containing just one or two lines of code), an is called
in so few places, that it makes no sense being a separate method any more...
it'd be nice to be able to just "inline" the method ... make the method go
away, and replace all invocations of the method with the method body. It's
just the opposite of "Extract method". Any possibility of this refactoring
being added?

I concur, posting that idea was on my To Do list :) I'd sure like to see
that.

Also on that list was "Create Strategy". Suppose I have this piece of code:

public class Test
{
public void Lalalala() { /* ... */ }

public void DoSomething()
{
/* CODE A */
}

private int DoSomethingElse(string x)
{
/* CODE B */
}
}

Suppose I select those last 2 methods and then pick the "Create
Strategy" refactoring, this would lead (after some dialog box) to:

public abstract class MyStrategyBase
{
public void DoSomething();
private int DoSomethingElse(string x);
}

public class MyStrategy : MyStrategyBase
{
public void DoSomething()
{
/* CODE A */
}

public int DoSomethingElse(string x)
{
/* CODE B */
}
}

public class Test
{
// Create variable of type "base" and initialize with concrete
// class
private MyStrategyBase _strategy = new MyStrategy();

public void Lalalala() { /* ... */ }

public void DoSomething()
{
_strategy.DoSomething();
}

private int DoSomethingElse(string x)
{
return _strategy.DoSomethingElse(x);
}
}

If you find this interesting, I'm willing to document this in more
detail, it's a bit much to type for now, but this will give you an idea.

Obviously, in the resulting Test-class, the last 2 methods would be
perfect candidates for Paul's "Inline Method" refactoring.

Kind regards,
Filip.

0

Please sign in to leave a comment.