Explore tens of thousands of sets crafted by our community.
Code Refactoring Techniques
10
Flashcards
0/10
Rename Method
Definition: Changing the name of a method to better convey what the method does. When to Use: Use when the method name is not descriptive of its purpose or has become misleading due to code changes over time.
Introduce Explaining Variable
Definition: Involves adding a named variable that explains the purpose of the complex expression. When to Use: Use when you have a complicated expression that is hard to understand at a glance.
Extract Class
Definition: Create a new class to hold data or methods that do not fit the current class's main responsibility. When to Use: Use when a class is doing too much work and has more than one responsibility.
Inline Method
Definition: Inline Method is the process of replacing a method call with the actual method body. When to Use: Use when the method is not complex and does not add clarity to the code.
Encapsulate Field
Definition: Transforming public fields into private fields with public getter and setter methods to provide access. When to Use: Use when you need to impose validation logic or encapsulation on fields accessed from outside the class.
Extract Method
Definition: This technique involves creating a new method by extracting a section of code from an existing method. When to Use: Use when you have code that can be grouped together, when a method is too long, or when code chunks are duplicated.
Extract Interface
Definition: Creating a new interface with methods that are common across different classes. When to Use: Use when multiple classes have a common set of methods, which can be used to define a common interface.
Replace Temp with Query
Definition: Instead of storing a result in a temporary variable, use a method to return the result directly. When to Use: Use when a temporary variable is used only to hold the result of an expression and is not modified later.
Move Method
Definition: This technique involves moving a method from its current class to another class where it is more relevant. When to Use: Use when a method is more concerned with the data or behavior of another class.
Collapse Hierarchy
Definition: The process of simplifying an inheritance hierarchy by either merging a subclass into a superclass or vice versa. When to Use: Use when a subclass and superclass are not different enough to warrant a separate hierarchy.
© Hypatia.Tech. 2024 All rights reserved.