Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #7020

Re: looping through a list, starting at 1

From Lew <lewbloch@gmail.com>
Newsgroups comp.lang.java.programmer
Subject Re: looping through a list, starting at 1
Date 2011-08-11 07:14 -0700
Organization http://groups.google.com
Message-ID <ad5e70fc-8d7b-47a7-83ff-50ec72b71faa@glegroupsg2000goo.googlegroups.com> (permalink)
References (5 earlier) <slrnj45uke.6gl.avl@gamma.logic.tuwien.ac.at> <DlD0q.35597$g12.31437@newsfe20.iad> <slrnj462qe.6gl.avl@gamma.logic.tuwien.ac.at> <fNN0q.575745$SG4.165292@newsfe03.iad> <slrnj47fmu.6gl.avl@gamma.logic.tuwien.ac.at>

Show all headers | View raw


On Thursday, August 11, 2011 4:37:02 AM UTC-7, Andreas Leitgeb wrote:
> Arved Sandstrom <asandstr...@eastlink.ca> wrote:
> > On 11-08-10 07:50 PM, Andreas Leitgeb wrote:
> >> How would I write a method (overload) that would only take an
> >> object if it is both List and RandomAccess? (Afaik: no way, but
> >> I might perhaps miss something.)
> > Pretty much in the standard way, which would take advantage of the
> > marker interface in a way suggested by Bloch: you create an interface
> > that extends List and RandomAccess, and concrete classes implement that.
> 
> That's just not applicable to the Collections framework as it is:
> We've all been taught to use interface-names for the static type
> of variables to hold whatever concrete implementation:
>   ArrayList l = new ArrayList();  // discouraged
>   List l = new ArrayList();       // encouraged

No, that's not what we've been taught.  We've been taught to use the type that most generally allows the compiler to enforce the specific contract we need.  That does not always mean use the interface (not "names"), nor the most general interface.  Sometimes you do want to peg a variable or return type to a specific implementation, or a specific interface, or an interface that mixes in two parent interfaces per Bloch.  Don't make a religion of "use the (most general) interface" - the actual principle is "use the type that guarantees your contract".  So 'RandomList extends List, RandomAccess' is perfectly acceptable.
... 
> That's of course just rambling.  Marker-interfaces just aren't used that
> way, but instead they are always checked at runtime. So, either Marker-

Nope.  Their value is when they're checked *at compile time*.

It's the use at run time that is suspect.

> interfaces (such as RandomAccess) are an anti-pattern themselves, or
> that pattern is one declared exception to the "instanceof"-red flag.

Marker interfaces are not an antipattern.  Use of 'instanceof' is, often.

Use marker interfaces for *compile time* safety.

-- 
Lew

Back to comp.lang.java.programmer | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

Re: looping through a list, starting at 1 Arne Vajhøj <arne@vajhoej.dk> - 2011-08-01 20:32 -0400
  Re: looping through a list, starting at 1 Robert Klemme <shortcutter@googlemail.com> - 2011-08-02 02:42 -0700
    Re: looping through a list, starting at 1 v_borchert@despammed.com (Volker Borchert) - 2011-08-02 19:32 +0000
      Re: looping through a list, starting at 1 Lew <lewbloch@gmail.com> - 2011-08-10 09:00 -0700
        Re: looping through a list, starting at 1 Andreas Leitgeb <avl@gamma.logic.tuwien.ac.at> - 2011-08-10 21:39 +0000
          Re: looping through a list, starting at 1 Arved Sandstrom <asandstrom3minus1@eastlink.ca> - 2011-08-10 19:10 -0300
            Re: looping through a list, starting at 1 Andreas Leitgeb <avl@gamma.logic.tuwien.ac.at> - 2011-08-10 22:50 +0000
              Re: looping through a list, starting at 1 Arved Sandstrom <asandstrom3minus1@eastlink.ca> - 2011-08-11 07:02 -0300
                Re: looping through a list, starting at 1 Andreas Leitgeb <avl@gamma.logic.tuwien.ac.at> - 2011-08-11 11:37 +0000
                Re: looping through a list, starting at 1 Lew <lewbloch@gmail.com> - 2011-08-11 07:14 -0700
                Re: looping through a list, starting at 1 Andreas Leitgeb <avl@gamma.logic.tuwien.ac.at> - 2011-08-11 16:18 +0000
                Re: looping through a list, starting at 1 Arved Sandstrom <asandstrom3minus1@eastlink.ca> - 2011-08-11 17:39 -0300
                Re: looping through a list, starting at 1 Andreas Leitgeb <avl@gamma.logic.tuwien.ac.at> - 2011-08-12 08:09 +0000
                Re: looping through a list, starting at 1 supercalifragilisticexpialadiamaticonormalizeringelimatisticantations <supercalifragilisticexpialadiamaticonormalizeringelimatisticantations@averylongandannoyingdomainname.com> - 2011-08-11 09:52 -0400
                Re: looping through a list, starting at 1 Arved Sandstrom <asandstrom3minus1@eastlink.ca> - 2011-08-11 17:26 -0300
                Re: looping through a list, starting at 1 supercalifragilisticexpialadiamaticonormalizeringelimatisticantations <supercalifragilisticexpialadiamaticonormalizeringelimatisticantations@averylongandannoyingdomainname.com> - 2011-08-11 20:11 -0400
          Re: looping through a list, starting at 1 Lew <lewbloch@gmail.com> - 2011-08-10 20:31 -0700
            Re: looping through a list, starting at 1 Andreas Leitgeb <avl@gamma.logic.tuwien.ac.at> - 2011-08-11 16:07 +0000
              Re: looping through a list, starting at 1 Lew <lewbloch@gmail.com> - 2011-08-11 09:20 -0700
                Re: looping through a list, starting at 1 Andreas Leitgeb <avl@gamma.logic.tuwien.ac.at> - 2011-08-11 18:03 +0000
                Re: looping through a list, starting at 1 Lew <lewbloch@gmail.com> - 2011-08-11 12:55 -0700
                Re: looping through a list, starting at 1 Andreas Leitgeb <avl@gamma.logic.tuwien.ac.at> - 2011-08-12 08:32 +0000
                Re: looping through a list, starting at 1 supercalifragilisticexpialadiamaticonormalizeringelimatisticantations <supercalifragilisticexpialadiamaticonormalizeringelimatisticantations@averylongandannoyingdomainname.com> - 2011-08-12 09:09 -0400
                Re: looping through a list, starting at 1 Andreas Leitgeb <avl@gamma.logic.tuwien.ac.at> - 2011-08-12 14:38 +0000
                Re: looping through a list, starting at 1 Andreas Leitgeb <avl@gamma.logic.tuwien.ac.at> - 2011-08-12 14:51 +0000

csiph-web