DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations

My Favorite Refactoring

A method by any other name is confusing. Read more about the importance of refactoring techniques in code, and this author's favorite one.

David Bernstein user avatar by
David Bernstein
CORE ·
Aug. 30, 18 · Opinion
Like (7)
Save
Tweet
Share
5.03K Views

Join the DZone community and get the full member experience.

Join For Free

Refactoring code is an essential activity when working with existing systems, as well as building new ones. Refactoring techniques allow us to safely transform code into designs that are perhaps better suited for future extension. The techniques used to refactor code range from very simple to quite complex and there is a strong emphasis to make small and safe changes to code so that we always have a buildable system.

Honestly, as I started to learn about refactoring and what it means I realized that the designs I come up with as I'm building a system don't really matter because in practice it's very straightforward to change the design as I'm building a system. After I've learned a series of techniques to help me do this, I found that is far more efficient to dive in and start building a system rather than trying to do a lot of upfront design.

What I'm doing upfront design I'm trying to visualize what the system will be like but I'm doing it in my head and on paper so it's easy to fool myself. It's also really difficult to see conceptually because we think in specifics, not conceptually. We think in terms of examples but we specify systems and talk about the design of systems as generalizations, so we're constantly going back and forth between the general and the specific. Traditional specifications don't give us a formal way of doing, this whereas emergent design and test-first development do because we code from examples when doing test-first development.

The best time to visualize a system is when we're building it out and I oftentimes do this through refactoring. Many IDE's such as Eclipse and Visual Studio provide automated refactoring tools and this can save me a lot of time. Most of the basic refactoring operations have been automated and so they're simple to use. There also mostly safe, which means that in many cases, we can use them on existing code even if that code is not under test.

My favorite refactoring is a safer factoring. It's one of the things that I love about it. I can use this for refactoring on the most intractable and difficult to understand code without the fear of breaking anything. Plus, it's my number one tool for understanding legacy code.

What is this amazing and magical refactoring? It's the Rename Method.

Having good, intention-revealing names for methods is our first line of defense in documentation and helping readers understand what our code does. As I'm writing a method, I get clearer and clearer on what the method does and as I'm doing this, I want to change the name of the method to reflect my new understanding. Rename Method lets me do this.

It's a relatively simple refactoring with only eight steps, if you do it manually. I almost always use an automated refactoring tool to rename my methods so it's simply a matter of highlighting the method you want to rename, selecting the Refactor -> Rename menu option, typing the new name and letting the tool do the rest.

I prefer long, intention-revealing method names stated in the active voice. I don't like abbreviations or acronyms. I like my names to be plain and simple. Methods should say what they do and that's all.

I use the Rename Method refactoring more than perhaps any other refactoring and I'll rename a method several times in the course of writing it as I get clearer and clearer on exactly what it does.

I also rename methods as I'm learning legacy code to give more clarity to the code. When I'm confronted with legacy code that I don't understand then one of the ways I use to learn it is to rename methods if I feel their intention is unclear. Oftentimes, just the act of renaming key methods can help me gain a much better understanding of what's going on in legacy code.

So, what's in a name? Everything! Names let us give things meaning so make sure their meaning is clear, and if they aren't, use the Rename refactoring.

IT

Published at DZone with permission of David Bernstein, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • GitLab vs Jenkins: Which Is the Best CI/CD Tool?
  • [DZone Survey] Share Your Expertise and Take our 2023 Web, Mobile, and Low-Code Apps Survey
  • How Elasticsearch Works
  • Monolithic First

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: