Extracting Methods

rhesus-monkey 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 …

Like this Article? Subscribe to get every article sent to your email.

Related Post

  • Advantages of Using Class DiagramAdvantages of Using Class Diagram One of the new tools that showed up in Visual Studio 2005 that I don't see many people taking much advantage of is the Class Diagram.The class diagram displays the classes you drag […]
  • Using StoredProcedures with LINQ 2 SQL ClassesUsing StoredProcedures with LINQ 2 SQL Classes While it is true that LINQ will allow you to write all of your data access in .NET without writing a line of SQL, many organizations have already determined that using stored procedures to […]
  • Computed Columns Using LINQ to SQL ClassesComputed Columns Using LINQ to SQL Classes Last week we looked at the extension points Microsoft has wired into the LINQ to SQL classes and how they can be used to achieve some of the capabilities of the Business Logic Layer (BLL) […]
  • ListView and the DataPagerListView and the DataPager One of the new controls that Visual Studio 2008 brings us is the ListView.  Now, the ListView control is really several different controls all rolled into one.  There is a lot to see with […]
  • Object Initialization in CSharp 3.0 and VB.NET 9Object Initialization in CSharp 3.0 and VB.NET 9 Yesterday we looked at the new var keyword in CSharp.  This makes CSharp variable declaration similar to VB.  After all, they've had the DIM keyword for years which essentially does the […]

About Dave Bush

Dave Bush is a Full Stack ASP.NET developer. His commitment to quality through test driven development, vast knowledge of C#, HTML, CSS and JavaScript as well as his ability to mentor younger programmers and his passion for Agile/Scrum as defined by the Agile Manifesto and the Scrum Alliance will certainly be an asset to your organization.

  • Utham

    Nice Post… Every uses IDE like me but only very few aware of this(not like me :-( )..