I don't know about you guys, but I use Extract Method all the time. I've come up with some ideas for improving it, and I wanted you guys feedback on my proposed ideas before I filed an RFE officially.
I want to know if you think you would use / like such a feature, and if you have any ideas for improving it. My description might be complicated and poorly written, I hope you can understand it.
I often have complex code from which I want to extract a method consisting of many statements. (This RFE mostly applies to extracting series of statements, not simple single expressions.) The process I use for extracting methods from such code is usually:
1. Introduce Variable for the expression which I want to be the return value of the extracted function
2. Move the variable declaration to the place right after all its necessary values have been computed (so as not to include undesired statements in the extracted method)
3. Select each expression in the to-be-extracted code block, which references local variables / parameters, and:
___a. Inline Variable on the value of the local variable if it was a simple re-computable expression (like calling field getter)
___b. Introduce Variable for the expression, if I desire for that value to be passed in as an argument to the exracted method
This is a somewhat simple process now that I have gotten used to it, but I think the Extract Method refactoring would be more useful and powerful and easy to use if this process were automated.
My idea of the user experience for an enhanced Extract Method is as follows. I think the UI visually could be two miniature editor windows: one containing the current code in the editor, and another being a preview of the extracted method. This preview would be updated in real time, reflecting changes made throughout the process described below. There would also be a list of extracted method parameters somewhere in the window.
Each step in this list could be executed as part of a Wizard-style dialog, if the UI were too cumbersome otherwise.
1. User selects region of code which is to be extracted
2. User is prompted to choose an expression in the rest of the method which is the return value:
___a. The editor enters a mode where all expressions which use the value produced by the to-be-extracted method statements are highlighted (when the mouse rolls over, a Hand cursor is shown)
___b. The user chooses a return value by clicking one of the highlighted expression
3. User chooses extracted method parameters:
___a. Editor enters mode where all uses of original method variables/parameters, which will be passed as extracted method's parameters, are highlighted; a list of the parameters (perhaps with Google Toolbar-style color-coded matching highlighting) is shown in a floating/docked dialog
b. User can click one of the highlighted uses, to see a menu with up to three options: "Widen expression" "Inline outer expression" "Inline expression"
i. If user chooses Widen, the enclosing expression is selected (with Ctrl+W semantics), and the parameter now uses that expression instead of the original expression
ii. If user chooses Inline outer, the outermost expression (with CtrlShiftW semantics) is inlined into the method, using the parameter as the inner expression and the parameter which now represents the expression which the original expression contained
iii. If user chooses Inline expression, the entire expression is inlined in the extracted method, based on its last assignment in the original method. This may cause the parameter to be removed, and/or may cause new parameters to be created (if the expression in the last assignment used other local variables/parameters from the original method)
c. User can also select other un-highlighted expression in this mode, and mark the expression to be passed as an argument to the extracted method (user is asked for a name for this parameter)
4. Finally, user clicks "OK" button, and the specified method is extracted
I think this would be a great, powerful enhancement to IDEA's refactoring capabilities, requiring less work to extract methods.
What do you think - would you make use of this? Do you "extract methods" very often? Do you have ideas for making my proposed Extract Method process more effective?