Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Default constructor is good or evil? Checkstyle and PMD are opposite here [closed]

Checkstyle says:

Class should define a constructor.

PMD says:

Avoid unnecessary constructors - the compiler will generate these for you.

Who is right? Or let's put it this way - what are the pros and cons of having an empty default ctor in a class?

like image 364
yegor256 Avatar asked Oct 26 '10 13:10

yegor256


1 Answers

I like PMD's answer. The less code the better. Don't write constructors the compiler will write for you.

My impression is that the main argument for writing the constructor is that some poor programmer who doesn't understand how constructors work in Java might stumble over your code and get confused. I don't like writing code that is needlessly obscure but I don't like writing dumbed-down code either.

But this is me being obsessive and probably unreasonable. There is a world of application programmers whose central focus is on the business, not on the language, and who are not language experts. A survival technique a lot of people use is to have a consistent style, whether it is absolutely necessary is not the point.

like image 183
Nathan Hughes Avatar answered Oct 12 '22 12:10

Nathan Hughes