-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
New Code Style rules for DSpace "master" branch #1895
Conversation
It is fine for me. I have tested the eclipse plugin and it works (add notes to the wiki page). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm OK with the current rule set and I've added some proposals for additional rules.
checkstyle.xml
Outdated
|
||
<!-- ##### Other / Miscellaneous requirements ##### --> | ||
<!-- Require utility classes do not have a public constructor --> | ||
<module name="HideUtilityClassConstructor"/> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Related to visibility, I propose to also disallow direct class member access except for protected and final constant fields:
<module name="VisibilityModifier">
<property name="protectedAllowed" value="true"/>
<property name="allowPublicFinalFields" value="true"/>
<property name="allowPublicImmutableFields" value="true"/>
</module>
http://checkstyle.sourceforge.net/config_design.html#VisibilityModifier
In addition, I would also prevent any redundant visibility modifiers to keep to code clean:
<module name="RedundantModifier"/>
-> http://checkstyle.sourceforge.net/config_modifier.html#RedundantModifier
<module name="TreeWalker"> | ||
<!-- Maximum line length is 120 characters --> | ||
<module name="LineLength"> | ||
<property name="max" value="120"/> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I want only note that on the current javadoc for a REST class we have a violation that cannot easily solved...
https://github.com/DSpace/DSpace/blob/master/dspace-spring-rest/src/main/java/org/dspace/app/rest/utils/MultipartFileSender.java#L31
due to the long URL...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point, @abollini. It looks like we could simply tweak this setting similar to how Google does, and add in an ignorePattern
exception for long URLs:
https://github.com/checkstyle/checkstyle/blob/master/src/main/resources/google_checks.xml#L44
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This has now been resolved, as I've added exceptions for long URLs, packages or imports.
👍 on these rules...it's a good opportunity to do this, and these particular ones have the advantage of being well-accepted and understood by other java developers out there. A couple other non-controversial ones you might consider adding, while you're at it: |
I'm more concerned that we have a common style at all, than that we change it. In particular, I would be very wary of adding too many new rules at once. Which qualities of code on the screen are most important? Which contribute the most to readability and comprehension? why? Left to myself, I tend to use Allman brace style (although I only just found that it has a name). This is probably due to being a fossil from the PL/I era, when blocks opened with BEGIN and closed with END. I'll have to train myself to rely more on NetBeans' block-level indicator. Just grumbling.... The two things I have long wished we would enforce are tab/space discipline, and class and method documentation. Sadly I don't think Checkstyle can help with documentation that doesn't actually tell one anything significant. And then there's the issue of the well-known argument: when a method takes a org.dspace.core.Context, we all know what it is but one must write something anyway. One other thing that just occurred to me is that we ought to keep in mind a distinction between style rules and code-quality rules. Styling is for human readers. (Documentation rules are for human readers who are assisted by tooling that is sensitive to doc comments.) Beyond the rules themselves, I think that we would do well to make it simple for contributors to adjust their several development environments to follow those rules, and especially make it simple to make adjustments when developing for DSpace separately from those for use with local code (which may be subject to different standards). We'll need a clear human-readable articulation of The Rules. We would greatly facilitate adoption of The Rules by providing comprehensive and maintained style sets for all of the IDEs we can support. (I will help with NetBeans.) |
<property name="allowMissingParamTags" value="false"/> | ||
</module> | ||
<!-- Requirements for Javadocs for methods --> | ||
<module name="JavadocMethod"> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These defaults for javadoc seem sensible to me. I am glad to see the flexibility around params/returns/throws.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm a bit unhappy with making @param optional, but I can live with it. Expect to see them added whenever I have to puzzle out a method's requirements.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mwoodiupui: I think @param
should be highly encouraged, yes. I was mostly hesitant about making @param
required because I didn't want to slow this work down by forcing us to write a lot of code comments from scratch. I think future PR(s) could add in any missing @param
comments and then flip the switch to make them required.
@mwoodiupui : Quick note regarding IDE support. We've begun drafting some notes on enabling Checkstyle rules in various IDEs here: https://wiki.duraspace.org/pages/viewpage.action?pageId=90967266#CodeStyleGuide(WIP)-IDESupport I haven't yet tested NetBeans' Checkstyle import plugin, but would welcome enhancing the notes there. The overall goal is to use IDE-specific Checkstyle plugins, which would mean we'd be able to simply maintain our checkstyle definition, and ask everyone to import it into their IDE of choice. That should make IDE support a bit more seamless, assuming the IDE's Checkstyle plugin has no "gotchas". |
I've now tried out the NetBeans plugin "Checkstyle Beans". It partially integrates with the editor. Rule violations cause the line to be underlined in wiggly pale yellow, and a "tag" icon appears in the left margin. Hovering on the tag shows the rule's message. Plugin and built-in annotations rotate as a single list when you click on the arrow icon that means "multiple annotations this line." Unlike the built-in rule engine, no hints are offered to the editor to "just fix it". Corrections are not noted until the file is saved. Hovering on the underlined text itself does nothing, instead of showing the rule message(s). So: using IDE plugins with a common style file works for NetBeans, but will take some getting used to. |
I'd like to see <module name="IllegalCatch"/> at some point, but we have a lot of them. |
I've made some minor enhancements to the rules, based on above comments. I only pulled in suggestions which seem minor / non-controversial, as I'd like to avoid larger code refactors in this first cut, and instead concentrate mostly on obvious wins. Here's what I've added in 2fccaa4
Further feedback is welcome. Again, though, I'd encourage us to limit the number of initial rules here to only those that are non-controversial, quick wins (with the primary goals being to standardize our code alignment and braces). We can always follow up this initial code style with further enhancements. |
2fccaa4
to
1d355b3
Compare
Another minor update. Behind the scenes, I've been working on bulk changes to the
I've also been documenting how to do bulk code style changes in IDEA here: https://wiki.duraspace.org/pages/viewpage.action?pageId=90967266#CodeStyleGuide(WIP)-Fixingthecodebase |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that the current settings are a good starting point to start validating our code with checkstyle.
…exception for throws. Disable Javadocs test for public methods.
1d355b3
to
189212e
Compare
Good work, thank you @tdonohue. 👍 |
+1 thanks Tim! |
PR is now considered ready for merger, though any additional comments are more than welcome.
I'd like to propose the following new Java Code Style Rules for DSpace
master
branch (these rules will not be backported to any previous branches):These rules are both listed and enforced in the
checkstyle.xml
configuration applied to this PR's branch. More information on Checkstyle settings can be found at http://checkstyle.sourceforge.net/checks.htmlThis PR serves as public notification of this proposal, and I ask that all interested developers add your feedback into this PR (feel free to comment on individual line numbers in the
checkstyle.xml
file). Please let us know if you have objections to any proposed rule, or propose tweaks or additions to the set of rules. If you approve of these code style rules as-is, you are also welcome to 👍 this PR.By default, the Checkstyle configuration is currently not executed (as the existing code has many violations to these new rules, see below). However, you can test them locally by running:
Currently, based on the rules proposed above, here's the counts of violations in each DSpace module:
As the number of violations are significant, this work is being performed on a shared, public branch (
code-style-rules
). That way we can collaborate on fixing the violations.More information on this effort (including other sample Checkstyle configs from other projects) can be found at: https://wiki.duraspace.org/pages/viewpage.action?pageId=90967266