# Code Smell 114 - Empty Class

> TL;DR: Remove all empty classes.

# Problems

- [Bijection](https://maximilianocontieri.com/the-one-and-only-software-design-principle) Fault

- Namespace [Polluting](https://maximilianocontieri.com/code-smell-26-exceptions-polluting)

- Classes used as [DTOs](https://maximilianocontieri.com/code-smell-40-dtos)

- Classes used as [global references](https://maximilianocontieri.com/code-smell-60-global-classes)

# Solutions

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

2. If your classes are Anemic Exceptions, [remove them](https://maximilianocontieri.com/refactoring-004-remove-unhandled-exceptions).

# Context

Many developers still think classes are [data repositories](https://maximilianocontieri.com/code-smell-01-anemic-models).

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://maximilianocontieri.com/laziness-i-meta-programming).

# Tags

- Behavior

# Conclusion

Classes are what they do, their behavior.

Empty classes do nothing.

# Relations

%[https://maximilianocontieri.com/code-smell-26-exceptions-polluting]

%[https://maximilianocontieri.com/code-smell-40-dtos]

%[https://maximilianocontieri.com/code-smell-60-global-classes]

%[https://maximilianocontieri.com/code-smell-01-anemic-models]

# More Info

- [The one and only Design Principle](https://maximilianocontieri.com/the-one-and-only-software-design-principle)

- [Refactoring 004 - Remove Unhandled Exceptions](https://maximilianocontieri.com/refactoring-004-remove-unhandled-exceptions)

# 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_
 
%[https://maximilianocontieri.com/software-engineering-great-quotes]

* * *

This article is part of the CodeSmell Series.

%[https://maximilianocontieri.com/how-to-find-the-stinky-parts-of-your-code]
