Software Design/Extract Class
Extract Class is a general refactoring with several more specific instances which have varying positive effects but mostly the same drawbacks:
- Extract Wrapper Object aims at removing logic duplication in the code.
- Extract parts of a class into smaller classes improves code's robustness, clarity, apparency of semantics, testability, and reusability.
- Introduce Parameter Object aims at improving interface robustness.
This article corresponds to Extract Class refactoring described in Refactoring.[1]
Why not
[edit | edit source]Extracting a class contributes to pointer indirection that might slow down the software if objects of the extracted class are accessed frequently. It might also make the software to use more memory or spend more time in garbage collection in environments such as JVM (both effects are manifestations of higher resource usage) if a lot of objects of the extracted class are created.
Another disadvantage of this refactoring is that it spreads the logic over more locations that makes the readers to navigate more. See practice Don't break logic into too many small functions.
Related
[edit | edit source]- Split responsibilities between classes and functions
- Extract loosely coupled parts of a class into smaller classes
References
[edit | edit source]- ↑ Refactoring: Improving the Design of Existing Code (2 ed.). 2018. ISBN 978-0134757599. https://martinfowler.com/books/refactoring.html. Chapter 7, "Extract Class" section