cover-img

Code Smell 13 - Empty Constructors

Incomplete objects cause lots of issues.

11 October, 2022

9

9

0

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

Solutions

1.

Pass the object's essence on creation

2.

Create objects with their immutable essence.

Examples

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

Sample Code

Wrong

Right

Detection

Any linter can warn this (possible) situation.

Exceptions

Stateless objects. Always better solution than static class methods.

More info

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
This article is part of the CodeSmell Series.

refactoring

cleancode

codesmell

9

9

0

refactoring

cleancode

codesmell

Maxi Contieri

Buenos Aires, Argentina

🎓Learn something new every day.📆 💻CS software engineer 👷coding👨🏽‍🏫teaching ✍🏾writing 🎨Software Design 🏢SOLID 🌉TDD 👴Legacy 💩Code Smells

More Articles

Showwcase is a professional tech network with over 0 users from over 150 countries. We assist tech professionals in showcasing their unique skills through dedicated profiles and connect them with top global companies for career opportunities.

© Copyright 2024. Showcase Creators Inc. All rights reserved.