question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Is 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:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:9 (6 by maintainers)

github_iconTop GitHub Comments

3reactions
nicoespeoncommented, Oct 17, 2020

@avik-so I’m glad to tell you that, after so much time, this was implemented thanks to @justerest 🎉

If I take your example:

class Foo {
  barSize: number = 30;
  fooString: string = "baz";

  doFoo () {
    console.log(this.fooString);
  }

  doBar() {
    return this.barSize /2;
  }

  doubleBar() {
    return this.barSize *2;
  }
}

class Baz {
  foo = new Foo();

  doBaz() {
    this.foo.doBar();
    this.foo.doFoo();
  }
}

Executing “Extract Class” on Foo will pop up this modal to ask for the things you want to extract:

image

And will generate such code:

class Foo {
  private extractedClass: ExtractedClass = new ExtractedClass();

  get barSize(): number {
    return this.extractedClass.barSize;
  }

  fooString: string = "baz";

  doFoo() {
    console.log(this.fooString);
  }

  doBar() {
    return this.extractedClass.doBar();
  }

  doubleBar() {
    return this.extractedClass.doubleBar();
  }
}

class ExtractedClass {
  barSize: number = 30;

  doBar() {
    return this.barSize / 2;
  }

  doubleBar() {
    return this.barSize * 2;
  }
}

class Baz {
  foo = new Foo();

  doBaz() {
    this.foo.doBar();
    this.foo.doFoo();
  }
}

Note that Baz hasn’t change. To preserve the existing behavior, everything was preserved on Foo. 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 in Foo 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.

3reactions
nicoespeoncommented, Nov 22, 2019

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found