CLion 2024.1 Help

Extract/Introduce variable

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.

This refactoring is available for C/C++, Objective-C/C++, Python and JavaScript.

Extract a variable in-place

  1. In the editor, select the expression to be replaced with a variable.

  2. Press Ctrl+Alt+V or select Refactor | Variable from the context menu.

  3. If more than one occurrence of the selected expression is found, select Replace this occurrence only or Replace all occurrences in the popup.

    Extract variable - multiple occurrences
  4. Specify the name of the variable. You can choose from the list of suggestions or set another name.

    Extracting a variable

    If you want to apply automatic type declaration for the variable, set the Declare auto checkbox.

    When applicable, you can also declare a variable as const by setting Declare const:

    Extracting a variable - declare options
Last modified: 27 June 2024