question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Discussion: `final` modifier convention in method arguments

See original GitHub issue

final 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 final modifier in method arguments?
    void foo(final int a, final String b) { ... }
    
    • Pros: Compiler force immutable.
    • Cons: Method signature could be too long.
  • No final modifier 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 final to force it immutable.
  • No final modifier in method arguments + clarify the convention in the CONTRIBUTING.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:open
  • Created 4 years ago
  • Reactions:3
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
trustincommented, Nov 11, 2019

For this library, I agree with @anuraaga . I vote for no final in method args/local + checkstyle.

+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

0reactions
codefromthecryptcommented, Feb 28, 2020

(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)

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found