Person p = ....
p = doSomething(p)
...
p = doSomethingElse(p)
is hard to reason about when it should be Person p = ....
... lots of code
Person updated = doSomething(p)
... lots of code
doSomethingElse(updated)
Which is easier to read and understand. So you might have had bugs that would have been prevented with final, but you didn't attribute to final.You shouldn't be able to doSomethingElse(p) when you actually mean to doSomethingElse(updated).
What would help is splitting the function up so that you don't have multiple named variables representing multiple versions of the same thing in the same scope:
Person doSomethingAndDots() {
Person p = doSomething(...)
...
return p
}
main() {
doSomethingElse(doSomethingAndDots())
}That is to ease the pain of unwrapping nested types and error-checking intermediate steps so you don't have to come up with new names for each intermediate.
In the codebase I work on most, all variables that can be marked final are marked final. That means that whenever I see a non-final variable, I know that something tricky is happening and I need to be extra careful in reading the code.
The Optional class is typical of the halfassery that has accompanied many improvements to Java over the years. From java.util.logging to generics to streams, I'm invariably irritated by compromises particularly as they've decided that maintaining backward compatibility is now a secondary concern.
I've run out of patience in the direction Java has taken and I've been a user since 1.0.4 - I'm going to try Kotlin for my next project.
It also works in the opposite direction: a Java parameter or return value annotated as @Nullable (doesn't have to be intellij's, the Kotlin compiler understands several annotation libraries) will appear as a nullable reference to Kotlin code, and a @NotNull or @Nonnull annotation makes it appear as a non-nullable reference.
return Optional.ofNullable(applications).orElseThrow(NullPointerException::new);
return Optional.ofNullable(applications).orElse(new Application());