Extract class refactor missing?

Hi, I have been using Resharper for a couple months now and can't count the number of times I wish there was an option to extract a new class from a group of methods. How I imaged it working is, you select the methods in the class you want to extract as a new class, then tell resharper how you want to get a reference to the new class (constructor injection, public property, etc). I do this refactor a lot. Is this currently possible in Resharper and I'm just missing something? Or is there plans to put in an 'extract class' refactor in the future?

Thanks,
Rob

6 comments
Comment actions Permalink

Select the methods and press Ctrl-R, M.

"Rob Emanuele"  wrote in message
news:21872608.271241291231551290.JavaMail.devnet@domU-12-31-39-18-36-57.compute-1.internal...

Hi, I have been using Resharper for a couple months now and can't count the
number of times I wish there was an option to extract a new class from a
group of methods. How I imaged it working is, you select the methods in the
class you want to extract as a new class, then tell resharper how you want
to get a reference to the new class (constructor injection, public property,
etc). I do this refactor a lot. Is this currently possible in Resharper and
I'm just missing something? Or is there plans to put in an 'extract class'
refactor in the future?

Thanks,
Rob

---
Original message URL: http://devnet.jetbrains.net/message/5279404#5279404

0
Comment actions Permalink

That keystroke is bound to 'Extract Method' for me (and by default), which does not apply to a group of methods and which isn't the refactor that I'm referencing. Extract method creates a method from a code block already contained in another method. I was looking for something that would create a class based off of methods already contained in another class. Any suggestions?

0
Comment actions Permalink

Hello Rob,

Does ReSharper | Refactor | Extract Superclass suit your needs? Thank you!

Andrey Serebryansky
Senior Support Engineer
JetBrains, Inc
http://www.jetbrains.com
"Develop with pleasure!"

That keystroke is bound to 'Extract Method' for me (and by default),
which does not apply to a group of methods and which isn't the
refactor that I'm referencing. Extract method creates a method from a
code block already contained in another method. I was looking for
something that would create a class based off of methods already
contained in another class. Any suggestions?

---
Original message URL:
http://devnet.jetbrains.net/message/5279414#5279414



0
Comment actions Permalink

Hi Andrey,

Unfortunately, no. 'Extract Superclass' moves the methods up to a newly created base class which the source class then inherits from. I'm looking for methods of a class to be moved to an entirely new class, where the original class would then need a reference to the newly created class. An example might clear things up:

public class OrderProcessor
{
     public OrderProcessor() { }

     public void Process(Order order)
     {
          //Doing lots of order processing stuff
          ...

          SendNotifications(Order.WatchersOrWhatever);
     }

     public void SendNotifications(List<Person> orderWatchers)
     {
          //Send email notifications
          orderWatchers.Select( p => SendEmailNotification(p));

          //Send other notifications
          ...
     }

     public void SendEmailNotification(Person person)
     {
          //Send email to person.
          ...
     }
}

The OrderProcessor class is doing too much even from the code we see here; it's handing notifications when that really shouldn't be it's responsibility. However, when you're developing the notification system you might do so in the OrderProcessor class in order to just get something working (especially if you're doing TDD). After getting it working, the refactor to do here is to introduce another class, say NotificationService, and have that class injected into OrderProcessor:

public class OrderProcessor
{
     private readonly NotificationService _notificationService;

     public OrderProcessor(NotificationService notificationService)
     {
          _notificationService = notificationService;
     }

     public void Process(Order order)
     {
          //Doing lots of order processing stuff
          ...

          //Send an email notification
          _notificationService.SendNotifications(Order.WatchersOrWhatever);
     }
}



public class NotificationService
{

     public void SendNotifications(List<Person> orderWatchers)
     {
          //Send email notifications
          orderWatchers.Select( p => SendEmailNotification(p));

          //Send other notifications
          ...
     }

     public void SendEmailNotification(Person person)
     {
          //Send email to person.
          ...
     }


}


Now we have better separation of concerns. This is a refactor I do a lot: I develop functionality in whatever class makes sense at the time, but once a set of functionality becomes clearly distinct and having that functionality in that class means the class is doing too much, I break out that functionality into a different class. This is what the 'Extract Class' refactor means to me, and I'm seeing that missing in ReSharper.

Also this is in Martin Fowlers Refactoring book, see http://www.refactoring.com/catalog/extractClass.html

I hope this helps explaining what I mean.
Thanks,
Rob
0
Comment actions Permalink

Hello Rob,

Now I see what you mean. There's a feature request in our tracker: http://youtrack.jetbrains.net/issue/RSRP-159792
and you're welcome to vote for it. Thank you!

Andrey Serebryansky
Senior Support Engineer
JetBrains, Inc
http://www.jetbrains.com
"Develop with pleasure!"

Hi Andrey,

Unfortunately, no. 'Extract Superclass' moves the methods up to a
newly created base class which the source class then inherits from.
I'm looking for methods of a class to be moved to an entirely new
class, where the original class would then need a reference to the
newly created class. An example might clear things up:

public class OrderProcessor
{
public OrderProcessor() { }
public void Process(Order order)
{
//Doing lots of order processing stuff
...
SendNotifications(Order.WatchersOrWhatever);
}
public void SendNotifications(List<Person> orderWatchers)
{
//Send email notifications
orderWatchers.Select( p => SendEmailNotification(p));
//Send other notifications
...
}
public void SendEmailNotification(Person person)
{
//Send email to person.
...
}
}
The OrderProcessor class is doing too much even from the code we see
here; it's handing notifications when that really shouldn't be it's
responsibility. However, when you're developing the notification
system you might do so in the OrderProcessor class in order to just
get something working (especially if you're doing TDD). After getting
it working, the refactor to do here is to introduce another class, say
NotificationService, and have that class injected into OrderProcessor:

public class OrderProcessor
{
private readonly NotificationService _notificationService;
public OrderProcessor(NotificationService notificationService)
{
_notificationService = notificationService;
}
public void Process(Order order)
{
//Doing lots of order processing stuff
...
//Send an email notification

_notificationService.SendNotifications(Order.WatchersOrWhatever);
}
}
public class NotificationService
{
public void SendNotifications(List<Person> orderWatchers)
{
//Send email notifications
orderWatchers.Select( p => SendEmailNotification(p));
//Send other notifications
...
}
public void SendEmailNotification(Person person)
{
//Send email to person.
...
}
}
Now we have better separation of concerns. This is a refactor I do a
lot: I develop functionality in whatever class makes sense at the
time, but once a set of functionality becomes clearly distinct and
having that functionality in that class means the class is doing too
much, I break out that functionality into a different class. This is
what the 'Extract Class' refactor means to me, and I'm seeing that
missing in ReSharper.

Also this is in Martin Fowlers Refactoring book, see
http://www.refactoring.com/catalog/extractClass.html

I hope this helps explaining what I mean.
Thanks,
Rob
---
Original message URL:
http://devnet.jetbrains.net/message/5279504#5279504



0
Comment actions Permalink

Ok, thank you Andrey!

0

Please sign in to leave a comment.