Visual Studio 2008 / Refactoring Code
Subtitles of the Movie
Now, there's a really cool piece of functionality that I want you to be aware of called Refactoring that's built into Visual Studio and it works really well with C#. It actually has more functionality on the C# environment than it does in the VB so those of you who are doing those C# VB battles, there's one for the C# side. Refactoring is the process of changing the names of functions and methods and so forth because in the real world, you've seen this before with your applications if you're a developer. The closer you get to the end of the development process, the more clearly you understand how you should have built this application. You realize, man, if I, you know, this should have been structured differently. This should have been named differently. I should be calling these, these parameters in different orders and so forth and so refactoring is going back and making those changes to code. It happens all the time. They've added some tools in Visual Studio to make this a lot easier and again in C# you get a lot more functionality than in the VB environment. So I want to show you some simple refactoring here. The way I'm going to demo it is I'm going to add my own function down here, so I'll put Private Void Mark Test and I'll do an integer X and a string, not string six, string Y. We will pass in and I'll, if I could talk and type at the same time it would help tremendously. And then I'll just come in here and do my MessageBox.Show and we'll do our You Clicked just like before. Put my semicolon on the end. Now, up here I'll just comment this line out if you're typing along with me and then we'll just call this Mark Test and we've got to pass in an integer six and Hello and a semicolon. Now, we can call this. We're not doing anything with it inside here and for now we won't worry about it. But you'll notice that if we run this and we click it, it's still getting You Clicked because it's jumping down in here, right and instead of running in the message box here, it's calling it here. Well, you know, it's calling it right here and running this and we're passing two parameters that we're not using down here. We're passing them in here but we're not using them. But I want you to see something here. What happens when I say you know what? Man, I shouldn't have named that Mark Test. Well, I can right click on it, go to Refactor and rename it. So I'll just rename it and instead of Mark Test I will put Click Check and notice I can search in comments, string, I can rename overloads but for now I'll just take the simple one and I will click OK to preview the changes. I'll apply them and notice it changed it for me in both places. How cool is that? Everywhere that this was being called, it would have changed it in my code. So it would have refactored the name on it. Now let me show you another example. I'm going to right click. I'm going to go back to Refactor and notice I can extract methods, I can encapsulate fields, extract interfaces, promote a local variable to a parameter, remove parameters and even reorder parameters. So I'm going to switch this around. I'm going to say you know what? I want to pass the string first and I'm going to say don't preview the changes and just say OK and notice it changed them here; string first, then integer and it also changed them here where I was calling it. So you can see, this is a very powerful set of tools for going back and making changes to your data. So anyway, that's Refactoring. It's a very simple example. Play with that, use it, you can do a lot of things when you're going back and making changes to your code and for a lot of people, it makes it possible to go refactor and change code because this can be such a nightmare to change and get it wrong and break your program, OK? So anyway, real simple look at Refactoring. Play with that some in C#. You'll be amazed at what you can get accomplished with that tool.
Tutorial Information
| Course: | Introduction to Microsoft C# 2008 |
| Author: | Mark Long |
| SKU: | 34046 |
| ISBN: | 1-935320-78-5 |
| Release Date: | 2009-10-09 |
| Duration: | 7 hrs / 76 lessons |
| Work Files: |
Yes |
| Captions: | Available on CD and Online University |
| Compatibility: |
Vista/XP/2000, OS X, Linux QuickTime 7, Flash 8 |
VTC Sign up & Benefits
- Unlimited Access
- 98,729 Video Tutorials (23,265 free)
- Video Available as Flash or QuickTime
- Over 1026 Courses
- $30 for One Month Access
- Multi-User Discounts Available
United States 