RFR: 8071571: Move substring of same string to slow path
john.r.rose at oracle.com
Wed Apr 1 02:54:32 UTC 2015
On Mar 27, 2015, at 3:01 PM, Martin Buchholz <martinrb at google.com> wrote:
> final Type foo = this.foo;
There are IMO a few places where local finals pay for themselves,
even after Java 8.
One is Martin's idiom. The key thing to observe is that 'foo' is
a read-only snapshot or view of 'this.foo'. If we were after
conciseness (only) we could delete the whole line, but the line
as a whole has a clear purpose.
Another reasonable use of local finals is when writing SSA-style code.
A blank final enforces SSA, and makes it clearer what's going on.
If I'm writing a large, complex loop and I want to make it clear that
something is loop-invariant, I sometimes use a final. But in cases
like that it's better to factor the loop body into a separate method,
if that is possible.
Another place I like finals is where I am capturing values which
are constants, sampled from a variable (e.g., "cursor++"). In
that case, the different declaration makes clear the distinction
between a snapshot and a varying cursor. This is of course
a matter of taste.
In all these cases the benefits are only present when the affected
variable has a large or complex live range. So the best use
cases are always going to give the onlooker a sense of unease.
In simple use cases, "final" is just noise, but in complex ones
it can help solidify the reader's understanding.
More information about the core-libs-dev