I once knew a programmer who had us all fooled into thinking he was the most organized programmer on the planet. When he left at night, his desk looked like he had never even been to work. When we did code reviews, his code was nicely formatted, there wasn’t a duplicate line of code anywhere.
Aside from the look of his code, he knew his stuff too. And this is back in the day when code editors were only slightly better than notepad… at least compared to today’s tools.
When he left and we started maintaining code he had written we found that only the code he had to show us was pretty. The rest of his code looked like all of our code.
But today, the amount of time it takes to keep your code looking nice is trivial thanks to some macros that Microsoft has built into Visual Studio. One of those macros is the refactoring macro called, “Extract Method.”
What’s it do?
Well, it helps us take care of that duplicate code issue. We’ve all done it as programmers–written code in three or four different places only to realize that we should have written it once as a method and then called it from all the other locations. It turns out that as I prepared for this post, I had no trouble finding a sample of code I needed to refactor in this way.
The beauty of the macro is that all you have to do is block a section of code, right click the block of code and select “Refactor” > “Extract Method…” from the context menu and the wizard walks you through the rest.
When you are done, you have a new method with the block of code in it, a call to the function where the block of code use to be, and any parameters that need to be passed have been discovered, declared in the method signature, and passed into the method from the calling method.
It’s a thing of beauty.
her places talking about Method Extraction:
- Extract Method – Tip of week #11 « The Pathfinder< - Refactoring is very essential part of coding. You need to refactor your code, to give optimal result. Visual Studio have an inbuilt option for refactoring your code. Right click – Refactor, today I am going to talk about Extract Method. …
- Tip of the day #4 – Refactoring – Extract method – I have worked with application migrations and the Extract Method feature is my second favorite, after the Rename of course. It is really easy to use and after few steps your new method will be ready: 1. Select the [...]
- New feature: Extract method – Siegfried Pammer – Welcome to the SharpDevelop Community Site! Ask your questions in the forum, hear from the team members in the blogs, read articles on SharpDevelop.
- C# IDE Tips & Tricks Part 2 – anonymous. Extract Method you can select the code and click on extract method in smart window and shows the preview as shown in the window. ExtractMethod. Here I am summarizing what we have learnt in this post. review.
- Extract Method – The Most Important Refactoring EVER – It is so fundamental that many developers execute this pattern as a matter-of-course, but did not know the name for it; well, now you do – it is called Extract Method. IMO, mastering this refactoring is the key to understanding the …