Refactoring 007 - The Refactor That Reveals Missing Concepts

Written by mcsee | Published 2026/03/23
Tech Story Tags: programming | technology | clean-code | software-development | software-engineering | refactoring-007 | code-smells | code-duplication

TLDRPut together what belongs together.via the TL;DR App

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

TL;DR: Put together what belongs together

Problems Addressed πŸ˜”

  • Code Duplication
  • Missing Abstraction
  • Low Cohesion

https://hackernoon.com/how-to-find-the-stinky-parts-of-your-code-part-xxv

https://hackernoon.com/how-to-find-the-stinky-parts-of-your-code-part-xxix

https://hackernoon.com/how-to-find-the-stinky-parts-of-your-code-part-xxx

Context πŸ’¬

When a class starts doing work that should be delegated to others, it suffers from Low Cohesion.

You often see this through "Data Clumps"β€”groups of variables that always travel togetherβ€”or methods that seem unrelated to the class's primary responsibility.

This bloat makes the code harder to understand and creates a "God Object" that changes for too many different reasons.

You need to extract these related behaviors and properties into a new, dedicated class, you give a name to a previously hidden concept.

This not only promotes code reuse across the system but also ensures that each class has a single, well-defined reason to change.

You are not just moving code; you are discovering the missing abstractions in your domain model.

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 behavior
      public String telephoneNumber() {
          return ("(" + homeAreaCode + ") " + homeNumber);
      }
      String areaCode() {
          return homeAreaCode;
      }
      String officeNumber() {
          return officeNumber;
      } 
 }

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();
      }

  }

Type πŸ“

[X] Automatic

Most IDEs implement this safe refactor.

Safety πŸ›‘οΈ

This is a safe refactoring.

Why is the Code Better? ✨

Logic code is in just one place together with its rules.

How Does it Improve the Bijection? πŸ—ΊοΈ

In many software models, you often leave concepts homeless, scattering their behavior and data across unrelated classes.

This creates a gap between the Real World (where a concept clearly exists) and theΒ MAPPERΒ (where it is missing).

When you extract a class, you reify a concept.

You give a name and a physical place to an idea that was previously hidden in the implementation details.

This direct 1:1Β BijectionΒ between a real-world entity and a software object reduces cognitive load.

You no longer need to mental map data clumps back to their original meaning.

You are not just organizing code; you are making the software more "real."

Tags 🏷️

  • Hierarchies

Level πŸ”‹

[X] Intermediate

https://hackernoon.com/improving-the-code-one-line-at-a-time

https://hackernoon.com/refactoring-019-how-to-reify-email-addresses

https://hackernoon.com/refactoring-013-eliminating-repeated-code-with-dry-principles

https://hackernoon.com/heres-why-you-should-replace-inheritance-with-delegation

https://hackernoon.com/refactoring-018-how-to-replace-a-singleton

https://hackernoon.com/refactoring-020-transform-static-functions

Refactor with AI πŸ€–

Suggested Prompt: 1. Identify classes with low cohesion and unrelated methods.2. Extract related behaviors and properties into new classes.3. Refactor the original class to use the new extracted classes

Without Proper Instructions πŸ“΅

With Specific Instructions πŸ‘©β€πŸ«

See also πŸ“š

https://refactoring.com/catalog/extractClass.html?embedable=true

https://refactoring.guru/extract-class?embedable=true

Credits πŸ™

Image fromΒ drpepperscott230Β onΒ Pixabay


This article is part of the Refactoring Series

https://maximilianocontieri.com/how-to-improve-your-code-with-easy-refactorings


Written by mcsee | I’m a sr software engineer specialized in Clean Code, Design and TDD Book "Clean Code Cookbook" 500+ articles written
Published by HackerNoon on 2026/03/23