Visual Studio Shortcut: Rename Refactoring (Ctrl+R, R)

1 minute read

Although I’ve been doing a lot of refactoring with ReSharper lately as I mentioned a few weeks ago, the built-in Visual Studio behaviour is still pretty good.

If you don’t know what I’m talking about then I may be about to make your life easier and it might have been something you shied away before because you thought it was hard to do.

Before you do any refactoring you might want ask yourself a few of these questions? I’d recommend a combination of the following steps:

*When did you last check your code into source control?

*Should you create a new branch for the refactoring? Maybe overkill, but good practice.

*Is the code you’re changing cover by unit tests? If you make a change it’s good to know it hasn’t broken anything that was working.

With that in mind let’s get on with it.

Suppose a previous developer decided that using i for a variable name was helpful (HINT: it’s not) and you want to rename it something more meaningful so the code is readable. Place the cursor on the variable and press Ctrl + R, R , then you’ll get this nice popup asking you to enter a new name: You’ll see you have the option to search in comments and search in strings which may be useful. I find search in strings often caused more problems so be careful when you are doing this one. By it’s nature if you’re using strings then the compiler probably won’t warn you when you broke something. In this case some well placed unit tests will help ensure your refactoring doesn’t break anything.

But as you can see you can Preview the changes before you make them on the next screen.

It’s worth noting you’re not doing Find and Replace, it won’t change every i to number just those referenced by the symbol you’re renaming. Press enter or click apply and it’s done. This is a simple example but the process is the same for more far reaching refactorings. As well as local variables you can rename Fields, Namespaces, Types, Properties and Methods.

Happy Renaming!

Leave a Comment