Whole Tomato Software Forums
Whole Tomato Software Forums
Main Site | Profile | Register | Active Topics | Members | Search | FAQ
 All Forums
 Visual Assist
 Technical Support
 Can "Extract Method" do this?

You must be registered to post a reply.
Click here to register.

Screensize:
UserName:
Password:
Format: BoldItalicizeUnderlineStrikethrough Align leftCenterAlign right Insert horizontal ruleUpload and insert imageInsert hyperlinkInsert email addressInsert codeInsert quoted textInsert listInsert Emoji
   
Message:

Forum code is on.
Html is off.

 
Check to subscribe to this topic.
   

T O P I C    R E V I E W
Phoenix Posted - May 15 2013 : 05:57:17 AM
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?
1   L A T E S T    R E P L I E S    (Newest First)
feline Posted - May 15 2013 : 9:48:54 PM
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.

© 2023 Whole Tomato Software, LLC Go To Top Of Page
Snitz Forums 2000