Open
Labels
Description
Affects PMD Version: 6.21.0
Rule: CommentSize
Description:
Please enhance the documentation with why a comment that is too large is a problem.
For example, why is maxLines == 6? Is there some sort of research that says that a 7 line comment is worse than a 6 line comment?
For example, why is maxLineLength == 80? Is there some sort of research that says that lines longer or shorter than 80 is worse.
The reasons in documentation will help programmers decide when a comment really is too long instead looking at the size limitation as an arbitrary limit. I figured it was an arbitrary limit and disabled the rule altogether.
Reacted by B. K. Oxley (binkley), Linus Fernandes, Andrei Yusupau and Björn Kautler
Activity
oowekyala commentedon Feb 2, 2020
Tbh I can't find any reason why comment size should be limited, and don't expect to find any. Quality documentation cannot be measured in line length. My feeling would be that this rule shouldn't exist in our distribution. I'm curious, whether some developers find it useful, what threshold they use and why.
OTOH, limiting the margin for documentation can be useful. Afaik, in typography, the rule of thumb is to keep lines less than 60 to 70 chars in width. Accounting for source code indentation, this makes an 80 char margin reasonable. Too long, or too short, and the back and forth jumps are straining for the eyes. This is just from memory, though I could try to find sources for that.
tweimer commentedon Feb 2, 2020
I agree that maxLineLength is useful for readability, but maxLines can be removed IMHO.
linusjf commentedon Feb 8, 2020
Is there anything else that's to be done or can be done with this rule? Evidently, we don't want to be parsing javadoc comments for paragraph tags and restricting the size of their contents to six or eight lines. Or do we?
adangel commentedon Feb 13, 2020
The only hint I could find, is this:
pmd/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/documentation/CommentSizeRule.java
Line 21 in 542097f
If there are developers in the team, who enjoy writing novels, this rule could be used.
The default maxLines limit seems to be arbitrary and the metric of course is as well - it doesn't differ between lots of useful lines of comments and lots of nonsense content.
For maxLineLength, there is another reason, why to limit this: If you browse the source code e.g. on github, it easier, if you don't need to scroll horizontally... The limit here seems to be at about 120 characters. But this doesn't apply only to comments, it applies to anything. So, maybe a separate rule is better here (would probably be category best practices or code style).
I think, we should add this purpose ("manage those who can't shut up") to the rule description and clearly describe, that the rule has no way of understanding, whether the comment is useful or not - it just looks at the line numbers.
--> Pull requests are welcome --> https://github.com/pmd/pmd/blob/master/pmd-java/src/main/resources/category/java/documentation.xml#L48-L77
IMHO, I don't think this rule solves a real, actual problem.
linusjf commentedon Feb 13, 2020
My understanding is the maxlinelength attribute could be cannibalised to restrict paragraph size if needed. However, I couldn't find an optimal length for paragraph size. Also, how do you go about defining paragraph length? While I'm not much for writing lots of javadoc comments, I have come across code where the author has taken a great deal of trouble to lovingly explain how a method or class functions and its purpose. We could also look at Oracle Java ApI docs to see whether there's an optimal paragraph length to be used. However, as Clement rightly pointed out, this lies in the realm of typography. Additionally, sometimes paragraph tags are used and otherwise, they aren't. Do we need an additional javadoc parser and do we need a subset of an HTML parser on top of this?
Methinks, this becomes too complicated already. Can it be simplified?
oowekyala commentedon Feb 17, 2020
@Fernal73 Restricting paragraph size is also quite meaningless, especially if there is any non-trivial HTML markup. Javadoc parsing will probably be a feature of PMD 7 btw, but I wouldn't want to use it for this.
linusjf commentedon Feb 17, 2020
linusjf commentedon Feb 17, 2020
https://dzone.com/articles/using-markdown-syntax-javadoc
https://www.richardnichols.net/2009/06/markdown-doclet-for-javadoc/
I'll add this to the mix.
oowekyala commentedon Feb 17, 2020
Sure, let's make this complicated issue even worse...
Let's stick to what @adangel proposes and stay on topic here. I like the idea of making a separate rule to enforce margin width. Maybe it will be more discoverable that way (and not tied to CommentSize, which to me looks like it enforces a bad practice).
linusjf commentedon Feb 17, 2020
linusjf commentedon Feb 18, 2020
Or should we ignore this altogether?
Why bother?
If anyone wants to enforce paragraph limits, let them run it through a post-processor on the generated HTML documentation.
This would be on the lines on something like an HTML linter, except smarter.
Something like proselint, perhaps?
I'm liking the idea of generating markdown documentation for Java source code more and more, by the minute. Is there a possibility that Oracle is moving that way, too?
oowekyala commentedon Feb 18, 2020
I don't think we need to make the rule smarter than it is, when in fine checking paragraph size is just as meaningless as checking full line count. Imho any documentation is better than none, and placing arbitrary constraints on documentation, would actually encourage not writing documentation as a way to avoid arbitrary violations.
One exception to "any documentation is better than none", is autogenerated documentation, which is just noise. I think we should have a separate rule to detect that.
What i meant with margins is just enforce a character limit on line length, not trying to understand the HTML, which may be arbitrarily complicated and depend on css that we don't have access to. The point of the rule is to make the comment readable in the source, without altering the html output.
(closed by misclick)
linusjf commentedon Feb 18, 2020
oowekyala commentedon Feb 18, 2020
It's hardly comparable, you can't just "stop writing code", in the end your program has to do something. My point is that keeping paragraphs under a certain size doesn't improve the quality of documentation as much as it constrains the people writing it.
Yes. Things like the following are just a waste of space, the types are already documented. Either they should be removed, or given an actual description.
pmd/pmd-core/src/main/java/net/sourceforge/pmd/util/StringUtil.java
Lines 411 to 413 in 86cc64f
linusjf commentedon Feb 18, 2020
binkley commentedon Jul 27, 2024
I'm hitting this concern with comments on tests.
It doesn't feel like something that should be a default: each project would have their own standards on when comments are overlong.
jsotuyod commentedon Jul 27, 2024
Not really sure what you mean by this. PMD doesn't have default rules. As you said, each project would have their own standards (for everything really), and should create their own rulesets as stated in our best practices.
PMD does ship a
quickstart.xmlruleset, meant for people to get started quickly with PMD and serve as a base for their first own ruleset, but that one doesn't includeCommentSizeeither…fudianchn commentedon Aug 8, 2026
Thanks for the quick merge of #6948! @zbynek mentioned this issue in his review, which brought it to my attention.
Reading through the thread, one thing stood out. Despite the different conclusions about the rule itself, everyone seems to agree on the original ask here: the documentation should be honest about what
maxLinescan and can't do (a rough size check that can't judge quality, with an arbitrary default), whilemaxLineLengthis the part with a real readability rationale. That's also @adangel's concrete suggestion above.If it's helpful, I'd be glad to put together a small, non-breaking documentation clarification along those lines (no behavior change, just making the rationale and limitations explicit in the rule description and field JavaDoc). For context, the current description is just "Determines whether the dimensions of non-header comments found are within the specified limits." The draft would spell out each property instead, e.g. describing
maxLinesas a rough size check that cannot distinguish useful documentation from filler (with an arbitrary default), andmaxLineLengthas the genuine readability concern.I deliberately stopped short of touching
maxLinesitself (deprecating, splitting, or removing it), since that's clearly a design decision for the maintainers. But if there's ever appetite for a direction on that, I'm happy to help with a follow-up.numeralnathan commentedon Aug 8, 2026
The IDE automatically adds the following, but the developers don't add a comment. This really annoys me. It wastes my time to read through something that does not add any value. In fact, it would be better without it and save my time.
Is there a PMD rule to flag either of these? If not, I can create a feature request.