Extracting Methods

The extract method feature can create a method to replace all occurrences of a given code fragment.

 

 

Instructions on how to complete a procedure

To create a new method from an expression:

  1. In the editor, select the the code fragement which you would like to replace with a method.

  2. Right-click and select Refactor | Extract Method -or- click Alt+Shift+M.
    The Extract Method dialog is launched.

Extract Method dialog

  1. Enter the name of the new method in the Method name field.

  2. Select the Access modifier for your method.

  3. If multiple occurrences of the code fragement appear in your code, mark the 'replace all occurrences of statements with method' checkbox for all occurrences to be replaced with the new method.

  4. Mark the Generate method comments checkbox for comments to be created for your method.

  5. Click OK to apply your changes or click Preview if you want to see a preview of the changes that this refactoring will create.

  6. If you clicked preview a preview window will open with a changes tree showing all the changes which will be made to reflect the extracting of the method.

  1. The changes will be listed according to the context within which they appear. You can expand the nodes to see all changes within particular files, classes or functions.

  2. Use the Next / Previous Change arrows to scroll through all possible changes.
    Unmarking the checkboxes next to the changes will cause those changes not to take effect.

  3. Click OK to apply the changes.

The method will be extracted and the relevant changes made to the code.