This content originally appeared on Level Up Coding - Medium and was authored by Maximiliano Contieri
Objects created without arguments are often mutable and erratic

TL;DR: Pass all your essential arguments when creating objects.
Problems
- Mutability
- Anemic Models
Solutions
Context
It is common usage using a zero-argument constructor and a bunch of setters to change it.
Beans is a well-known example of this code smell.
Sample Code
Wrong
public Person();
// Anemic and mutable
Right
public Person(String name, int age){
this.name = name;
this.age = age;
}
}
// We 'pass' the essence to the object
// So it does not mutate
Detection
[X] Automatic
We can check all constructors, but there are some false positives.
Stateless objects are a valid example.
Tags
- Mutability
Conclusion
Empty constructors are mutability hints and accidental implementation issues.
We need to research usages to improve our solutions.
Relations
More Info
- Nullary Constructor
- Nude Models — Part I : Setters
- Nude Models — Part II : Getters
- The Evil Power of Mutants
Credits
Photo by Ade Adebowale on Unsplash
Don’t worry about design, if you listen to your code a good design will appear…Listen to the technical people. If they are complaining about the difficulty of making changes, then take such complaints seriously and give them time to fix things.
Martin Fowler
Software Engineering Great Quotes
This article is part of the CodeSmell Series.
How to Find the Stinky parts of your Code
Zero Argument Constructor: Code Smell 131 was originally published in Level Up Coding on Medium, where people are continuing the conversation by highlighting and responding to this story.
This content originally appeared on Level Up Coding - Medium and was authored by Maximiliano Contieri

Maximiliano Contieri | Sciencx (2022-05-02T00:10:14+00:00) Zero Argument Constructor: Code Smell 131. Retrieved from https://www.scien.cx/2022/05/02/zero-argument-constructor-code-smell-131/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.