Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!news-1.dfn.de!news.dfn.de!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail From: Robert Klemme Newsgroups: comp.lang.java.programmer Subject: Re: Usefulness of "final" (Was: Re: Inserting In a List) Date: Wed, 03 Apr 2013 19:08:34 +0200 Lines: 73 Message-ID: References: <19un43xj77bua.vw45l4e2wshi.dlg@40tude.net> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Trace: individual.net jwrVDlA4e9Qw/vnrbEftsgaHm/kPoLD4IYhG0946ovmeNM9DY= Cancel-Lock: sha1:gzA42kgpKdLVesy0eBeMICVzeos= User-Agent: Mozilla/5.0 (Windows NT 6.0; WOW64; rv:17.0) Gecko/20130307 Thunderbird/17.0.4 In-Reply-To: X-Antivirus: avast! (VPS 130403-1, 03.04.2013), Outbound message X-Antivirus-Status: Clean Xref: csiph.com comp.lang.java.programmer:23221 On 03.04.2013 12:15, lipska the kat wrote: > On 02/04/13 20:06, Robert Klemme wrote: >> On 04/02/2013 04:08 PM, lipska the kat wrote: >> >>> Just as a matter of interest what's with all the finals >>> >>> particularly >>> >>> for (final File name : folder.listFiles()) > > [snip] > >> I believe in using "final" pretty often as it will immediately indicate >> which local variables are constant for a method call and which are >> modified all the time. Plus, with "final" you can easier catch errors >> in control flow: >> >> final String x; >> >> if ( someCondition() ) { >> x = y.toString(); >> } >> else { >> if ( someOtherCondition() ) { >> x = "foo"; >> } >> // forgot the else branch here >> x = "bar"; >> } >> >> System.out.println("We got " + x); >> >> Generally I find "finally" quite useful - apparently significantly more >> useful than you do. :-) > > Well I'm not sure that using a storage class to help you write a > conditional statement is 'good programming style' but hey ho, different > strokes for different folks :-) I am not sure what you mean by that. Can you elaborate? Where's the storage class in the example above? > Anyway, the usability of final depends on your point of view I suppose. We can certainly agree on *that*. > If for some reason I find myself using 'final' all over the place then I > would have to ask myself if my abstraction was coherent. If one has > something, or in fact a number of somethings that need 'protecting' in > this way then surely it is better to wrap them up in a component and > control access by virtue of the public interface of that component. It probably depends. Sometimes you want to hold on to something because obtaining it is expensive or the accessor might return a changed version during subsequent calls but you want to be sure to retain a specific status. In those cases I would not think that wrapping it up necessarily helps because the data may actually have been wrapped already. It feels a bit over the top introducing another layer just to avoid a local variable with "final". > It's more OO, makes for cleaner code and of course provides opportunity > for the holy grail of OO 're-usability' Maybe I could better see (and agree) if you provide a specific example of what you mean here. Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/