Code Smell 114 — Empty Class

Have you encountered classes without behavior? Classes are their behavior.

Maximiliano Contieri
2 min readFeb 12, 2022
Photo by Kelly Sikkema on Unsplash

TL;DR: Remove all empty classes.

Problems

Solutions

1. Remove the classes and replace them with objects instead.

2. If your classes are Anemic Exceptions, remove them.

Context

Many developers still think classes are data repositories.

They couple different behavior concept with returning different data.

Sample Code

Wrong

Right

Detection

[X] Automatic

Several linters warn us of empty classes.

We can also make our own scripts using metaprogramming.

Tags

  • Behavior

Conclusion

Classes are what they do, their behavior.

Empty classes do nothing.

Relations

More Info

An error arises from treating object variables (instance variables) as if they were data attributes and then creating your hierarchy based on shared attributes. Always create hierarchies based on shared behaviors, side.

David West

--

--

Maximiliano Contieri

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