Discussion: `final` modifier convention in method arguments
See original GitHub issuefinal modifier is used in local variables or class fields when it could be immutable. However, conventionally, we didn’t use the final modifier in method arguments. CONTRIBUTING.md or checkstyle doesn’t mention or force about it. Sometimes it confuses contributors who contribute Armeria for the first time when writing a method.
I want to hear folks’ opinions about the final modifier convention.
- Add
finalmodifier in method arguments?void foo(final int a, final String b) { ... }- Pros: Compiler force immutable.
- Cons: Method signature could be too long.
- No
finalmodifier in method arguments +checkstyle- Pros: Method signature is cleaner, and we don’t have to review anymore.
- Cons: Flexibility 👎. Sometimes, he/she might want to add
finalto force it immutable.
- No
finalmodifier in method arguments + clarify the convention in theCONTRIBUTING.md- Pros: Flexibility 👍. The method signature is cleaner.
- Cons: No validation in the build process.
Additionally, modern IDE like IntelliJ draws an underline in a local variable if it is mutable. Remove the final modifier for the local variable to reduce verbosely?
Issue Analytics
- State:
- Created 4 years ago
- Reactions:3
- Comments:6 (4 by maintainers)
Top Results From Across the Web
Using the "final" modifier whenever applicable in Java [closed]
I'm pretty dogmatic about declaring every possible variable final . This includes method parameters, local variables, and rarely, ...
Read more >marking parameters and local variables as final
Method parameters and local variables should not be declared final unless it improves readability or documents an actual design decision.
Read more >Writing Classes and Javadoc
We will discuss how to write static methods first programs (and learn about the special main method in an application program) and then...
Read more >In Java, should I use "final" for parameters and locals even ...
I use final the same way as you. To me it looks superfluous on local variables and method parameters, and it doesn't convey...
Read more >What is final in Java? Final variable , Method and Class ...
Javarevisited · Now, let's understand the final keyword in detail in Java. · Any variable either a member variable or local variable (declared...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

+1
IntelliJ automatically highlights a mutable variable/parameter, so I’m happy without
final- https://intellij-support.jetbrains.com/hc/en-us/community/posts/360000395439/comments/360000082320(ps my vote is no final on locals/parameters unless it is somehow clarifying. use the attention of the keyword explicitly rather than the other way around)