My Second Favorite Refactoring

In my last blog post, I shared with you my favorite refactoring and it might not have been what you were expecting. I just can’t leave the topic without sharing with you my second favorite refactoring, as well.

Like my favorite refactoring, my second favorite refactoring is also a safe refactoring, which means that if you have an automated refactoring tool that supports this refactoring then you can apply it to any legacy code, even when it’s not under test, although you might have to do a bit more work than renaming a method.

Also, this particular refactoring I feel is one of the most important for adding clarity to the domain model and making code much more understandable. Have I piqued your interest?

Well, my second favorite refactoring is Extract Method.

Extract Method is an automated refactoring that allows you to highlight a block of code and extracted into its own method. In the old days of programming when I was learning to become a developer 30 years ago, this was considered to be a poor practice. Back then, the reason that we created a method was so that we could call it from multiple places and eliminate redundancy. Having a method that you only call from one place seemed wasteful because it meant that you were making an indirect call rather than just putting that behavior inline.

My how times have changed. Today we see that inlining code is a poor practice because it can make control flow more difficult to see and it violates the Single Level of Abstraction Principle (SLAP) by having code call out both delegation and implementation details.

Today, I mostly extract methods, not because I intend to reuse them but rather because it gives me the opportunity to give bits of functionality a meaningful name. If you can name a behavior then it makes sense to wrap that behavior in a method with that name. This makes code expressive so it is more readable and understandable.

Wrapping a block of code in a private method with an intention revealing name is preferable to using a block comment around code. Instead of tracing through code we can start to get a sense of the different layers of abstraction that we’re dealing with and if our models are consistent and our entities cohesive then we can more easily identify defects based upon the responsibility where it occurs.

Long methods are the number one hiding place not only for smaller component methods but also for entire classes. When we fail to call out classes, we tend to distort our domain model, making it far more difficult to understand what we’re actually doing. As we start extracting out behaviors and identifying the entities that they belong to then we begin to refine our object model so that what we’re doing becomes clearer.

Great article. I agree that extraction helps to create the domain language.

Like
Reply

To view or add a comment, sign in

More articles by David Scott Bernstein

  • Green Tests and Red Tests

    In practice, I found that there are times that I want a bit more test coverage than I get from just doing test-first…

  • Radiators and Silos

    In the old days of corporate America, the way you got ahead was through hard work and perseverance. You strove to…

    2 Comments
  • Makers and Menders

    I’ve been getting back into some research interests of mine that require data acquisition from a variety of sensors so…

  • Core Developer Practices

    Every field of engineering has a core set of practices that they follow and software engineering is no different. But…

    1 Comment
  • Still XP After All These Years

    Are you humming in your head Paul Simon’s “Still Crazy After All These Years”? I am. And it does seem crazy.

  • The Importance of Continuous Integration

    Perhaps the most important yet easiest to implement of all the software development practices in Agile is continuous…

  • The Importance of Technical Practices (Again)

    Software development has undergone many revolutions over the last few decades. The way we build software today is…

  • Summary of Seven Strategies Series

    I finished my “Seven Strategies” series of 72 blog posts with seven strategies for implementing each of the nine…

    1 Comment
  • Refactor to Learn What Not to Do

    One of the things that I was not expecting when I started refactoring other people’s code was that I started to see…

    4 Comments
  • Refactor to Clean Up Before Moving On

    Of course, the best time to refactor code is while it’s fresh in your mind, right after having worked with it. Once I…

Others also viewed

Explore content categories