2.5.6 Safely Renaming Fields With Refactoring Tools

Safely Renaming Fields With Refactoring Tools

After a Player field becomes private, rename NumberOfTurns to numberOfTurns consistently. Use the editor to change the symbol rather than replacing unrelated text.

Use the Rename refactoring
  1. Save and build the working soccer project before editing.
  2. Select the field identifier in its declaration. Use Rename from the editor context menu; in Visual Studio the usual shortcut is Ctrl+R, Ctrl+R.
  3. Enter numberOfTurns and review the preview of changes before applying them.
  4. Check its declaration and uses inside Player, then build and repeat a known turn-count check.
Inspect the affected symbol
Members inside Player after the rename
private int numberOfTurns;

public int GetNumberOfTurns()
{
    return this.numberOfTurns;
}

The method still returns the same count. Do not rename GetNumberOfTurns just because its text contains similar words. Check comments and string labels separately; they are not necessarily references to the field.

For a private field, Find All References should help you inspect its implementation uses. Unexpected outside dependencies deserve investigation before continuing.

Apply the idea
Check your understanding