Re: java method returning an array of 2 integers
- From: "Monique Y. Mudama" <spam@xxxxxxxxxxxxxxxx>
- Date: Thu, 8 Sep 2005 11:15:57 -0600
On 2005-09-08, Mark Haase penned:
> In article <431f1f6a$0$22917$ed2619ec@xxxxxxxxxxxxxxxxxxxxxxxxxx>,
> Thomas Hawtin <usenet@xxxxxxxxxxxxxxxxx> wrote:
>
>> In general it's better to write you program to avoid this sort of
>> thing. Possible solutions include splitting the method into two,
>> creating an type that contains both values or passing an object to
>> the method which can then have the values applied to it.
>
> Thomas is right, because there's no way to enforce the contract!
>
> What if somebody extends your class and overrides the method to
> return an array with length 1? Then your code gets an
> ArrayIndexOufOfBoundsException and it dies. You could try/catch the
> calling method, but its much smarter to provide an object which is
> dedicated to this.
Well, not if you're iterating through the return values to the length
of the array.
In stead of saying "it's better in general to avoid this," I would
suggest a rule. If the order of the return value is intended to convey
the nature of the thing (0th represents width, 1st represents height,
2nd represents depth), it would be better to use an object containing
fields width, height, and depth. If the return value just represents a
list of things and the order doesn't imply its nature, ie a list of
students on the school bus today, an array is fine.
Hrm, once again I've said something whose intention was clear to me,
but resulted in hopelessly muddled text. I should start writing books
on philosophy.
--
monique
Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html
.
- References:
- java method returning an array of 2 integers
- From: Thanasis \(sch\)
- Re: java method returning an array of 2 integers
- From: Oliver Wong
- Re: java method returning an array of 2 integers
- From: Thanasis \(sch\)
- Re: java method returning an array of 2 integers
- From: Allan Bruce
- Re: java method returning an array of 2 integers
- From: Thomas Hawtin
- java method returning an array of 2 integers
- Prev by Date: Re: I Need to Rename a Lot of Classes -- Any Tips (Also moving from Jave 2 to Java 5)
- Next by Date: Re: J2ME and Bluetooth
- Previous by thread: Re: java method returning an array of 2 integers
- Next by thread: Re: java method returning an array of 2 integers
- Index(es):
Relevant Pages
|