
Signature
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
> Sun, 29 Jun 2008 11:25:56 -0700 (PDT), ankur
> <ankur.a.agarwal@gmail.com> wrote, quoted or indirectly quoted someone
[quoted text clipped - 31 lines]
> Then it could be absolutely sure thePrice will be assigned. It does
> not have to analyse the conditional expressions to know this.
Yup, and to be sure that your variable will only be assigned precisely
once, create it using the final keyword.
final int thePrice;
if(..) thePrice = 1;
else thePrice = 2;
This will throw a compiler error whenever thePrice is not assigned, or
when thePrice is assigned a second time. Much more secure.
Note that I would not use two assignments in one line, it's less
readable and harder to work with in the debugger.
Regards,
Maarten
Arne Vajhøj - 30 Jun 2008 00:32 GMT
>> Sun, 29 Jun 2008 11:25:56 -0700 (PDT), ankur
>> <ankur.a.agarwal@gmail.com> wrote, quoted or indirectly quoted someone
[quoted text clipped - 41 lines]
> Note that I would not use two assignments in one line, it's less
> readable and harder to work with in the debugger.
When it comes to style I would recommend Java Coding Convention
from SUN.
The above is not compliant.
Arne
Maarten Bodewes - 30 Jun 2008 20:05 GMT
>>> Sun, 29 Jun 2008 11:25:56 -0700 (PDT), ankur
>>> <ankur.a.agarwal@gmail.com> wrote, quoted or indirectly quoted someone
[quoted text clipped - 46 lines]
>
> The above is not compliant.
Absolutely! My only possible way of explaining myself is not having the
Eclipse checkstyle plugin working on my Thunderbird client :)
Maarten
John B. Matthews - 30 Jun 2008 04:00 GMT
[...]
> Yup, and to be sure that your variable will only be assigned precisely
> once, create it using the final keyword.
[quoted text clipped - 5 lines]
> This will throw a compiler error whenever thePrice is not assigned, or
> when thePrice is assigned a second time. Much more secure.
Aha, I had seen this in Roedy's and others' code without fully
understanding its import. I'm a fan of letting the compiler do the
work:-) Thanks!
[...]

Signature
John B. Matthews
trashgod at gmail dot com
home dot woh dot rr dot com slash jbmatthews
Roedy Green - 30 Jun 2008 23:31 GMT
On Sun, 29 Jun 2008 23:00:31 -0400, "John B. Matthews"
<nospam@nospam.com> wrote, quoted or indirectly quoted someone who
said :
>Aha, I had seen this in Roedy's and others' code without fully
>understanding its import. I'm a fan of letting the compiler do the
>work:-) Thanks!
I am a great fan of final. See http://mindprod.com/jgloss/final.html
for why.

Signature
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com