Whole Tomato Software Forums
Whole Tomato Software Forums
Main Site | Profile | Register | Active Topics | Members | Search | FAQ
User name:
Password:
Save Password
Forgot your password?

 All Forums
 Visual Assist
 Technical Support
 Can "Extract Method" do this?
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Phoenix
New Member

8 Posts

Posted - May 15 2013 :  05:57:17 AM  Show Profile  Reply with Quote
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?

Edited by - Phoenix on May 15 2013 05:58:29 AM

feline
Whole Tomato Software

United Kingdom
19025 Posts

Posted - May 15 2013 :  9:48:54 PM  Show Profile  Reply with Quote
Unfortunately no. Extract Method requires / assumes that if you have selected the line declaring a variable, you have selected all lines that use that variable. There are a couple of changes we are considering making to Extract Method that would help here.

Firstly, warning you when a local variable is used outside of the selection, so you are warned you are going to break something:

case=2063

Secondly, having Extract Method also extract the declaration of a local variable, if all uses of the local variable have been selected and extracted:

case=63055

the second case is more likely to help, but unfortunately I don't currently have an estimate on when either of these is going to be done.

zen is the art of being at one with the two'ness
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
© 2023 Whole Tomato Software, LLC Go To Top Of Page
Snitz Forums 2000