Skip to content
Tutorials Center
My learning

Topics

Lesson 16 of 40 ยท Editing and Navigation

Refactoring and Rename Symbol

Change code safely in VS Code: rename symbols across files with F2, extract variables and functions, move to a new file, review references and compare how rename differs from find and replace.

Refactoring means changing the structure of code without changing what it does: renaming a function, splitting a long function in two, moving code to another file. Doing it with text search is risky, because text does not know meaning. VS Code asks the language service (the extension that understands your language) to do the change, so only real references are touched.

Rename Symbol (F2)

Put the cursor on a function, variable, class, method or parameter and press F2. Type the new name and press Enter. The language service finds every reference to that exact symbol, in every file of the project that it knows about, and renames them all in one step.

Why this beats Find and Replace:

Situation Find and Replace Rename Symbol (F2)
Variable count and a different local count in another function Renames both Renames only the one you pointed at
The word appears in a string or comment Renames it Leaves it alone
counter contains count Needs Whole Word Handled automatically
Imports in other files Must be matched manually Updated

The setting editor.renameOnType (linked editing) is a different, lighter feature: in HTML it renames a closing tag as you edit the opening one. The setting editor.rename.enablePreview (on by default) shows a Refactor Preview panel with a checklist of changes, so you can accept or reject each file before applying. Press Shift+Enter in the rename box to open the preview.

After a rename, check the changes in Source Control, run tests and, if needed, undo with Ctrl+Z (undo is per file; Git helps for the whole change).

Rename a file and update imports

Renaming or moving a file in the Explorer can update imports automatically. VS Code asks "Update imports for 'old.js'?" (controlled by javascript.updateImportsOnFileMove.enabled and typescript.updateImportsOnFileMove.enabled: prompt, always or never). Other languages provide similar behavior through their extension.

Extract and inline

Select code and press Ctrl+Shift+R (Refactor...) or the light bulb to see available refactorings. Common ones, depending on the language:

  • Extract to function / method: turns the selected statements into a new function and replaces them with a call, inferring parameters and return value.
  • Extract to constant / variable: pulls an expression such as price * 1.2 into a named variable.
  • Inline variable: replaces a variable with its value everywhere.
  • Convert to arrow function, convert to template literal, convert var to const (JavaScript and TypeScript).
  • Move to a new file: moves a function or class to its own file and fixes imports.
  • Add/remove braces, generate getters and setters, implement interface (TypeScript, Java and C# extensions).

Java, C#, Python and others offer their own lists. Install the official extension for your language and press Ctrl+. on code to see the actions.

Understanding impact first

Before a risky change, find out who uses the code:

  1. Shift+F12 (Go to References) lists every use; step through it in the peek window.
  2. Call Hierarchy (Command Palette: Peek Call Hierarchy, Shift+Alt+H) shows which functions call this one, and what it calls.
  3. Type Hierarchy shows base and derived classes.
  4. CodeLens, small gray text above functions (3 references), shows reference counts in languages that support it; it is controlled by editor.codeLens.
  5. Find in Files helps for strings and configuration that the language service does not link.

Safe refactoring workflow

flowchart LR
  A[Commit or stash] --> B[Check references]
  B --> C[Refactor with F2 or Ctrl+.]
  C --> D[Review the diff]
  D --> E[Run tests]
  E --> F{Pass?}
  F -- Yes --> G[Commit]
  F -- No --> H[Fix or discard]

Small, reviewed steps make refactoring safe; Git is the safety net.

  1. Make sure the tests pass and the working tree is committed or stashed.
  2. Do one kind of change at a time: rename, then extract, then move.
  3. Look at the diff in Source Control after each step.
  4. Run the tests again; commit.

A stand-in: rename by scope, not by text

Why does Rename Symbol work better than text replacement? Because it knows scope. This JavaScript stand-in has a toy symbol table: each variable is recorded with its function, and renaming touches only the chosen scope. It is a model of the idea, not the real language service.

JS Stand-in: scope-aware rename

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Notice how text replacement changed counter to nter, rewrote the string and touched the other function, while the scope-aware rename changed only the variable in total().

Language-specific tips

  • JavaScript/TypeScript: very strong built-in support; also try Organize Imports (Shift+Alt+O) and Remove Unused Imports.
  • Python: the Python extension with Pylance supports rename, extract variable and extract method, and Sort Imports through Ruff or isort.
  • Java and C#: language extensions (Extension Pack for Java, C# Dev Kit) provide much of an IDE's refactoring set, including move, change signature and generate constructors.
  • Dynamic code: renames cannot follow names built from strings (obj["get" + name]), so search for those separately.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which key renames the symbol under the cursor across files?

    F2 runs Rename Symbol; F12 is Go to Definition.
  2. 2. Why is Rename Symbol safer than Find and Replace?

    The language service knows which references belong to the same symbol, ignoring strings, comments and unrelated variables.
  3. 3. What does Extract to function do?

    It creates a new function from the selection and calls it where the code used to be.
  4. 4. What is a good first step before a big refactoring?

    A clean state with passing tests lets you review the diff and revert if needed.

Summary

  • F2 renames a symbol everywhere, using scope information from the language service.
  • Ctrl+. and Ctrl+Shift+R offer extract, inline, move and conversion refactorings.
  • Check references (Shift+F12) and call hierarchy before risky changes.
  • Rename or move a file and let VS Code update imports.
  • Refactor in small steps with Git and tests as a safety net.

Next: Debugging Basics: Breakpoints and Stepping