Eclipse Tips and Tricks, Refactoring, Extracting Code into a Method

adevedo's picture
0
No votes yet

It was stated in Wikipedia that code refactoring is a "disciplined technique for restructuring an existing body of code, altering its internal structure without changing its external behavior", also was defined by Martin Fowler and Kent Beck as a change made to the internal structure of software to make it easier to understand and cheaper to modify without changing its observable behavior… It is a disciplined way to clean up code that minimizes the chances of introducing bugs.



Eclipse includes a set of actions that can help in refactoring Java codes using the most common actions that can be applied

In this article we show how we can use Eclipse to extract a block of code into a Java method

Example #1: Simple void method extraction


Assuming the following lines





Select the lines from 17 - 23 and click right click with the mouse, select the "Refactor" menu, from the "Refactor" menu select the item "Extract Method"





Method extraction dialog will appear, type the new method name, select the access modifier of the method and then click "Ok" button





Eclipse will convert the lines from 17 - 23 into a new method with the name you typed and with return type void and no parameters, and the lines will be replaced with a calling to the new method





Example #2: Extracting method with a return value


Assuming the following lines, the change here is that the variable defined at line 18 - in the middle of the selection range - will be used in the next lines to line 23, after the selection range





Again, select the lines from 17 - 23 and click right click with the mouse, select the "Refactor" menu, from the "Refactor" menu select the item "Extract Method"





Method extraction dialog will appear, note that eclipse detected that the method should return a type of List<String>, type the new method name, select the access modifier of the method and then click "Ok" button





Eclipse will convert the lines from 17 - 23 into a new method with the name you typed and with return type List<String> and no parameters, and the lines will be replaced with a calling to the new method and the method return value will be assigned to a new variable called "lines", which will be used in the next lines





Example #3: Extracting method with a return value and with parameters


Assuming the following lines, the change here is that the variable defined at line 19 - in the middle of the selection range - will be used in the next lines to line 24, after the selection range, and also the line number 18 reference a variable defined at line 17, before the selection range





Select the lines from 18 - 24 and click right click with the mouse, select the "Refactor" menu, from the "Refactor" menu select the item "Extract Method"





Method extraction dialog will appear, note that eclipse detected that the method should return a type of List and that the method should take 1 parameter of type String called "fileName", type the new method name, select the access modifier of the method and then click "Ok" button





Eclipse will convert the lines from 18 - 24 into a new method with the name you typed and with return type List and with 1 parameter called fileName of type String, and the lines will be replaced with a calling to the new method passing the varible defined at line 17 - fileName - as the method parameter and the method return value will be assigned to a new variable called "lines", which will be used in the next lines



Add new comment

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image.
By submitting this form, you accept the Mollom privacy policy.