Re: Usefulness of "final" (Was: Re: Inserting In a List)
On 03.04.2013 20:51, lipska the kat wrote:
On 03/04/13 18:08, Robert Klemme wrote:
I am not sure what you mean by that. Can you elaborate? Where's the
storage class in the example above?
final, although it's not is it, at least it's not Java terminology,
apologies, I should have said 'modifier'. I'll restate.
Ah, OK, I see. Thanks!
Well I'm not sure that using a modifier to help you write a
conditional statement is 'good programming style'. When I see the
modifier final it says something to me, it says, this value is not
modifiable ('scuse the pun).
Sometimes you need nested conditions as shown and with a blank final you
can ensure that the variable is assigned exactly once on every possible
path. I find that useful.
Is it improving the clarity of your code to
use final for it's side effect, that is the side effect of causing the
compiler to barf because a final variable may already have been
initialized. I'm not sure about that.
That's not a side effect - it is _the_ effect of "final" modifier for
variables. "final" ensures a variable is assigned to at most once:
http://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.12.4
There is no other effect - final is all about assignment. This is true
for local variables - it's different with field which get assigned
compile time constants.
I'd say, "final" is probably even more useful for fields because it
ensures that a field is initialized in every constructor path - you
cannot forget it.
http://docs.oracle.com/javase/specs/jls/se7/html/jls-8.html#jls-8.3.1.2
For a single local variable I'd probably agree, in fact in general I
would agree but that wasn't my initial point really, in the code that
kicked off this sub thread there was more than one final variable, in
fact there were several in close proximity, I was initially questioning
the clarity of this for a new user. However then I opened my mouth and
put my foot in it and said ...
:-)
Maybe I could better see (and agree) if you provide a specific example
of what you mean here.
I think you probably know what I mean and any off the cuff example will
be contrived to the point irrelevance, so, leave it with me and I'll see
if I can come up with a simple self contained example.
Thank you!
Kind regards
robert
--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/