Refactoring4:15 with Dean Davidson
James A. Michener once said “I'm not a very good writer, but I'm an excellent rewriter”. Learn how to get Visual Studio to rewrite your code for you using powerful refactoring tools.
Refactoring your code involves changing the way it's written and 0:01 organized without changing the functionality. 0:03 We refactor our code to increase readability and reduce complexity. 0:07 This ultimately makes your projects easier to maintain and saves time and money. 0:11 Your results may vary but refactoring always puts a smile on my face because 0:17 I love taking something that was previously complicated And 0:21 making it easy to understand. 0:24 Let's simplify some code. 0:26 I'm gonna start with my personal favorite refactoring tool, extract method. 0:28 In my ShippingHelper class I have a method called calculateShippingCost. 0:33 I did an okay job of leaving comments as to why these things are affecting 0:37 the shipping costs. 0:41 But it still is hard to quickly understand what this method does because it has so 0:42 many lines. 0:45 We can break some of this logic out using extract method. 0:46 First off, I'm going to select my logic for the multi-item surcharge and 0:50 press control period to bring up the quick actions in refactoring menu. 0:54 Visual Studio's already detected that extract method is the only viable action. 0:58 I'll press enter to extract method and give my new method a name. 1:02 I call my new method CalculateMultiItemSurcharge and 1:06 press Enter. 1:10 Note that Visual Studio was able to determine that I am modifying my cost 1:12 variable. 1:15 And that the new method needs access to the cost of variable as well as number of 1:16 items, distance and cost. 1:20 Next let's do the same thing with our heavy item surcharge. 1:23 And finally let's do the same with our oversized box surcharge. 1:40 We are left with many small methods that perform a single task instead of a single 1:57 method that performs many tasks. 2:01 This makes our code easier to read and easier to modify. 2:04 You know, now that I'm looking at it CalculateWeightSurcharge isn't 2:08 actually the best name for that method. 2:11 It really applies only to heavy items and 2:14 I want to rename it to CalculateHeavyItemsSurcharge, but 2:16 I'm afraid that doing so might break other methods that reference it. 2:20 Luckily for me Visual Studio knows how to check references for you and 2:23 rename them automatically. 2:27 I'll change the name of the method to CalculateHeavyItemSurcharge and 2:29 press control period. 2:33 Visual Studio shows me a diff, so I know what other lines of code I'm changing and 2:40 I press Enter to accept the change. 2:45 Rename works for renaming fields, methods, variables, properties, 2:48 name spaces, and types. 2:52 Now that I've had some time to think things over, 2:55 I want item size to appear my method signature before a number of items. 2:56 But again changing this might cause other methods that rely on this method to throw 3:00 exceptions. 3:04 Or worse, since both properties are the same type, methods that call this method 3:05 might pass in an item size as number of items and vice versa. 3:09 We can have Visual Studio modify all references by highlighting the method 3:14 pressing control period and 3:18 pressing Enter to select Change Signature then reordering our parameters as desired. 3:19 I just noticed that I have some using statements that I don't need. 3:28 I can automatically remove them by pressing Ctrl+EQ or 3:32 right clicking on my CS file and going to Organize Usings, Remove and Sort Usings. 3:35 This is important because it keeps your class files clean, but it also improves 3:45 performance on IntelliSense and compile time because of Visual Studio and 3:49 MSBuild respectively don't have to search as many name spaces. 3:53 Plus you look cool and 3:57 impress your friends with your minimalist using directives. 3:58 By using Visual Studio's refactoring tools, we clean this class up and 4:02 made it easier to understand. 4:05 It's hard for me to overstate the importance of refactoring. 4:07 You should refactor your code often and rely on Visual Studio to help you do it. 4:10
You need to sign up for Treehouse in order to download course files.Sign up