Visual Studio Refactoring Tips
5 Visual Studio Refactoring Tips ๐ก
Either for the sake of optimization or better code readability, we often refactor the code. Code refactoring includes many actions, such as deleting unnecessary code, encapsulating certain functionalities, changing the names of variables, and functions for better understanding, and so on.
If we use ๐๐ข๐ฌ๐ฎ๐๐ฅ ๐๐ญ๐ฎ๐๐ข๐จ as an ๐๐๐, it can help us a lot to quickly and successfully refactor the code.
Visual Studio makes things easy for us and has support for a lot of code changes. Today I am presenting 5 shortcuts in Visual Studio that I use all the time.
โข ๐๐๐ง๐๐ฆ๐ข๐ง๐ ๐ข๐๐๐ง๐ญ๐ข๐๐ข๐๐ซ
When we want to change the name of a variable in a file/function, it often happens that the variable is used in several places.
Use ๐๐ญ๐ซ๐ฅ+๐, ๐ so that when you change the name of a variable in one place, all references are automatically changed.
โข ๐๐ฑ๐ญ๐ซ๐๐๐ญ ๐๐๐ญ๐ก๐จ๐
We often want to increase code readability by extracting a specific piece of code in one method into another method.
The hotkey ๐๐ญ๐ซ๐ฅ+. triggers the ๐๐ฎ๐ข๐๐ค ๐๐๐ญ๐ข๐จ๐ง๐ฌ ๐๐ง๐ ๐๐๐๐๐๐ญ๐จ๐ซ๐ข๐ง๐ ๐ฌ menu. From this menu, we can choose ๐๐ฑ๐ญ๐ซ๐๐๐ญ ๐๐๐ญ๐ก๐จ๐ or ๐๐ฑ๐ญ๐ซ๐๐๐ญ ๐๐จ๐๐๐ฅ ๐
๐ฎ๐ง๐๐ญ๐ข๐จ๐ง ๐๐ฑ๐ญ๐ซ๐๐๐ญ ๐๐จ๐๐๐ฅ ๐
๐ฎ๐ง๐๐ญ๐ข๐จ๐ง.
โข ๐๐๐ฆ๐จ๐ฏ๐ ๐๐ฌ๐ข๐ง๐ ๐ฌ
When we see a "grey" using statement at the beginning of the file, it means that the namespace is not used anywhere in the file - it is natural to delete it.
Using the ๐๐๐ฆ๐จ๐ฏ๐ ๐๐ง๐ง๐๐๐๐ฌ๐ฌ๐๐ซ๐ฒ ๐๐ฌ๐ข๐ง๐ ๐ฌ ๐จ๐ฉ๐ญ๐ข๐จ๐ง ๐๐ซ๐จ๐ฆ ๐๐๐๐ +. ๐ฆ๐๐ง๐ฎ, we delete all "using" statements that are not used.
โข ๐๐ฑ๐ญ๐ซ๐๐๐ญ ๐๐ง๐ญ๐๐ซ๐๐๐๐
If we have a class, and we want to extract the Interface from that class (e.g., due to Dependency Injection), we can do that with the help of the shortcut ๐๐๐๐ + ๐, ๐.
We can also select all the methods that interest us from the opened window.
โข ๐ ๐จ๐ซ๐๐๐๐ก ๐ญ๐จ ๐๐๐๐
From CTRL+. the menu above the foreach loop, we will generally get a suggestion to ๐๐จ๐ง๐ฏ๐๐ซ๐ญ ๐ญ๐ก๐ ๐๐จ๐๐ ๐ญ๐จ ๐๐๐๐ to increase the readability of the code.
*Notice that typically loops are faster than LINQ queries because the compiler can optimize loops while LINQ queries extensively rely on method calls.
Subscribe to my newsletter
Read articles from Md.Jahirul Islam directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Md.Jahirul Islam
Md.Jahirul Islam
Hello, This is Jahirul Islam and I love programming. I'm proficient in C#, .Net Core, Angular, JavaScript, MySql, and Azure DevOps with 5 years of experience. I believe my strong points are my responsibility and punctuality. I have an incredible intention to detail and do not stop until the work is complete. To work in any organization where I will get the opportunity to utilize my knowledge & skills to attain the desired goals & at the same time develop a successful career. Able to hard-working and organize a heavy workload with competing deadlines. Excellent interpersonal communication skills.