java:S1186 FP for default constructors in java 21+?

With java 21 I see javadoc warnings like these:

warning: use of default constructor, which does not provide a comment

Adding an explicit constructor to provide javadoc does make sense, I guess.

When I run SonarQube, I then get warnings for missing comments. Which also makes sense, I guess. Generally.

But javadoc linting and sonar java:S1186 kind of fight each other here, on the explicit empty constructor:

/** Create new instance of FooBar. */
public FooBar() {
    // Empty constructor to make javadoc lint happy, this comment to make SonarCloud happy
}

I would dearly love to have an option on java:S1186 to allow an empty public (no-argument) constructor.

Hi Jesper,

Thank you for your feedback! This is an interesting use-case. However, I believe that S1186 should still not make an exception for empty default constructors that have a JavaDoc comment.

The protection that S1186 provides is still relevant in this scenario, as the implementation might be forgotten or misunderstood by readers who may wonder if it was unintentionally left out. If a default constructor has a JavaDoc, it could be even more confusing, as the documentation might describe behavior that is not actually implemented.

A similar situation arises when a subclass overrides an empty method with another empty method, simply to update the JavaDoc for the method in the subclass.

In both cases (empty default constructors and empty method overrides), I therefore think that it’s still beneficial to clearly state the developer’s intent with a comment:

class FooBar extends Bar {

    /** Behavior description, notes, comments, examples */
    public FooBar() {
        // empty
    }

    /** Behavior description, notes, comments, examples */
    @Override
    public void doSomething() {
        // empty
    }
}

Thanks for considering the issue.

Does the two different views here not call for the suggested option?

Cheers!

Would you suggest then having a rule parameter for S1186 to enable or disable the report of empty methods and constructors in case they have a JavaDoc comment?

Since there is a workaround that requires only 8 extra characters (// empty), I think it’s not worth the effort to implement that. It would also go against the spirit of that rule.

Best,
Marco

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.