L
Lew
Pitch said:Error: cannot assign a value to final variable resPitch said:public void doSomething()
// throws [MyResource|App]Exception
{
final MyResource res; [snip]
try
{
// do something with res
}
finally
{
res.close()
}
}
This implies res is not going to get null in the last try-block. I think
this is a real possibility if more programmers work on the code so you'd
still need if != null
No, 'res' is guaranteed not to be null.
What about this:
assert res != null;
try
{
// do something with res
res = null;
}
finally
{
res.close()
}
1 error
The helpful compiler can now enforce the rules; no need to waste expensive
run-time tests or exceptions. With a dash of immutability, fine-grained
exception handling to distinguish acquisition failure from in-stream failure,
carefully positioned 'assert' invariants, and a properly-scoped 'finally' to
enforce resource release, one provably locks out the Bad Things from run-time
possibility.
It's worth a little extra care in the lower levels of resource client code to
gain such robustness.