Refactoring Inline Class

Problem

A class does almost nothing and is not responsible for anything, and no additional responsibilities are planned for it.

Solution

Solution: Move all features from the class to another one.
Before
Inline Class - Before
After
Inline Class - After

Why Refactor

  • Often this technique is needed after the features of one class are "transplanted" to other classes, leaving that class with little to do.

Benefits

  • Eliminating needless classes frees up operating memory on the computer – and bandwidth in your head.

How to Refactor

  1. In the recipient class, create the public fields and methods present in the donor class. Methods should refer to the equivalent methods of the donor class.

  2. Replace all references to the donor class with references to the fields and methods of the recipient class.

  3. Now test the program and make sure that no errors have been added. If tests show that everything is working A-OK, start using Move Method and Move Field to completely transplant all functionality to the recipient class from the original one. Continue doing so until the original class is completely empty.

  4. Delete the original class.

Tired of reading?

No wonder, there are 7 hours worth of the text on this website.

Try out something different. We've just launched the interactive learning course on refactoring. It has more content and much more fun than a boring text.

Learn more...

Live Example

First time here? No worries!

Everything is quite simple. This example is like video (but much cooler):

  1. After pressing the Play button, you will see floating messages, which will guide you through the example. You can proceed by clicking on them.
  2. You can fast-forward or return to previous steps by pressing arrow buttons on the left.
  3. Also, you can take a look at the code diff window, which will show what has changed in code during the example ()
  4. To check the code for errors, you can press "Compile and test" button ()