Re: Is there any sense in using final keyword in catch block?
- From: Tom Anderson <twic@xxxxxxxxxxxxxxx>
- Date: Thu, 23 Oct 2008 17:54:23 +0100
On Thu, 23 Oct 2008, Mike Schilling wrote:
Daniel Pitts wrote:Royan wrote:
The question is simple, does it make sense to add final keyword like in the example below?It is often a stylistic thing. My personal style is to make fields
try {
// code
} catch (final XxxxException e) {
// do exception handling
}
Whether it makes sense or not, please justify your answer.
and local variables final, but leave parameters/catch as not final.
My style is to not use final for local variables (including parameters and catch variables) at all. Unless they're being closed over by an anonymous class, where it's required. I think the point of a final qualifier is to reassure the programmer (reading or writing the code) that it won't change unexpectedly - it's an aid to reasoning about the code. Instance variables can be read and written from anywhere in the class (or even outside, if they're not private), and thus in a class of more than trivial size, it can be hard to keep track of whether that's happening, and where. Declaring a field final eliminates this difficulty at a stroke. However, local variables can only be accessed from inside the method they're local to; that's a much smaller body of code that the programmer has to keep in mind to know everything about the variable's use. There, i don't think there's much uncertainty to be reduced, and the additional keywords just clutter up the screen and mean more letters to type.
Note that for this analysis to hold, your methods have to be short: small enough that you can hold the whole thing in your mind at once. However, i would say that this is exactly how methods should be. If your method is big enough that making its locals final is helping you, then you need to split it up into multiple smaller methods. Essentially, final locals are a code smell.
This is completely arbitrary. My ideal would have been if Java had made every field/parameter/local final by default, and the programmer could specific nonfinal for things they really wanted to change.
I quite like this idea too. I'd want to see how it felt in practice before i fully supported it, though.
for (nonfinal int i = 0; i < 10; i++)
Yup, a *big* improvement.
Even better, IMHO:
for (variable int i = 0; i < 10; i++)
!
tom
--
Virtually everything you touch has been mined. -- Prof Keith Atkinson
.
- Follow-Ups:
- References:
- Is there any sense in using final keyword in catch block?
- From: Royan
- Re: Is there any sense in using final keyword in catch block?
- From: Daniel Pitts
- Re: Is there any sense in using final keyword in catch block?
- From: Mike Schilling
- Is there any sense in using final keyword in catch block?
- Prev by Date: Re: Handling of a SVG representation with Java's Text Listeners?
- Next by Date: Re: Struts - accessing the ActionForm bean in JSP
- Previous by thread: Re: Is there any sense in using final keyword in catch block?
- Next by thread: Re: Is there any sense in using final keyword in catch block?
- Index(es):
Relevant Pages
|