Often I face code where after some basic initialization there is some calculation and finally the result of the calculation is used for something else. Now I would like to extract this intermediate calculation as a method.
Consider the following (very simplified) example:
int _tmain(int argc, _TCHAR* argv[])
{
int basic = 1;
int additional = 3; //extract!
int intermediateResult = additional + basic; //extract!
int result = intermediateResult;
}
Instead I would like to have something like
int intermediateCalculation(int basic)
{
int additional = 3;
return additional + basic;
}
int _tmain(int argc, _TCHAR* argv[])
{
int basic = 1;
int intermediateResult = intermediateCalculation(basic);
int result = intermediateResult;
}
Again: This is a huge simplification.
When I select the two lines commented with "extract!" and apply "Extract Method" VA suggests "void MyMethod(int basic)". The result won't compile.
When I select the two lines excluding the last semicolon (after "basic") VA suggests "bool MyMethod(int &intermediateResult, int basic)". Again the result of this refactoring will not compile.
Is there any way or workaround to use "Extract Method" here?