Zero Argument Constructor: Code Smell 131

Objects created without arguments are often mutable and erratic

TL;DR: Pass all your essential arguments when creating objects.

Problems

Solutions

  1. Use one complete and single constructor.
  2. Avoid Setters and Getters

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

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

Objects created without arguments are often mutable and erratic

TL;DR: Pass all your essential arguments when creating objects.

Problems

Solutions

  1. Use one complete and single constructor.
  2. Avoid Setters and Getters

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

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


Print Share Comment Cite Upload Translate Updates
APA

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/

MLA
" » Zero Argument Constructor: Code Smell 131." Maximiliano Contieri | Sciencx - Monday May 2, 2022, https://www.scien.cx/2022/05/02/zero-argument-constructor-code-smell-131/
HARVARD
Maximiliano Contieri | Sciencx Monday May 2, 2022 » Zero Argument Constructor: Code Smell 131., viewed ,<https://www.scien.cx/2022/05/02/zero-argument-constructor-code-smell-131/>
VANCOUVER
Maximiliano Contieri | Sciencx - » Zero Argument Constructor: Code Smell 131. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/05/02/zero-argument-constructor-code-smell-131/
CHICAGO
" » Zero Argument Constructor: Code Smell 131." Maximiliano Contieri | Sciencx - Accessed . https://www.scien.cx/2022/05/02/zero-argument-constructor-code-smell-131/
IEEE
" » Zero Argument Constructor: Code Smell 131." Maximiliano Contieri | Sciencx [Online]. Available: https://www.scien.cx/2022/05/02/zero-argument-constructor-code-smell-131/. [Accessed: ]
rf:citation
» Zero Argument Constructor: Code Smell 131 | Maximiliano Contieri | Sciencx | 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.

You must be logged in to translate posts. Please log in or register.