Study smarter with Fiveable
Get study guides, practice questions, and cheatsheets for all your subjects. Join 500,000+ students with a 96% pass rate.
Refactoring is the disciplined art of restructuring existing code without changing its external behavior—and it's a skill you'll be tested on repeatedly throughout your programming career. Understanding these techniques connects directly to core course concepts like object-oriented design principles, code maintainability, and software architecture. When you encounter questions about cohesion, coupling, encapsulation, and polymorphism, refactoring techniques are often the practical application being assessed.
Don't just memorize what each technique does—know when to apply it and which design principle it supports. Exam questions frequently present you with problematic code and ask you to identify the appropriate refactoring, or they'll test whether you understand the trade-offs between techniques. Master the underlying concepts, and you'll recognize the right tool for any code smell you encounter.
These techniques break down complex code into smaller, more focused units. The core principle: smaller pieces are easier to understand, test, and reuse.
Compare: Extract Method vs. Extract Class—both decompose complexity, but Extract Method works within a class while Extract Class creates new classes. If an exam asks about reducing method length, think Extract Method; if it's about a class with too many responsibilities, think Extract Class.
Clear code communicates intent. These techniques make code self-documenting by ensuring names and structure reveal purpose.
x becomes customerAge, doStuff() becomes calculateTotalPrice()int total = getBase() + getTax(); use a getTotal() methodCompare: Rename vs. Replace Temp with Query—Rename improves existing names, while Replace Temp with Query eliminates variables entirely by encoding their purpose in method names. Both improve readability, but Replace Temp with Query also improves structure.
These techniques improve how code is organized across classes. The goal: high cohesion within classes, low coupling between them.
createOrder(String name, String address, String city, String zip) becomes createOrder(Address address)Compare: Move Method vs. Encapsulate Field—Move Method improves organization between classes, while Encapsulate Field improves organization within a class. Both reduce inappropriate access to data, but at different architectural levels.
These techniques reduce complexity by removing unnecessary indirection or conditional logic. Sometimes the best code is less code.
Compare: Inline Method vs. Extract Method—these are inverse operations. Extract Method is for code that's too long; Inline Method is for abstractions that don't earn their keep. Knowing when to apply each shows mastery of refactoring judgment.
| Concept | Best Examples |
|---|---|
| Single Responsibility Principle | Extract Class, Extract Method, Move Method |
| Improving Readability | Rename, Replace Temp with Query, Introduce Parameter Object |
| Reducing Coupling | Extract Interface, Move Method, Encapsulate Field |
| Increasing Cohesion | Move Method, Extract Class |
| Enabling Testability | Extract Interface, Extract Method, Encapsulate Field |
| Polymorphism & OOP | Replace Conditional with Polymorphism, Extract Interface |
| Simplifying Code | Inline Method, Replace Temp with Query |
| Data Protection | Encapsulate Field, Introduce Parameter Object |
A method in class Order frequently accesses fields from class Customer. Which refactoring technique would best improve this code's cohesion, and why?
Compare and contrast Extract Method and Extract Class. Under what circumstances would you choose one over the other?
You encounter a 15-line method with a temporary variable that's assigned once and used twice. Which two refactoring techniques might apply, and in what order would you apply them?
How does Replace Conditional with Polymorphism support the open/closed principle? Give a scenario where this refactoring would be appropriate.
A public field balance in a BankAccount class is accessed directly throughout the codebase. Which refactoring would you apply, and what additional capability does this enable that direct field access doesn't support?