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 […]
  • Readable CodeReadable Code Time out today for just a bit of a rant. A couple of days ago, I posted a top ten list for why you shouldn't buy Visual Studio 2008.  Most people totally got that this was all tongue in […]
  • 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 […]

About Dave Bush

Dave Bush is a .NET programmer and Certified ScrumMaster who is passionate about managing risk as it relates to developing software. When he is not writing or speaking about topics related to Application Lifecycle Risk Management (ALRM), he is an example to his peers as he develops web sites in the ASP.NET environment using industry best practices.

One Response to Extracting Methods

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Awards & Certs