CLion 2023.3 Help

Extract constant

The Extract Constant refactoring helps you eliminate hard-coded constants, making your code easier to read and maintain.

Extract a constant in-place

In-place refactorings are enabled in CLion by default. If you haven't changed this setting, the Extract Constant refactoring for C/C++ is performed in-place, right in the editor.

  1. Place the caret within the expression or declaration of a variable to be replaced by a constant.

  2. Press Ctrl+Alt+C or select Refactor | Extract/Introduce | Constant from the main menu or from the context menu.

    Introducing constant
  3. If more than one expression is detected for the current caret position, the Expressions list appears. If this is the case, select the required expression. To do that, click the expression. Alternatively, use the Up and Down arrow keys to navigate to the expression of interest, and then press Enter to select it:

  4. If more than one occurrence of the expression is found within the class, specify whether you wish to replace only the selected occurrence, or all the found occurrences with the new constant.

  5. Specify the name of the constant. Select the name from the list or type the name in the box with a red border.

  6. To complete the refactoring, press Tab or Enter.

    If you haven't completed the refactoring and want to cancel the changes you have made, press Escape.

    Note that sometimes you may need to press the corresponding key more than once.

Last modified: 15 March 2024