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
Place the caret inside an expression or variable declaration to be replaced by a parameter.
Press Ctrl+Alt+P or select
from the main menu or the context menu.If several expressions are detected for the current caret position, select the required option from the list:
Specify the parameter name in the popup.
Preview and apply the changes.
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: 08 October 2024