top of page

Visual Studio Built-In Refactoring Tool (Quick Actions)



Sometime in the last few iterations (years, probably .. I find things very late in the game) of the Visual Studio IDE, Microsoft introduced a refactoring tool. As much as I love searching StackOverflow for hours to find the answer to the super obscure coding issue I'm having, this refactoring tool saves me sooooooooo much time and irritation!!


Whomever built this deserves the Nobel Peace Prize for helping developers refrain from killing people while raging on StackOverflow .

Seriously, I really think many developers out there have legitimately calmed down because of this <3




Anyways ...... to the tool!


Let's say we wanted to build a Person model and add a few people to a list of Person. Let's say we then wanted to add this Person list to another list. For practical purposes, let's assume the original Person list is a readonly list coming from your backend Business Layer of data. And you need to fill the new list with only some of the Person data. This is a situation I've had many times IRL.


Your Person Class:















Your code to get the 'backend list'. Remember, we're assuming this is a readonly list coming from your data layer. Pretend we aren't hardcoding it ....

Now that we have our data list in personList, we need to get this list into another list. The first way we think to do this is with a foreach loop like this:


var newList = new List<Person>();

            foreach (var dude in personList)
            {
                newList.Add(dude);
            } 

Right? Slick, eh? Well .... not really. A loop is better performance wise, so if you have thousands of records in personList, then maybe this is where you should stop.


However, if using LINQ won't bust the bank on your efficiency, click on or highlight the 'foreach' text in the IDE. You'll notice either a yellow lightbulb or a blue like maker or paintbrush thingy in the far left margin. The technical term for these are Quick Actions.

Now, hover over the lightbulb / marker and you'll get a dropdown with some options .....


You will see a few different options for LINQ and usually a few other options, like a for loop instead of a foreach loop, if that trips your trigger.




If you choose the first LINQ option, it gives you the original 'select where' SQL query language. The second option uses the .where or .select option.


Cool, right? And this refactor tool will help with some pretty complex setups too, nested loops, ifs, etc. During the many years of my career, I have spent hours upon hours on Google and StackOverflow, trying to figure out that one thing I'm missing in the line to get it to work. I love you, S.O. but I do enjoy spending less time with you.


The Quick Actions tool has other features too, helps you to correct errors (this has been in place forever so I'm sure you've used that part), helps you find references, conversions, efficiency gains, all sorts of helpful guidance. This is just 1 of the many built in helpers VS offers. To find more hidden gems, go to https://docs.microsoft.com/en-us/visualstudio/ide/code-generation-in-visual-studio?view=vs-2019 to find more helpers.

24 views0 comments

Commentaires


bottom of page