Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

7
  • 18
    If your method is clear and does only one thing the reader will know too. Final word only makes the code unreadable. And if its unreadable its much more ambiguous Commented Mar 3, 2016 at 14:46
  • 13
    @eddieferetro Disagreed. The keyword final states intent, which makes the code more readable. Also, once you must deal with real-world code, you'll notice it's seldom pristine and clear, and liberally adding finals everywhere you can will help you spot bugs and understand legacy code better. Commented Aug 26, 2016 at 2:21
  • 12
    Is the "intent" that the variable will never change, and that your code relies on that fact? Or is the intent "it just so happens that this variable never changes so I'm marking it final". The later is useless and possibly harmful noise. And since you seem to advocate marking all the locals, you are doing the later. Big -1. Commented Aug 26, 2016 at 4:19
  • 4
    final states intent, which is usually a good thing in a piece of code, but it comes at the price of adding visual clutter. Like most things in programming, there is a trade-off here: is expressing the fact your variable is only going to be used only once worth the added verbosity? Commented Oct 11, 2017 at 8:30
  • 2
    The visual clutter will be greatly reduced when using syntax highlighting. I mark my locals that are assigned only once - and that I want to be assigned to only once - with final. That may be most of the locals, but clearly not all. For me there is no "happens to never change". There are 2 clearly separated kinds of locals in my world. Those that aren't ever supposed to change again (final) and those that must change as a result of what the task at hand dictates (of which there are very little, making the functions pretty easy to reason about). Commented Oct 23, 2019 at 14:50