---
schema_version: "1"
id: 4a0431ccd83a528ff86813d57e5986e7feeb419f7ac03273c5b1d8ff02478b1a
title: Refactor Apex Code
collection: developer
version: current
locale: en-us
portal_url: https://docs.salesforce.com/developer/current/en-us/salesforcedx/webconsole/guides/work-with-code/webconsole-apex-refactor.md
canonical_url: https://developer.salesforce.com/docs/platform/webconsole/guide/webconsole-apex-refactor.html
source_site: developer.salesforce.com
content_type: documentation
content_hash: 811c9844412a23ec385cb41002f5a2482d6de5b3739ba30519887982ec1b5480
products:
  - Web Console
  - salesforcedx
---
# Refactor Apex Code

The Apex extension for VS Code has several useful tools for refactoring your Apex code.

## Refactor: Rename

You can rename any valid Apex symbol defined in your source: methods, locals, fields, properties, constructors, or types (classes, triggers, or enums). To perform a rename, right-click the symbol that you want to rename and select **Rename Symbol**.

![GIF showing the symbol renaming process](https://developer.salesforce.com/docs/platform/media/shared/apex-rename-demo.gif '{"class": "image-lg"}')

Validation of the new symbol name is performed before applying the rename. If the validation fails, an error message explains the reason why the rename refactoring couldn’t be applied. Validation fails if the new name isn’t a valid Apex identifier or, sometimes, if the new name conflicts with an existing identifier name. (If these situations were allowed, a compiler error or a runtime behavior change could result.)

![GIF showing a renaming error](https://developer.salesforce.com/docs/platform/media/shared/apex_rename_error.gif '{"class": "image-lg"}')

If the new name conflicts with an existing identifier name, we try to fully qualify the references to the existing name in contexts where the conflicts exist.

![GIF showing a renaming conflict](https://developer.salesforce.com/docs/platform/media/shared/apex-rename-conflict.gif '{"class": "image-md"}')

## Refactor: Extract Constant

You can extract literals into constants. Literals include: String, Integer, Long, Double, Decimal, and Boolean.

1. In the editor, select an expression that you want to extract.
2. Click the lightbulb in the gutter and select 'Extract Constant'.
3. The new constant is declared as a field in the containing class from where the refactoring was invoked.
4. The selected expression should now be replaced with the name of the new field.

![GIF showing extract constant](https://developer.salesforce.com/docs/platform/media/shared/extract-constant.gif '{"class": "image-md"}')

## Refactor: Extract Local Variable

You can extract expressions into local variables.

1. In the editor, select an expression that you want to extract.
2. Click the lightbulb in the gutter and select 'Extract Variable'.
3. The new local variable is declared in the line above the declaration where the refactoring was invoked.
4. The selected expression should now be replaced with the name of the new local variable.

![GIF showing extract local variable other](https://developer.salesforce.com/docs/platform/media/vscode/extract-local-variable-other.gif '{"class": "image-md"}')

