CLion 2018.2 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.

Examples

BeforeAfter
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; }
BeforeAfter
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; }
BeforeAfter
def print_test(self): # "test" will be extracted into a parameter print "test"
def print_test(self,test): print test
BeforeAfter
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)); }

To extract parameter

  1. In the editor, place the cursor within the expression or local variable declaration to be replaced by a parameter.
  2. Do one of the following:
    • Press Ctrl+Alt+P.
    • Choose Refactor | Extract | Parameter on the main menu or on the context menu.
  3. If more than one expression is detected for the current cursor 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.
    cl extractParameterExpressions
  4. In the:
    1. Specify the parameter name in the Name field.
    2. 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 changes.
    cl extractParameterName
Last modified: 16 June 2018

See Also

External Links: