Exercise 5: Refactoring in an IDE

This short exercise explores how some of the refactorings done in the previous exercise can be performed much more easily with the aid of the IntelliJ IDE. For more information on automated refactoring using IntelliJ, please see the following help pages:

Preparation

  1. Download the code for this exercise into the top level of your repository and unzip the file. This should create a directory named exercise5. The code in this directory is the car rental example from the lectures and previous exercise, before any refactorings have been applied.

  2. Follow the IntelliJ instructions from the Unit Testing exercise to import the exercise5 directory as an IntelliJ project and run the unit tests.

  3. IntelliJ should have recognised that you are working in a Git repository. Open the ‘Commit’ tool window, either by using the button on the left edge of the IDE window, or by choosing ViewTool WindowsCommit from the menubar.

    IntelliJ will show you a Changelist consisting of 13 unversioned files. Use the checkbox to select all of these files. Enter a suitable message in the ‘Commit message’ text area, then click the Commit button at the bottom of the tool window.

Extract Method

  1. Open Customer.java and locate the statement() method. Use the mouse to select the switch statement inside the loop and the preceding definition of the thisAmount variable. Then choose RefactorExtractMethod, or press Ctrl+Alt+M on Linux/Windows (⌥+⌘+M on Mac).

    When the Extract Method dialog appears, change the method name to amountFor, uncheck the ‘Generate annotations’ box, then click the Refactor button. Examine the code to review the changes that IntelliJ has made.

  2. Run the tests and make sure that they still pass.

Renaming Variables

  1. In the editor, go the newly-created amountFor() method. Click on the definition of the thisAmount variable inside this method. Choose RefactorRename from the menubar or press Shift+F6.

    You won’t see a dialog appear because renaming is done directly in place, within the editor. Simply type amount as the new name and press Esc to finish. All occurrences in the method will be changed.

    Run the tests and make sure that they still pass.

  2. Use the Commit tool window to commit the code changes. Before you click the Commit button, you’ll need to use the checkboxes to select the changes that should be included in the commit, and you’ll need to update the ‘Commit message’ text area with a new message.

  3. Press Ctrl+V to open the VCS Operations pop-up menu. Choose the ‘Push’ option from this menu to open the Push Commits dialog. Review the commits to be pushed, then click the Push button at the bottom-right of the dialog to push those commits to gitlab.com.

Move Method

In the lectures, we moved the definition of the amountFor() method from Customer to Rental, renaming it in the process. This can be achieved with two steps in IntelliJ.

  1. Position the cursor on the name of the amountFor() method, then choose RefactorMove Instance Method…, or press F6. On the resulting Move Instance Method dialog, choose ‘Public’ as the visibility, then click Refactor.

    Run the tests and make sure that they still pass.

  2. Now change the name of the moved method to getCharge(), following the same approach as used earlier in Renaming Variables. Notice how the method call made in the statement() method of Customer is also changed.

    Once again, run the tests and make sure that they still pass.

  3. Use the Commit tool window to commit these changes to your repository.

Inline Temp

In the lectures, we eliminated the temporary variable thisAmount by applying Inline Temp. We can do the same very easily in IntelliJ.

  1. Edit Customer.java and go to the statement() method. Position the cursor on the thisAmount variable – either on the line where it is defined or on of the two subsequent lines where it is used.

  2. Choose RefactorInline Variable, or press Ctrl+Alt+N on Linux/Windows (⌥+⌘+N on Mac). Observe the changes that have been made. Then run the tests and make sure that they still pass.

  3. Use the Commit tool window to commit these changes to your repository. Then use the VCS Operations pop-up menu to push the recent commits up to gitlab.com.