|By David Dossot||
|December 31, 2012 07:15 AM EST||
In "Final Parameters and Local Variables", Dr. Heinz M. Kabutz rants against the generalized used of the final keyword in Java code. For him, this is a "trend' and an "idiotic coding standard".
I'm a firm believer of the complete opposite.
As a software developer, I spend most of my time reasoning about code. Anything that can make this reasoning easier is welcome. Good practices like short methods and descriptive names fall in this category. I believe immutable variables do too.
Immutable variables simplify reasoning because they ensure a stable state within a scope, whether it's a whole class or a single method. Having established invariants is a tremendous help in understanding code.
Whether it is with my own code or not, I've experienced time and again that my mental load was way lower with immutable variables than mutable ones. Maybe it's just a limitation of my own brain power, but, to me, less mental load translates in deeper understanding. And to the contrary, finding out amid-function that one of its argument has been reassigned creates an intense sense of confusion, prompting to re-read the method again. And again.
Of course, this isn't 100% true in Java, mainly because its default data structures are unfortunately mutable. But still, the comfort gained by using the final keyword everywhere, and actually letting your favorite IDE do it for you, far outweighs the small visual clutter it creates.
Unsurprisingly, I'm of the same opinion about early returns and loop breaks, but this is for another debate...
Dr. Kabutz will certainly argue that this is a matter of personal discipline or talent to ensure that one doesn't mess with invariants, because he doesn't "need the compiler to tell [him] this". Again I disagree. I don't trust myself to be on top of things at all time so I want the compiler to tell me everything... and more. I want Findbugs to scrutinize everything I write and break the build if I've been sloppy. I want Checkstyle to reject my code if it isn't compliant to whatever standard is enforced on the project I'm working on.
I do agree on one thing that Dr. Kabutz said though, which is that using the final keyword everywhere in printed books' code snippets is an annoyance. Indeed, books formatting rules constraints on code samples are so stringent (think 71 columns) that the rules of readability are tipped towards "less code as possible".
What is your experience with final variables everywhere? Love, hate or ...
Nov. 25, 2014 09:30 PM EST Reads: 953
Nov. 25, 2014 09:30 PM EST Reads: 988
Nov. 25, 2014 08:00 PM EST Reads: 1,319
Nov. 25, 2014 07:00 PM EST Reads: 1,117
Nov. 25, 2014 04:30 PM EST Reads: 1,184
Nov. 24, 2014 07:00 PM EST Reads: 1,566
Nov. 24, 2014 12:00 PM EST Reads: 1,444
Nov. 24, 2014 11:00 AM EST Reads: 1,579
Nov. 24, 2014 09:00 AM EST Reads: 1,594
Nov. 23, 2014 07:30 PM EST Reads: 1,784
Nov. 23, 2014 12:00 PM EST Reads: 1,724
Nov. 23, 2014 07:45 AM EST Reads: 1,736
Nov. 22, 2014 05:30 PM EST Reads: 1,473
Nov. 22, 2014 05:30 PM EST Reads: 1,540
Nov. 21, 2014 08:00 PM EST Reads: 1,471
Nov. 21, 2014 08:00 PM EST Reads: 1,543
Nov. 21, 2014 07:00 PM EST Reads: 1,399
Nov. 20, 2014 09:15 PM EST Reads: 1,451
Nov. 20, 2014 06:00 PM EST Reads: 1,390
Nov. 20, 2014 01:00 PM EST Reads: 1,670