DEV Community

Cover image for Refactoring 007 - Extract Class
Maxi Contieri
Maxi Contieri

Posted on • Originally published at maximilianocontieri.com

6 1

Refactoring 007 - Extract Class

Behavior is repeated across the system. But we are missing a concept

TL;DR: Put together what belongs together

Problems Addressed

  • Code Duplication

  • Missing Abstraction

  • Low Cohesion

Related Code Smells

Steps

  1. Extract the methods (and accidentally the properties) coupled into a new concept

  2. Use the new concept

Sample Code

Before

 final class Person {

      private String name;

      // Below cohesive properties
      private String homeAreaCode;
      private String homeNumber;

      public String name() {
          return name;
      }

      // Below cohesive behaviour
      public String telephoneNumber() {
          return ("(" + homeAreaCode + ") " + homeNumber);
      }
      String areaCode() {
          return homeAreaCode;
      }
      String officeNumber() {
          return officeNumber;
      } 
 }

Enter fullscreen mode Exit fullscreen mode

After

// 1. Extract the methods (and accidentally the properties) coupled into a new concept      
   public class TelephoneNumber {

      private String number;
      private String areaCode;

      public String telephoneNumber() {
          return ("(" + areaCode + ") " + _number);
      }
      public String areaCode() {
          return areaCode;
      }
      public String number() {
          return number;
      }
   }

final class Person {

      private String name;

      // 2. Use the new concept
      private TelephoneNumber officeTelephone = new TelephoneNumber();

      public String name() {
          return name;
      }
      public String telephoneNumber(){
          return officeTelephone.getTelephoneNumber();
      }

  }


Enter fullscreen mode Exit fullscreen mode

Type

[X] Automatic

Most IDEs implement this safe refactor.

Why code is better?

Logic code is in just one place together with its rules

Tags

  • Classes

See also

Refactoring.com

Refactoring Guru

Credits

Image from drpepperscott230 on Pixabay


This article is part of the Refactoring Series

Image of Timescale

Timescale – the developer's data platform for modern apps, built on PostgreSQL

Timescale Cloud is PostgreSQL optimized for speed, scale, and performance. Over 3 million IoT, AI, crypto, and dev tool apps are powered by Timescale. Try it free today! No credit card required.

Try free

Top comments (0)

Image of Docusign

🛠️ Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more

👋 Kindness is contagious

Explore a sea of insights with this enlightening post, highly esteemed within the nurturing DEV Community. Coders of all stripes are invited to participate and contribute to our shared knowledge.

Expressing gratitude with a simple "thank you" can make a big impact. Leave your thanks in the comments!

On DEV, exchanging ideas smooths our way and strengthens our community bonds. Found this useful? A quick note of thanks to the author can mean a lot.

Okay