Re: java method returning an array of 2 integers



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
.



Relevant Pages

  • Re: getElementById returns null
    ... Don't mix Array() and Array initializers. ... Avoid repeated property access; ... know where the automatic ones happen -- that avoids this comb-like posting ... avoid posting using Google Groups if you ...
    (comp.lang.javascript)
  • Re: CPU and Memory performance
    ... If you program really needs to shuffle a lot of data, high CPU usage could be normal. ... Don't constantly resize large arrays. ... Place small wait statements in all toplevel loops. ... Avoid overlapping objects on the front panel. ...
    (comp.lang.labview)
  • Re: getElementById returns null
    ... Don't mix Array() and Array initializers. ... Avoid repeated property access; ... Don't use spacer elements, use CSS margins/paddings instead. ... know where the automatic ones happen -- that avoids this comb-like posting ...
    (comp.lang.javascript)
  • Re: is it ok to return null?
    ... But the goal should not necessarily be to avoid an exception. ... That's not to say I don't agree with returning an empty array from a ... by the advice to return an empty string to avoid a null. ...
    (comp.lang.java.help)
  • Re: Who killed two hikers near Mt. Pilchuck, Washington?
    ... but I avoid certain situations that are unsafe these days. ... higher likelihood of danger than staying home and watching TV would ... While a solo hitchhiker ... nature of nature and / or my own carelessness or lack of knowledge. ...
    (alt.true-crime)