0

It is generally a good idea to restrict access to your variables as much as reasonably possible. For example make things private instead of publicand make variables final if they aren't supposed to change. I believe the same applies to method parameters, but I find actual practice of this to be extremely rare.

For example, no IDEs that I know of offer warnings for finality of method parameters, even though you should be able to tell easily if the parameter can be made final. Even core Java libraries don't make their method parameters final, even though they could; and I find it pretty rare for a 3rd party library to do this either.

Overall the benefits here are certainly relatively low, given that the scope is so small. Does it give enough benefits though to justify as a coding standard?

8
  • No. Until somebody cites an example where it matters, this is a waste of time that clutters your code. In Java, what does the final parameter even do or prevent?
    – user949300
    Commented Sep 27, 2017 at 1:05
  • I think this is primarily opinion based, so I'm voting to close. IMO the scope is small enough and reassigning arguments in a method is such a unusual thing to see that while it would be syntactically more correct, it isn't important to mark them final. It may be better to have a compiler plugin that errors when parameters are assigned to.
    – Samuel
    Commented Sep 27, 2017 at 1:17
  • This to me sounds like the definition of boiler plate that people complain about in Java. Along the same lines where all local variables are final but then objects keep on getting mutated by their methods. Commented Sep 27, 2017 at 2:10
  • @Samuel If OP would rephrase to ask "whats a concrete example of a final parameter making an improvement" that, IMO, would be worthwhile.
    – user949300
    Commented Sep 27, 2017 at 2:50
  • @user949300 Any time you use a final parameter, you can simply omit final and the code will work with the same behavior. final is about what you cannot do, and thus what assumptions you can make. So instead you'd want examples of confusing or broken code that would have been disallowed had the parameter been final. Commented Sep 27, 2017 at 3:07

0