Mastering Refactoring: When and How to Move Methods, Fields, and Classes
This article explains core refactoring techniques—Move Method, Move Field, Extract Class, Inline Class, Hide Delegate, Remove Middle Man, Introduce Foreign Method, and Introduce Local Extension—showing when to apply each, how to identify candidates, and the benefits for cleaner, more maintainable object‑oriented code.
In object‑oriented programming, clearly define each object's responsibilities; a class should do only what it is meant to do and fulfill its duties.
1. Move Method
Class behavior must follow the single‑responsibility principle; avoid overstepping. If a function interacts more with another class than its own, create a similar function in that other class, delegate the original, or remove it.
Moving functions is a pillar of refactoring. When a class has too many responsibilities or is highly coupled with another, move functions to simplify classes. Identify functions used more on another object than its own, examine callers and callees, and decide where to move.
2. Move Field
Fields should reside where they are most used. If a field is used more by another class, create the field in the target class and update all users to use the new field.
3. Extract Class
A class should be a clear abstraction handling specific responsibilities. When a class grows too large, create a new class and move related fields and methods to it. This prevents a “big ball of mud” and improves understandability.
4. Inline Class
If a small class no longer has enough responsibility, move its features into another class and delete the original.
5. Hide Delegate
Clients call another object through a delegate class. Build all needed functions on the service class to hide the delegation, reducing client knowledge of the underlying structure and easing future changes.
6. Remove Middle Man
When a service class becomes a simple pass‑through, let clients call the delegated class directly, eliminating unnecessary middle‑man code.
7. Introduce Foreign Method
If you need a new function for a service class you cannot modify, add the function in a client class and pass the service instance as the first parameter. This avoids duplicating code across clients.
8. Introduce Local Extension
When multiple extra functions are needed for a class you cannot change, create a new class that extends or wraps the original, providing all additional features in one place.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
ITFLY8 Architecture Home
ITFLY8 Architecture Home - focused on architecture knowledge sharing and exchange, covering project management and product design. Includes large-scale distributed website architecture (high performance, high availability, caching, message queues...), design patterns, architecture patterns, big data, project management (SCRUM, PMP, Prince2), product design, and more.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
