Skip to content

Latest commit

 

History

History
142 lines (114 loc) · 5.35 KB

ij-osx-extract-method-2.md

File metadata and controls

142 lines (114 loc) · 5.35 KB

Top => IntelliJ IDEA on Mac OS X

JetBrains IntelliJ IDEA on Mac OS X

Kata: Extract method 2

In this kata, we select several lines and extract them into a method. The automated extract method refactoring in IntelliJ IDEA does not generate exactly the result we want, and we must edit the new method manually to complete the refactoring.

Key sequences to practice

  • ⌃⇥ (Ctrl + Tab) - navigate from the project view pane to an editor pane
  • ⌘↖︎ (Command + Home) or fn⌘← (Fn + Command + Left Arrow) - navigate to the top of the file (in editor)
  • ⌥→ (Option + Right Arrow) - move forward by token
  • ⌥⇧→ (Option + Shift + Right Arrow) - move forward by token while selecting
  • ⌥← (Option + Left Arrow) - move backward by token
  • → (Right Arrow) - move the caret one position to the right
  • ⌘⌥⇧] (Command + Option + Shift + ]) - select from caret through end of block
  • ⇧ and ↓ (Shift) with (Down Arrow) - select lines
  • ⌘⌥M (Command + Option + M) - open the "extract method" dialog
  • fnF2 (Fn + F2) - go to the next error in the file
  • ⌘⏎ (Command + Return/Enter) - "smart" line split (preserves formatting)
  • ⌘⌫ (Command + Delete/Backspace) - delete the current line (with nothing selected)
  • ⌃↓ (Ctrl + Up Arrow) - Move backward to the previous method

Keystrokes without explanations

⌃⇥
⌘↖︎ (or fn⌘←)
⌘L 12
hold ⇧
↓ (repeat through line 29)
release ⇧
⌘⌥M
Visibility: Public
Name: validateArguments
Arguments: stringArg
OK
fnF2
⌥→
⌥→
⌘⏎
⌘⌫
⌥←
"
fnF2
⌘⌫
fnF2
⌘⌫
fnF2
⌥⇧→
⌥⇧→
⌥⇧→
!result.equals("")
fnF2
⌘⌫
⌃↑
⌥⇧←
void
fnF2
⌘⌫
fn⇧F2
⌥⇧→
⌥⇧→
⌫

Walkthrough of steps with explanations

  1. Open the Java source file, LongConditional.java.
  2. If focus is not already in the editor pane for LongConditional.java, press ⌃⇥ (Ctrl + Tab) and select the editor pane for LongConditional.java.
  3. Press ⌘L (Command +L) to open the "go to line number" dialog and navigate to line 12.
  4. Hold ⇧ (Shift) and press ↓ (Down Arrow) until lines 12 through 29 inclusive are selected.
  5. Press ⌘⌥M (Command + Option +M) to open the "extract method" dialog.
  6. In the "extract method" dialog, select:
  • Visibility: Public
  • Name: validateArguments
  • Arguments: stringArg, intArg
  1. Choose OK. The generated method has syntax errors.
  2. Press fnF2 (Fn + F2) to go to the next error in the file. That should take you to line 58, which should look like this:
        int errorCount = ; String result = ;

with the caret positioned just after the first equals sign (=).

  1. We don't need the variable errorCount at all. Press ⌥→ (Option + Right Arrow) twice to advance the caret up to the token, "String".
  2. Press ⌘⏎ (Command + Return/Enter) to do a "smart" line split. The caret remains on the first line, which we now want to delete.
  3. Press ⌘⌫ (Command + Delete/Backspace) to delete the current line. The caret is left at the end of the line reading, "String result = ;".
  4. Press ⌥← (Option + Left Arrow) to move the caret before the semicolon.
  5. Initialize the variable result to an empty string, so it looks like this:
        String result = "";
  1. Now we can delete the lines that refer to errorCount. Press fnF2 (Fn + F2) to move focus to the next error. Press ⌘⌫ (Command + Delete) to delete that line. Do this sequence of keystrokes twice to delete the lines where errorCount is incremented.
  2. Press fnF2 (Fn + F2) one more time to position the caret at the beginning of the token, "errorCount" on line 69, which should read
        if (errorCount > 0) {
  1. Press ⌥⇧→ (Option + Shift + Right Arrow) three times to select the text inside the parentheses. The selected text should be
errorCount > 0
  1. Overtype the selection with the text, "!result.equals("")", resulting in:
        if (!result.equals("")) {
  1. Now we will delete the remaining line that contains a reference to errorCount. Press fnF2 (Fn + F2) to move the caret to the next error, and ⌘⌫ (Command + Delete/Backspace) to delete that line.

  2. The method should not return anything. Press ⌃↑ (Ctrl + Up Arrow) to move the caret to the method declaration.

  3. Press ⌥⇧← (Option + Shift + Left Arrow) to move the caret ahead to the token "String" (method return type) and select it.

  4. Overtype the selection with "void", so the line now looks like this:

    private void validateArguments(String stringArg, int intArg) {

at this point, the return statement is marked as an error.

  1. Press fnF2 (Fn + F2) to advance the caret to the next error.
  2. Press ⌘⌫ (Command + Delete/Backspace) to delete the line containing the return statement.
  3. By changing this method to return void, we introduced an error on the line that calls the method. Press fn⇧F2 (Fn + Shift + F2) to move the caret to the previous error. This is the line that calls the validateArguments method.
  4. Press ⌥⇧→ (Option + Shift + Right Arrow) twice to select the text, "result = ".
  5. Press ⌫ (Delete/Backspace) to delete the selection. The line should now look like this:
        validateArguments(stringArg, intArg);

To reverse the changes:

  1. Press ⌘Z (Command + Z) repeatedly until the changes have been reversed. Should be 30 times.