Refactoring 007 — Extract Class

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

Maximiliano Contieri
Javarevisited
2 min readJul 4, 2022

--

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

After

Type

[X] Automatic

Most IDEs like IntelliJIDEA and Eclipse 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

Refactoring courses

Credits

Image from drpepperscott230 on Pixabay

This article is part of the Refactoring Series.

--

--

Maximiliano Contieri
Javarevisited

I’m a senior software engineer specialized in declarative designs. S.O.L.I.D. and agile methodologies fan. Maximilianocontieri.com