In Justika engineering team we have an agenda in every week, Tech Talk, one by one from us in turns speaking about tech, as much as possible the topic is beneficial for our team.
The last week is my turn to bring the material, I have summarized the topic about Refactoring: This class is too large originally narated by Clare Sudbary.
Common Problem in A Large Class
- Taking too much responsibility
- A lot of private nested code so it’s hard to test
- There is one large method that’s doing too much
- Repeating pattern with different details
Why Refactor?
By breaking the large class into smaller classes. The benefits:
- Know where to to go when want to make changes or fix issues
- Don’t need to understand the whole system to when amending the code
- Don’t worry changing a state in one area will affect the other
Principle
“Make the change easy, then make the easy change” - Kent Beck
Move in tiny steps with several connected aims:
- Run compiling and the tests at every step
- If a change causes any tests to fail, we able to fix them instantly
- Easy to keep track of where we are in
How To Do It?
-
Rearrange methods
Group the methods and properties without editing the code any way
-
Analyse relationship between methods
Identify how the code is connected to the rest of the code in the class so we can create new classes with minimum coupling
-
Modify the methods that are staying behind
Modify the methods that are stay in the parent class but currently called by those methods that are moving
How to deal with this:
- Make called method as public so we can call back to them from the new class.
- Move the calls out of the new class code and into other native parent class’s methods instead.
-
Create covering test for the new classes
For moving existing test to the specific test class.
Assume we will extract the methods of File Loading group to a class named FileLoader, in this step we should prepare a test class for FileLoader, we can name it as FileLoaderTest.
-
Create a new class, move the code in a small amount then run the tests
-
Repeat the (5) action until all the rests are moved and passed the test.
-
Extract more new classes
Reference:
- Refactoring: This class is too large by Clare Sudbery