Code Smell 114 - Empty Class
Tags: oop, tutorial, cleancode, webdev
Published: 2022-02-12 15:39:25

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

> TL;DR: Remove all empty classes.

# Problems

- [Bijection](https://dev.to/mcsee/the-one-and-only-software-design-principle-3086) Fault

- Namespace [Polluting](https://dev.to/mcsee/code-smell-26-exceptions-polluting-pbn)

- Classes used as [DTOs](https://dev.to/mcsee/code-smell-40-dtos-4426)

- Classes used as [global references](https://dev.to/mcsee/code-smell-60-global-classes-3o9m)

# Solutions

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

2. If your classes are Anemic Exceptions, [remove them](https://dev.to/mcsee/refactoring-003-extract-constant-4j5p).

# Context

Many developers still think classes are [data repositories](https://dev.to/mcsee/code-smell-01-anemic-models-4dg2).

They couple *different behavior* concept with *returning different data*.

# Sample Code

## Wrong

[Gist Url]: # (https://gist.github.com/mcsee/729e6032d21c0c2997228680170ff768)
```javascript
class ShopItem { 
  code() { }
  description() { }                 
}

class BookItem extends ShopItem { 
   code() { return 'book' }
   description() { return 'some book'}     
}

// concrete Class has no real behavior, just return different 'data'
```

## Right

[Gist Url]: # (https://gist.github.com/mcsee/a8c680954291f8d9be4023ff8062b504)
```javascript
class ShopItem { 
  constructor(code, description){
    //validate code and description
    this._code = code;
    this._description = description;
  }
  code() { return this._code }
  description() { return this._description }                 
  //Add more functions to avoid anemic classes
  //getters are also code smells, so we need to iterate it
}

bookItem = new ShopItem('book', 'some book);
//create more items
```

# Detection

[X] Automatic 

Several linters warn us of empty classes. 

We can also make our own scripts using [metaprogramming](https://dev.to/mcsee/laziness-i-meta-programming-32a9).

# Tags

- Behavior

# Conclusion

Classes are what they do, their behavior.

Empty classes do nothing.

# Relations

{% post https://dev.to/mcsee/code-smell-26-exceptions-polluting-pbn %}

{% post https://dev.to/mcsee/code-smell-40-dtos-4426 %}

{% post https://dev.to/mcsee/code-smell-60-global-classes-3o9m %}

{% post https://dev.to/mcsee/code-smell-01-anemic-models-4dg2 %}

# More Info

- [The one and only Design Principle](https://dev.to/mcsee/the-one-and-only-software-design-principle-3086)

-[Refactoring 004 - Remove Unhandled Exceptions](https://dev.to/mcsee/refactoring-003-extract-constant-4j5p)

# Credits

Photo by <a href="https://unsplash.com/@kellysikkema">Kelly Sikkema</a> on <a href="https://unsplash.com/s/photos/empty">Unsplash</a>
  
* * *

> 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_
 
{% post https://dev.to/mcsee/software-engineering-great-quotes-26ci %}

* * *

This article is part of the CodeSmell Series.

{% post https://dev.to/mcsee/how-to-find-the-stinky-parts-of-your-code-1dbc %}