Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
Writing code is a constant push and pull on your development skills. First, you write some code and test it to see if it works the way you intended. Then as the project progresses, you think about ways to improve and change the code. Some people call this rewriting refactoring. Regardless of how you define it, Visual Studio has refactoring tools to help. If you are a C# developer, then you don't have to do anything special, as there are about 10 refactoring tools built into Visual Studio. If you are a Visual Basic programmer, you should download the free CodeRush Xpress tool, which provides dramatic code refactoring support.
I'm inside Visual Studio, and I've opened the RefactorYourCode Solution, which contains a C# and a Visual Basic project. I will start my refactoring journey by going to Program.cs. A common refactoring is when you need to rename something. I am going to rename this variable. So I will put my cursor on the variable, and I will right-click and choose Refactor. Then I will choose Rename. Also note the keystrokes Ctrl+R, Ctrl+R. Now during this refactoring, a dialog box will appear.
I can type in my new variable name, and then I can choose to preview the changes inside the dialog. I can also choose where to search for this word. I am going to click OK. Here's my preview screen. It shows that it's going to change line 13. It's a little bit harder to see, but it shows it's also going to change line 18. These changes look good to me, so I am going to click the Apply button, and I successfully changed those two lines of code. Next, I want to save the method name. So I'll click on the save book, which I am using twice. I am using it on line 21 and on line 18.
I am going to click here and use the keystrokes Ctrl+R, Ctrl+R. Make a slight change. Call this SaveTheBook. Again, choose to preview reference changes, and it shows in my preview screen several places where this code is going to be updated. I can click through these different elements up here to look. I'll click on Apply. And if I look in the Book.cs file, I also see that it changed it successfully over here. Next, I want to go to the MathLib file and show you the Extract Method Refactor.
I look at this code, and I'm thinking, "I want to reuse this in my application. It shouldn't live in entirely inside this CalculatePaintNeeded Method." So what I can do is come over here, choose Refactor > Extract Method. I'll get the dialog box again, and I think the new name should be called CalcArea, and as you can see Visual Studio wrote a new function for me, copied my code down there to line 24, and then changed my code on line 17 to call that method and pass in the necessary information that's needed for this function.
Next, I am going to switch back to the Book and show you the reorder parameter function. I have three parameters here. We decided on our team that this first string variable belongs at the end. So what I do is I put my cursor on SaveTheBook, choose the Refactor menu, and then choose this one, Reorder Parameters. Another dialog crops up. It shows the three parameters. I am going to pick this first parameter and then the down arrow, click on it twice to move it to the end of the parameter list. I feel good about not previewing my changes, so I am going to click on OK, and now you see the parameter has moved to the third position.
Now this a bit tricky, because I'm calling this function from a couple of areas in my code--calling it over here in Program.cs--so let's verify that it changed correctly over there. Yes it did, look on line 18. File path is now the last argument, and this string down here is also the last argument. My last refactoring for C# is going to be Extract Interface. That's a common refactoring pattern. I am going to go to this Book class, and I want to take these two properties, title and price, and I want to pull them out of the class and put them in a common interface. So I will choose the Book class, invoke the Refactor menu, choose Extract Interface, and then look at this interface name and see if it meets my requirements.
I like IBook. Next, I come down, and I choose the two properties. I want to leave the method inside the class and then click OK. Visual Studio creates a brand-new file over here--IBook.cs--extracts my interface and then correctly fixes up my code in the Book class to implement that interface. If you are a Visual Basic programmer, you don't have refactoring in Visual Studio by default. The good news is if you like what you see in Express, it's also available for C# programmers. If you plan on following along with me in this demo, you need to install the Express tools.
I've included those in the Installers folder inside your Exercise Files folder. So you go to Installers and then run this Express Installer. When I ran Visual Studio the next time after installing DevExpress, it asked me how you would like to run DevExpress, and I told it to let me load it manually when I start up the application. So I am going to go to this menu, DevExpress, and pick Load. And immediately you will see some changes in my C# code. It's now drawing a red line between my curly braces and other colored lines between like my if statements here.
It will also do the same thing over in my vb code. There are more refactoring tools in Visual Studio. I have only shown a few of them in this movie. If you like Visual Basic, then check out the VB refactoring title in this chapter.
Get unlimited access to all courses for just $25/month.Become a member
82 Video lessons · 101856 Viewers
61 Video lessons · 88609 Viewers
71 Video lessons · 72422 Viewers
56 Video lessons · 104123 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.
Your file was successfully uploaded.