-
Notifications
You must be signed in to change notification settings - Fork 146
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
Define best practice regarding position of @NonNull
annotations
#10380
Comments
@NotNull
annotations@NonNull
annotations
Regarding the position of NonNull, Nullable (and other type) annotationContextWe detected an inconsistent pattern during recent code reviews when using annotations and the final keyword, especially for method parameters. Our style includes both of the following patterns:
This is also present:
We can discover the same inconsistency in the use of
Our hedera code style is not explicit for this case, but it does mention:
Following the idea that the declaration statement for a field and the declaration of a method parameter should be consistent, we could infer that the suggested approach regarding method parameters is:
It is worth mentioning that spotbugs Things to considerOur code-baseAs mentioned above, we have occurrences for both styles, but there is a remarkable preference towards the pattern: IntelliJ inconsistencyWrong order of method parameter annotations vs. final modifier For example, Given a Type annotation Given that SpotlessSpotless does not currently reorder annotations. It does not move annotations around modifiers. Spotless also does not reorder annotations of a particular variety (say, putting all declaration annotations in a canonical order, such as alphabetical order. One can use formatAnnotations spotless/plugin-gradle/README.md at main to enforce the rule "type annotations should appear immediately before the type they annotate" but only when writing the annotations in the correct places (before/after modifiers such as public/final)
It will be formatted as follows:
The final modifier can't be reordered despite adding NonNull to recognized type annotations (that are not automatically recognized by formatAnnotations). A word from other tools (checker framework)The Checker Framework Manual: Custom pluggable types for Java
ConclusionIt's a gray area. Even though Palantir, Google, and Headera code-style documents expressed in a way what is the desired style when using type annotations, the NonNull and Nullable annotations we are using from Spotbugs ( which is known not to have support for type annotations) are masked declaration annotations. We don't have any other custom type annotations. A possible solution to enforce the rule is to implement our spotless formatting step that would apply the desired style. We already implemented a custom step for handling the license: ReferencesType Annotations and Pluggable Type Systems (The Java™ Tutorials > Learning the Java Language > Annotations) |
For what it's worth, the modular services code has been (informally at least) preferring |
After a conversation with @rbair23 the following was defined:
There is no strong motivation to force only one pattern, so closing the ticket and leaving it for future reference. |
Review comment on PR:
Originally posted by @hendrikebbers in #10332 (comment)
The text was updated successfully, but these errors were encountered: