Extract Class
See original GitHub issueIs this request related to a problem? Please describe.
Often when I’m refactoring, I find a hidden class. A collection of functions and properties that would be better in a new class or in another existing class.
Describe the solution you’d like
I’d like to be able to select a function or more, maybe even some variables, and extract it out to a new class. I’d also like for all existing calls to those functions to now be prepended with the new class.
Additional context
For example, I’d like to turn this:
class Foo () {
barSize: int = 30;
fooString: string = "baz";
doFoo () {
console.log(fooString);
}
doBar() {
return barSize /2;
}
doubleBar() {
return barSize *2;
}
}
class Baz() {
Foo.doBar();
Foo.doFoo();
}
into this:
class Foo () {
fooString: string = "baz";
doFoo () {
console.log(fooString);
}
}
class Bar() {
barSize: int = 30;
doBar() {
return barSize /2;
}
doubleBar() {
return barSize *2;
}
}
class Baz() {
Bar.doBar();
Foo.doFoo();
}
Being able to then move it into a new file would be a nice bonus.
Issue Analytics
- State:
- Created 4 years ago
- Reactions:1
- Comments:9 (6 by maintainers)
Top Results From Across the Web
Extract Class - Refactoring.Guru
This refactoring method will help maintain adherence to the Single Responsibility Principle. The code of your classes will be more obvious and understandable....
Read more >Extract Class - Refactoring
Extract Class. open in web edition · How do I access the web edition? refactorgram. class Person { get ...
Read more >Extract class - Wikipedia
In software engineering, the Extract Class refactoring is applied when a class becomes overweight with too many methods and its purpose becomes unclear....
Read more >Extract Class refactoring | ReSharper Documentation - JetBrains
Press Ctrl+Shift+R and then choose Extract Class. ; Right-click and choose Refactor | Extract Class from the context menu. ; Choose ReSharper | ......
Read more >How to Use Extract Class Refactoring to Create Amazing Code
Extract Class is a refactoring technique in which you move functionality from one class to another, creating a new class in the process....
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@avik-so I’m glad to tell you that, after so much time, this was implemented thanks to @justerest 🎉
If I take your example:
Executing “Extract Class” on
Foo
will pop up this modal to ask for the things you want to extract:And will generate such code:
Note that
Baz
hasn’t change. To preserve the existing behavior, everything was preserved onFoo
. So the next steps for you is to progressively adapt the clients to use the new class instead, so you can finally get rid of the things inFoo
and effectively complete the refactoring.The good thing is that all the part of creating a new class and moving the stuff is done automatically, without breaking the code.
The
ExtractedClass
is generated in the same file. We might enhance that later though, door is open to iterations.After giving a second thought about it, I think it’s easier than I thought.
We don’t actually need to index the project to do this refactoring, as I thought when I created https://github.com/nicoespeon/abracadabra/issues/46
Actually, all the information are contained in one file. Then, it’s relatively easy to put the extracted class into another file and import it.
I will consider implementing this feature sooner than I first thought.