PyCharm 2021.1 Help

Extract constant

The Extract Constant refactoring makes your source code easier to read and maintain. It also helps you avoid using hardcoded constants without any explanations about their values or purpose.

Extract a Python constant in-place

The in-place refactorings are enabled in PyCharm by default. If you haven't changed this setting, the Extract Constant refactoring for Python 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. Do one of the following:

    • Press Ctrl+Alt+C.

    • Choose Refactor | Extract | Constant from the main menu or the context menu.

  3. If more than one expression is detected for the current caret position, the Expressions list appears. If this is the case, click the expression to select it. Alternatively, press Up or Down 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, specify whether you wish to replace only the selected occurrence, or all the found occurrences with the new constant.

    Extracting a 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.

    specifying a name for the
extracting constant

  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.

Extract a constant using the dialog

If the in-place refactorings are disabled, the Extract Constant refactoring is performed by means of the Extract Constant Dialog dialog.

Enable inplace refactoring cleared
  1. In the Extract Constant Dialog dialog that opens, specify the name of the new constant.

  2. To automatically replace all occurrences of the selected expression (if it is found more than once), select the option Replace all occurrences.

  3. Click OK to create the constant.

The Extract Constant refactoring helps you avoid using hardcoded constants without any explanations about their values or purpose.

Extract constant

Extract a constant

  1. In the editor, select an expression or declaration of a variable you want to replace with a constant.

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

  3. If there are several expressions available for extracting, select the required one from the list that opens and press Enter.

  4. Select a name for the constant from the list of suggestions that opens or type a new one.

  5. Check the Put to header checkbox if you want the constant to be moved to the header file.

  6. Uncheck the Declare static checkbox if you don't want the constant to be declared as static.

  7. Press Enter.

Code example

BeforeAfter
@implementation PasswordValidator + (BOOL)isPasswordValid:(NSString *)password { // 4 will be extracted to a constant return password.length > 4; } @end
// Extracted constant static const int kMinPasswordLength = 4; @implementation PasswordValidator + (BOOL)isPasswordValid:(NSString *)password { return password.length > kMinPasswordLength; } @end
    Last modified: 08 March 2021