# Refactoring 007 - Extract Class

> TL;DR: Put together what belongs together 

# Problems Addressed

- Code Duplication

- Missing Abstraction

- Low Cohesion

# Related Code Smells

%[https://maximilianocontieri.com/code-smell-124-divergent-change]

%[https://maximilianocontieri.com/code-smell-143-data-clumps]

%[https://maximilianocontieri.com/code-smell-147-too-many-methods]

# Steps

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

2. Use the new concept

# Sample Code

## Before

[Gist Url]: # (https://gist.github.com/mcsee/04dfcde00d2d40c8741f9af2fbeba469)
```java
 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;
      } 
 }
 
```

## After

[Gist Url]: # (https://gist.github.com/mcsee/3038811d4e9e821908b54092ad8efaee)
```java
// 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();
      }
     
  }


```

# 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](https://refactoring.com/catalog/extractClass.html)

[Refactoring Guru](https://refactoring.guru/extract-class)

# Credits

Image from <a href="https://pixabay.com/es/users/drpepperscott230-1212529/">drpepperscott230</a> on <a href="https://pixabay.com/es/">Pixabay</a>

* * * 

This article is part of the Refactoring Series.
