Replace Temp With Query
This refactoring allows you to extract the variable's initializer expression into a method, and replace all references to the variable with the calls to the extracted method. The declaration of the variable will be removed and the query method can be used in other methods.
int size = getActualSize() and using size throughout the code, we just operate with
getActualSize() method. Though the resulting code has more invocations, it is much cleaner and helps identify precisely where the bottlenecks in the code can appear.
To replace temp with query
- In the editor, position the caret at the name of the local variable you want to be refactored.
- On the main menu, or on the context menu of the selection, choose . Replace Temp with Query dialog box appears.
When selecting a local variable, make sure that its value is not modified later in the code. Otherwise the error message appears.
- Specify the name for the extracted method.
- To declare the method static, select the Declare static check box. This option is enabled when the initial expression is static.
- In the Parameters section, select the parameters to be used in the extracted method. The parameters are all checked by default. If unchecked, the appropriate value will be used as a local variable in the extracted method.
- Check the result in the Signature Preview pane and click OK to create the method.