Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!newsfeed.utanet.at!newscore.univie.ac.at!aconews-feed.univie.ac.at!aconews.univie.ac.at!not-for-mail Newsgroups: comp.lang.java.programmer From: Andreas Leitgeb Subject: Re: looping through a list, starting at 1 References: <4e3745a2$0$305$14726298@news.sunsite.dk> <62239393-929c-4764-8c8e-9620a03a7b81@c29g2000yqd.googlegroups.com> <80346568-647c-4e27-8192-33e1765a09ce@glegroupsg2000goo.googlegroups.com> Reply-To: avl@logic.at User-Agent: slrn/pre0.9.9-111 (Linux) Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Message-ID: Date: 10 Aug 2011 22:50:54 GMT Lines: 46 NNTP-Posting-Host: gamma.logic.tuwien.ac.at X-Trace: 1313016654 tunews.univie.ac.at 71616 128.130.175.3 X-Complaints-To: abuse@tuwien.ac.at Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:6995 Arved Sandstrom wrote: > On 11-08-10 06:39 PM, Andreas Leitgeb wrote: >> Lew wrote: >>> Volker Borchert wrote: >>>> if (l instanceof RandomAccess) { >>> Tests on type like this are an antipattern. >>> Like many antipatterns there are occasions when one might >>> consider its use anyway, but it's a red flag that we're >>> probably going about things the wrong way. >> >> Are marker-interfaces (which RandomAccess is, iirc) already an >> antipattern, or is there a different way to check for them, >> or are marker-interfaces just one of the occasions where one >> would just acknowledge and consciously ignore the red flag? >> >> PS: I fully agree with your judgement *outside* the context >> of marker-interfaces, and am eager to learn *inside* that >> context. > > I don't see that marker interfaces are an anti-pattern. I believe that > Bloch's comments with respect to marker interfaces (Item 37 in Effective > Java) make sense: as he points out one ought to consider a marker > interface to be a type definition. That doesn't make sense to me... I can trivially create a class that inherits Object and implements RandomAccess and doesn't offer anything of the Collection-stuff. 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.) Lew's argument absolutely makes sense for other types: Rather than checking for List l, whether (l instanceof ArrayList) I'd rather make one overload for ArrayList and another one for List. But I cannot do something like that to separate List&RandomAccess from List. It's not the "no-method"-ness of marker interfaces that matters here. They are just a trick to implement what could otherwise be implemented with a getter: bool supportsRandomAccess(). As they don't do it with a method but with an interface, the syntactic way to obtain that "flag" is using instanceof. I don't see why this would be red-flagged without red-flagging that concept of marker-interfaces altogether.