# Code Smell 13 - Empty Constructors

*No-Parameterized constructors are a code smell of an **invalid** object that will dangerously mutate.
Incomplete objects cause lots of issues.*

> TL;DR: Pass the essence to all your objects so they will not need to mutate.

# Problems

- Mutability

- Incomplete objects

- Concurrency inconsistencies between creation and essence setting.

- Setters

%[https://maximilianocontieri.com/nude-models-part-i-setters]

# Solutions

1. Pass the object's essence on creation

2. Create objects with their immutable essence.

%[https://maximilianocontieri.com/the-evil-powers-of-mutants]

# Examples

- Some persistence frameworks in static typed languages require an empty constructor.

# Exceptions

- Stateless objects. Always better solution than static class methods.

# Sample Code

## Wrong

[Gist Url]: # (https://gist.github.com/mcsee/d9d34fad693fd4f6309d68636a5010e5)
```javascript
class AirTicket {
   constructor() {     
  }
}
```

## Right

[Gist Url]: # (https://gist.github.com/mcsee/2e8f884045798f699399bf1ba9c26ab2)
```javascript
class AirTicket {
   constructor(origin, destination, arline, departureTime, passenger) {     
     
  //...     
  }
}
```

# Detection

Any linter can warn this (possible) situation.

# More info

%[https://codexposed.hashnode.dev/constructors-demystified]

%[https://maximilianocontieri.com/the-evil-powers-of-mutants]

%[https://maximilianocontieri.com/code-smell-10-too-many-arguments]

# Tags

- Essence

- Incomplete

- Mutable

# Conclusion

Always create complete objects. Make their essence immutable to endure through time.

Every object needs its essence to be a valid one since inception.

We should read Plato's ideas about immutability and create entities in a complete and immutable way.

These immutable objects favor bijection and survive the passing of time.

# Credits

Photo by Brett Jordan in Pexels

* * *

> In a purely functional program, the value of a [constant] never changes, and yet, it changes all the time! A paradox!

_Joel Spolski_

%[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]

*Last update: 2021/06/18*
