The Extract Parameter refactoring is used to add a new parameter to a function declaration and to update the function calls accordingly.
Examples
Before
After
intfdiff(intx,inty);intmain(){intx=10;inty=9;intz=fdiff(x,y);return0;}intfdiff(intx,inty){//'2' will be extracted into the 'factor' parameterreturn(x-y)*2;}
intmain(intargc,constchar*argv[]){@autoreleasepool{floatresult;result=mulfunc(10,20);}return0;}floatmulfunc(intx,inty){//'2' will be extracted into the 'factor' parameterreturnx*y*2;}
defprint_test(self):# "test" will be extracted into a parameterprint"test"
defprint_test(self,test):printtest
Before
After
functioncalculate_sum(i){//'1' will be extracted into an optional parameteralert('Adding '+1+' to '+i);return1+i;}functionshow_sum(){alert('Result: '+calculate_sum(5));}
functioncalculate_sum(i,i2=1){alert('Adding '+i2+' to '+i);returni2+i;}functionshow_sum(){alert('Result: '+calculate_sum(5));}
To extract parameter
In the editor, place the cursor within the expression or local variable declaration to be replaced by a parameter.
Do one of the following:
Press Ctrl+Alt+P.
Choose Refactor | Extract | Parameter on the main menu or on the context menu.
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.
In the:
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.