Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Name that anti-pattern

Reply
Thread Tools

Name that anti-pattern

 
 
Antti S. Brax
Guest
Posts: n/a
 
      09-29-2005
I noticed recently something that could qualify as an anti
pattern (it's an instance of "designing for the future"). Please
comment.

I have a class which is not constrained by any interface. It has
a method which returns an InputStream. The implementation of the
class always returns a ByteArrayInputStream. Because the
designer of the class does not let me know the full capabilities
of the returned object I have to treat the returned object as a
plain InputStream and cannot take advantage of the information
that comes with a ByteArrayInputStream (mainly the available()
method).

Because of this I either have to use a lot of instanceof or end
up with an inefficient solution.

The designer of the class prepared for the situation where his
own implementation changes. The implementation never changed.
Poorly performing code resulted.

--
Antti S. Brax Rullalautailu pitää lapset poissa ladulta
http://www.iki.fi/asb/ http://www.cs.helsinki.fi/u/abrax/hlb/

[1385 messages expunged from folder "Spam"]
 
Reply With Quote
 
 
 
 
Thomas Schodt
Guest
Posts: n/a
 
      09-29-2005
Antti S. Brax wrote:

> I have a class which is not constrained by any interface. It has
> a method which returns an InputStream. The implementation of the
> class always returns a ByteArrayInputStream. Because the
> designer of the class does not let me know the full capabilities
> of the returned object I have to treat the returned object as a
> plain InputStream and cannot take advantage of the information
> that comes with a ByteArrayInputStream (mainly the available()
> method).


Your example breaks for me - as InputStream.available() exists.
 
Reply With Quote
 
 
 
 
Roedy Green
Guest
Posts: n/a
 
      09-29-2005
On Thu, 29 Sep 2005 09:42:47 +0100, Thomas Schodt
<(E-Mail Removed)> wrote or quoted :

>ByteArrayInputStream (mainly the available()
>> method).


In a similar way getURLConnection will return an HTTPConnection. If
you KNOW that absolutely, you can cast the result to an HTTPConnection
and use the extended methods.

If you were wrong, you will get a cast exception.


--
Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.
 
Reply With Quote
 
Antti S. Brax
Guest
Posts: n/a
 
      09-29-2005
http://www.velocityreviews.com/forums/(E-Mail Removed)lid wrote in comp.lang.java.programmer:
> Antti S. Brax wrote:
>
>> I have a class which is not constrained by any interface. It has
>> a method which returns an InputStream. The implementation of the
>> class always returns a ByteArrayInputStream. Because the
>> designer of the class does not let me know the full capabilities
>> of the returned object I have to treat the returned object as a
>> plain InputStream and cannot take advantage of the information
>> that comes with a ByteArrayInputStream (mainly the available()
>> method).

>
> Your example breaks for me - as InputStream.available() exists.


It breaks for you because you don't know what available()
does.

--
Antti S. Brax Rullalautailu pitää lapset poissa ladulta
http://www.iki.fi/asb/ http://www.cs.helsinki.fi/u/abrax/hlb/

[1385 messages expunged from folder "Spam"]
 
Reply With Quote
 
iamfractal@hotmail.com
Guest
Posts: n/a
 
      09-29-2005

Interesting.

I don't know what available() does either, but I'd be surprised if its
behaviour changes just because its object is re-cast.

Of course, I must be missing something here.

Maybe it's an antti-pattern

..ed

--
www.EdmundKirwan.com - Home of The Fractal Class Composition.

 
Reply With Quote
 
Thomas Schodt
Guest
Posts: n/a
 
      09-29-2005
Antti S. Brax wrote:
> It breaks for you because you don't know what available()
> does.
>


Returns a positive non-zero value if a read() posted now can be
guaranteed to be able to read that many bytes without blocking.

Returns 0 (zero) if no guarantees can be made
that a read() posted now would not block or return EOF.


Anyway, I am sure you know that
*if* the object happens to be a ByteArrayInputStream,
you *will* get the result of ByteArrayInputStream.available(),

so I'm struggling to see what the problem is.

The javadoc for InputStream says
public abstract class InputStream
The available() method for class InputStream always returns 0.
This method should be overridden by subclasses.

You will always be dealing with a subclass and the subclass should
always override the available() method.
 
Reply With Quote
 
Chris Smith
Guest
Posts: n/a
 
      09-29-2005
<(E-Mail Removed)> wrote:
> Interesting.
>
> I don't know what available() does either, but I'd be surprised if its
> behaviour changes just because its object is re-cast.
>
> Of course, I must be missing something here.


Sure. The ByteArrayInputStream class fulfills all the requirements of
the superclass (i.e., it returns the number of bytes that can be read
without blocking)... but it also makes the additional guarantee that it
returns the number of bytes remaining in the complete stream. Antti
wants to use that guarantee... and although the InputStream would act
that way without a cast, it would be very wrong to rely on the
guarantees made by a ByteArrayInputStream without verifying that the
reference is of that type.

I don't see this as an anti-pattern. The original author made a trade-
off between flexibility and performance. If Antti doesn't like it, then
fine. But that doesn't mean that we can categorize that whole family of
decisions as being a bad idea.

--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
Reply With Quote
 
Antti S. Brax
Guest
Posts: n/a
 
      09-30-2005
(E-Mail Removed) wrote in comp.lang.java.programmer:
> I don't see this as an anti-pattern. The original author made a trade-
> off between flexibility and performance.


But think for a second what flexibility was traded off? Surely a
ByteArrayInputStream can be used where an InputStream is needed,
but not vice versa. So no flexibility for the class user was
added (quite the opposite, flexibility was lost). The reason why
the author used InputStream was because he expected the
implementation to change (which never happened). In this question
changing the implementation to something else would have required
a massive redesign.

Or then the author was just having "OOP blindness". In other
words, used InputStream because every _interface_ he has seen
uses it.

> But that doesn't mean that we can categorize that whole family of
> decisions as being a bad idea.


Please describe the "family of decisions" you are talking about.
I think you understood me incorrectly and would like to correct
it.

--
Antti S. Brax Rullalautailu pitää lapset poissa ladulta
http://www.iki.fi/asb/ http://www.cs.helsinki.fi/u/abrax/hlb/

[1385 messages expunged from folder "Spam"]
 
Reply With Quote
 
Antti S. Brax
Guest
Posts: n/a
 
      09-30-2005
(E-Mail Removed)lid wrote in comp.lang.java.programmer:
> Anyway, I am sure you know that
> *if* the object happens to be a ByteArrayInputStream,
> you *will* get the result of ByteArrayInputStream.available(),
>
> so I'm struggling to see what the problem is.


Chris Smith just described it very well: "it also makes the
additional guarantee that it returns the number of bytes
remaining in the complete stream."

--
Antti S. Brax Rullalautailu pitää lapset poissa ladulta
http://www.iki.fi/asb/ http://www.cs.helsinki.fi/u/abrax/hlb/

[1385 messages expunged from folder "Spam"]
 
Reply With Quote
 
Thomas Schodt
Guest
Posts: n/a
 
      09-30-2005
Chris Smith wrote:
> The ByteArrayInputStream class fulfills all the requirements of
> the superclass (i.e., it returns the number of bytes that can be read
> without blocking)... but it also makes the additional guarantee that it
> returns the number of bytes remaining in the complete stream. Antti
> wants to use that guarantee...


I cannot find that guarantee in the Javadoc.
Yes, perusing the source, it is obvious it is there.

In this case it is highly unlikely you would ever come across an
implementation that does not make the same guarantee,
but relying on undocumented implementation specific behaviour
can often come back and bite you.
Almost makes me think it might be an anti-pattern...
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
adding a variable name to a hash to name is part of the variable name Bobby Chamness Perl 2 04-22-2007 09:54 PM
print("my name is {name}, and {age}-year old {gender}", name, age, gender); =?iso-8859-1?B?bW9vcJk=?= Java 7 01-02-2006 04:39 PM
IE name="name" & form.name property bug Java script Dude Javascript 5 06-30-2004 03:07 AM
name = name.substring(0, name.lastIndexOf('.')); Help please Jack-2 Javascript 3 12-24-2003 04:39 PM
Re: Urgent! how to get object name, method name and attribute name based on the strings? ding feng C++ 2 06-25-2003 01:18 PM



Advertisments