Code Smell 13 - Empty Constructors

Code Smell 13 - Empty Constructors

Leader posted 4 min read

Non-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.

Refactorings ⚙️

https://maximilianocontieri.com/refactoring-001-remove-setters

https://maximilianocontieri.com/refactoring-016-build-with-the-essence

Examples

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

Sample Code

Wrong

class AirTicket {
   constructor() {     
  }
}
class AirTicket {
   constructor(origin,
                destination, 
                arline,
                departureTime,
                passenger) {     
     
  // ...
  }
}

Detection

Any linter can warn this (possible) situation.

Exceptions

Tags ️

  • Anemic Models

Level

[X] Beginner

Why the Bijection Is Important

In the MAPPER, objects correspond to real-world entities.

Real people aren't born nameless and formless, then gradually acquire attributes.

You don't meet someone who temporarily has no age or email address.

When you model a person, you should capture their essential attributes at birth, just like reality.

Breaking this bijection by creating hollow objects forces you to represent impossible states.

Empty constructors create phantom and invalid objects that don't exist in your domain model, violating the mapping between your code and reality.

AI Generation

AI code generators frequently produce this smell because they often follow common ORM patterns.

When you prompt AI to "create a Person class," it typically generates empty constructors with getters and setters.

AI tools trained on legacy codebases inherit these patterns and propagate them unless you explicitly request immutable objects with required constructor parameters.

AI Detection

AI tools can detect and fix this smell when you provide clear instructions.

You need to specify that objects should be immutable with required constructor parameters.

Without explicit guidance, AI tools may not recognize empty constructors as problematic since they appear frequently in training data.

Try Them!

Remember: AI Assistants make lots of mistakes

Suggested Prompt: Create an immutable class with required information. Include constructor validation and no setters. Make all fields final and use constructor parameters only

Without Proper Instructions With Specific Instructions
ChatGPT ChatGPT
Claude Claude
Perplexity Perplexity
Copilot Copilot
You You
Gemini Gemini
DeepSeek DeepSeek
Meta AI Meta AI
Grok Grok
Qwen Qwen

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.

Relations ❤️

https://coderlegion.com/7246/code-smell-01-anemic-models

https://maximilianocontieri.com/code-smell-28-setters

https://maximilianocontieri.com/code-smell-40-dtos

https://coderlegion.com/8662/code-smell-10-too-many-arguments

https://maximilianocontieri.com/code-smell-116-variables-declared-with-var

More Information

https://codexposed.hashnode.dev/constructors-demystified

https://coderlegion.com/6742/the-evil-power-of-mutants

https://coderlegion.com/8662/code-smell-10-too-many-arguments

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

1 Comment

0 votes

More Posts

Code Smell 319 - Hardcoded Stateless Properties

Maxi Contieri - Apr 9

Code Smell 17 - Global Functions

Maxi Contieri - Feb 28

Code Smell 16 - Ripple Effect

Maxi Contieri - Feb 19

Code Smell 15 - Missed Preconditions

Maxi Contieri - Jan 30

Code Smell 14 - God Objects

Maxi Contieri - Jan 23
chevron_left

Related Jobs

View all jobs →

Commenters (This Week)

4 comments

Contribute meaningful comments to climb the leaderboard and earn badges!