CLion 2022.3 Help

Extract parameter

The Extract Parameter refactoring is used to add a new parameter to a function declaration and to update the function calls accordingly.

Extract Parameter uses either the default type value or the value with which the variable is initialized.

The following demo illustrates the usage of the Extract parameter refactoring, as well as Extract function, Extract lambda parameter, and Live templates:

Extract a parameter

  1. Place the caret inside an expression or variable declaration to be replaced by a parameter.

  2. Press Ctrl+Alt+P or select Refactor | Extract | Parameter from the main menu or the context menu.

  3. If several expressions are detected for the current caret position, select the required one from the list:

    the Extract Parameter expression list
  4. In the dialog that opens:

    • Specify the parameter name in the Name field.

    • If more than one occurrence of the expression is found within the function body, you can choose to replace only the selected occurrence or all the found occurrences with the references to the new parameter. Use the Replace all occurrences checkbox to specify your intention.

  5. Preview and apply the changes.

    Cl extract parameter name

Examples

Before

After

int fdiff (int x, int y); int main(){ int x = 10; int y = 9; int z = fdiff(x, y); return 0; } int fdiff (int x, int y){ //'2' will be extracted into the 'factor' parameter return (x-y)*2; }
int fdiff(int x, int y, int factor); int main(){ int x = 10; int y = 9; int z = fdiff(x, y, 2); return 0; } int fdiff(int x, int y, int factor) { return (x-y) * factor; }

Before

After

int main (int argc, const char * argv[]) { @autoreleasepool { float result; result = mulfunc(10, 20); } return 0; } float mulfunc(int x, int y) { //'2' will be extracted into the 'factor' parameter return x * y * 2; }
int main (int argc, const char * argv[]) { @autoreleasepool { float result; result = mulfunc(10, 20, 2); } return 0; } float mulfunc(int x, int y, int factor) { return x * y * factor; }

Before

After

def print_test(self): # "test" will be extracted into a parameter print "test"
def print_test(self,test): print test

Before

After

function calculate_sum(i) { //'1' will be extracted into an optional parameter alert('Adding ' + 1 + ' to ' + i); return 1 + i; } function show_sum() { alert('Result: ' + calculate_sum(5)); }
function calculate_sum(i, i2 = 1) { alert('Adding ' + i2 + ' to ' + i); return i2 + i; } function show_sum() { alert('Result: ' + calculate_sum(5)); }
Last modified: 27 December 2022