> I see some style gurus recommend using "final" on
> parameters that are not modified by the method.
[quoted text clipped - 5 lines]
> Would it not be better to have this "final" as an implicit
> feature defined by the language?
The language currently requires the use of final for parameters which
are used within an anonymous inner class. This use of final is only
relevant to the implementation of a method. It is never of interest to
clients of a method.
Mark Thornton
> I see some style gurus recommend using "final" on
> parameters that are not modified by the method.
[quoted text clipped - 5 lines]
> Would it not be better to have this "final" as an implicit
> feature defined by the language?
I don't think so. You'd then need a `nonfinal' keyword
to override the implicit finality, or else you'd need to
copy parameters to non-final local variables.
You say you modify only one in ten thousand parameters.
For my part, I find I modify somewhat more than that, usually
when I want to "normalize" them somehow. For example, I have
a Fraction class that represents rational numbers as a pair of
BigInteger values, and in the constructor I want to enforce a
few things:
public Fraction(BigInteger num, BigInteger den) {
// ensure that they're actually BigIntegers
if (num.getClass() != BigInteger.class)
num = new BigInteger(num.getBytes());
if (den.getClass() != BigInteger.class)
den = new BigInteger(den.getBytes());
// ensure that the denominator is non-negative
if (den.signum() < 0) {
den = den.negate();
num = num.negate();
}
// ensure relative primality
if (den.signum() != 0) {
BigInteger gcd = num.gcd(den);
if (! gcd.equals(BigInteger.ONE)) {
num = num.divide(gcd);
den = den.divide(gcd);
}
}
// now in "canonical form;" proceed with construction
...
}
This pattern of "progressive regularization" seems natural to
me: I have three (in this case) conditions to enforce, so I test
each in turn and adjust things as necessary. I *could* leave
the parameters alone and copy them to local variables, but what
benefit would that bring?
> [...]
> I never use "final" on parameters.
>
> In case you do, why is that so?
I use it when the parameter's value will be referenced
by an inner class:
void method(Thing thing) {
gizmo.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent evt) {
thing.doSomething(evt);
}
});
}
... will not compile as it stands. Adding `final' to the
method parameter fixes things.

Signature
Eric Sosman
esosman@ieee-dot-org.invalid
Hendrik Maryns - 11 Mar 2008 15:13 GMT
Eric Sosman schreef:
>> I see some style gurus recommend using "final" on
>> parameters that are not modified by the method.
[quoted text clipped - 48 lines]
> the parameters alone and copy them to local variables, but what
> benefit would that bring?
Probably none, but this also means your argument *against* making all
method parameters final is void, and there would be no need for a
‘non-final’ keyword.
(This is how things are in Eiffel, btw, and no problems there.)
H.

Signature
Hendrik Maryns
http://tcl.sfs.uni-tuebingen.de/~hendrik/
==================
http://aouw.org
Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html
Jacob - 11 Mar 2008 21:32 GMT
> This pattern of "progressive regularization" seems natural to
> me: I have three (in this case) conditions to enforce, so I test
> each in turn and adjust things as necessary.
I wouldn't recommend this practice.
You should establish a contract and stick to it. If your are
unhappy with your clients input you should rather throw an
IllegalArgumentException than silently adjust the parameters
to fit your needs.
I cannot from my own expereince recall any cases where I ever
found it natural to alter the input parameters like this.
Eric Sosman - 11 Mar 2008 22:39 GMT
>> This pattern of "progressive regularization" seems natural to
>> me: I have three (in this case) conditions to enforce, so I test
[quoted text clipped - 6 lines]
> IllegalArgumentException than silently adjust the parameters
> to fit your needs.
I think it helpful to "be lenient in what you accept and
strict in what you emit" (several variations are in circulation;
I've been unable to track down an authoritative original).
The principle can be overdone -- some blame it for the riotous
and weed-choked state of HTML, for instance -- but used with
care can make your classes easier to use and hence more useful.
To put it another way: The add() method of Set returns
false if the Set already contains the argument item. Would
it have been better design to throw an exception? ;-)
> I cannot from my own expereince recall any cases where I ever
> found it natural to alter the input parameters like this.
new Folks("Eric").strokes() != new Folks("Jacob").strokes()

Signature
Eric.Sosman@sun.com
Andreas Leitgeb - 12 Mar 2008 08:53 GMT
> I cannot from my own expereince recall any cases where I ever
> found it natural to alter the input parameters like this.
Actually, I do recall such events (perhaps not exactly "like this",
though). Most of the time it were really short and simple methods,
where I saved myself a local variable (plus copying the parameter
to it).
> I see some style gurus recommend using "final" on
> parameters that are not modified by the method.
>
> That probably includes more than 99.99% of all parameters,
> where the majority of the remaining 0.01% probably are
> programming mistakes.
> The "final" does not protect against what seems obvious:
> mutation on the dimension instance.
[quoted text clipped - 7 lines]
>
> In case you do, why is that so?
I don't use final on parameters.
It does not really provide anything and just clutters the code.
Arne
>Would it not be better to have this "final" as an implicit
>feature defined by the language?
If we had the language design to do this over again, I think it would
make more sense to default to final and require a "mutable" or "var"
keyword. Unfortunately it is too late now. You can't change that in
an upward compatible way. Perhaps the designer's of Java's successor
(e.g. Fortress) might be able to correct that.
Though compilers can effectively put the final in itself, it is great
documentation ESPECIALLY when it is missing, to warn you to look for
redefinition.
--
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
Hendrik Maryns - 12 Mar 2008 10:58 GMT
Roedy Green schreef:
>> Would it not be better to have this "final" as an implicit
>> feature defined by the language?
[quoted text clipped - 8 lines]
> documentation ESPECIALLY when it is missing, to warn you to look for
> redefinition.
Even better: have your IDE input them for you. Eclipse 3.3 JDT has
‘save actions’ which, amongst others, do this. Of course, they only do
that at saving and if it is possible, so if you have a function as Eric
describes already, it won’t be inserted.
I have Eclipse format, add ‘final’ everywhere, remove unused local
params, add ‘this’ everywhere, qualify static accesses, organize imports
and surround arguments to ‘if’ and ‘while’ with brackets if there aren’t
any.
> --
Roedy, your signature delimiter is incorrect: it should be dash dash
space, otherwise the mail/news readers won’t recognize it.
H.

Signature
Hendrik Maryns
http://tcl.sfs.uni-tuebingen.de/~hendrik/
==================
http://aouw.org
Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html
Roedy Green - 14 Mar 2008 03:43 GMT
On Wed, 12 Mar 2008 10:58:41 +0100, Hendrik Maryns
<gtw37bn02@sneakemail.com> wrote, quoted or indirectly quoted someone
who said :
>Roedy, your signature delimiter is incorrect: it should be dash dash
>space, otherwise the mail/news readers wont recognize it.
should be correct now. I think that was a foolish convention since you
can't proofread it by eye, and because so many editors "correct" it
for you quietly.

Signature
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
Arne Vajhøj - 13 Mar 2008 02:52 GMT
> Perhaps the designer's of Java's successor
> (e.g. Fortress) might be able to correct that.
I am not convinced that Fortress will replace Java.
http://en.wikipedia.org/wiki/Fortress_programming_language says:
#It is intended to be a successor to Fortran, with improvements
#including Unicode support and concrete syntax that is similar to
#mathematical notation. The language is not designed to be similar to
#Fortran. Syntactically, it most resembles Scala, Standard ML, and
#Haskell.
Arne