Extract Variable
Basics
The Extract Variable refactoring puts the result of the selected expression into a variable. It declares a new variable and uses the expression as an initializer. The original expression is replaced with the new variable.
You can select the expression to be replaced with a variable yourself. You can as well use smart expression selection. In this case CLion will help you select the desired expression.
This refactoring is available for C/C++, Objective-C/C++, Python and JavaScript.
Extracting variable in-place
To extract a variable using in-place refactoring, follow these steps
- In the editor, select the expression to be replaced with a variable. You can do that yourself or use the smart expression selection feature to let CLion help you. So, do one of the following:
- Highlight the expression. Then choose
Alternatively, pressCtrl+Alt+V.
on the main menu or on the context menu. - Place the cursor before or within the expression. Choose on the main menu or on the context menu. or press Ctrl+Alt+V.
- Highlight the expression. Then choose
- If more than one occurrence of the selected expression is found, select Replace this occurrence only or Replace all occurrences in the Multiple occurrences found pop-up menu. To select the required option, just click it. Alternatively, use the Up and Down arrow keys to navigate to the option of interest, and press Enter to select it.
- Specify the name of the variable. Do one of the following:
- Select one of the suggested names from the pop-up list. To do that, double-click the suitable name. Alternatively, use the Up and Down arrow keys to navigate to the name of interest, and Enter to select it.
- Edit the name by typing. The name is shown in the box with red borders and changes as you type. When finished, pressEnter.
- If you want to apply automatic type declaration for the variable, select the Select auto checkbox in the pop-up: Note that the IDE saves your choice as default setting for the next attempt of the Extract variable refactoring.
Last modified: 24 July 2018