Path: csiph.com!eternal-september.org!feeder.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Eric Sosman Newsgroups: comp.lang.java.programmer Subject: Re: Good pattern for realizing the out-param capability in Java? Date: Wed, 14 Sep 2016 17:25:01 -0400 Organization: A noiseless patient Spider Lines: 31 Message-ID: References: <8959892d-cd75-4680-b836-260a971f3366@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit Injection-Date: Wed, 14 Sep 2016 21:25:08 -0000 (UTC) Injection-Info: mx02.eternal-september.org; posting-host="3c2d73b4e7d1c9c00b5411ebb3357b4a"; logging-data="14755"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/CSU6LwisbiNX8F1lCl70m" User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:45.0) Gecko/20100101 Thunderbird/45.3.0 In-Reply-To: <8959892d-cd75-4680-b836-260a971f3366@googlegroups.com> Cancel-Lock: sha1:o/cz+ZI2HgyyYtvcsxmEB+slVr8= Xref: csiph.com comp.lang.java.programmer:35606 On 9/14/2016 3:54 PM, Eric Douglas wrote: > On Wednesday, September 14, 2016 at 12:17:37 PM UTC-4, sande...@yahoo.com wrote: >> I want to solicit the opinion of the Java experts about realizing the out-param capability in Java. Suppose there is a method in a class where I would like to realize the following: >> >> class MyClass { >> int myMethod(int inIntParam, int outIntParam, double outDoubleParam) { >> // Accepts inIntParam as input >> // Updates outIntParam and outDoubleParam >> // Returns an int >> … >> } >> … >> } > > Methods don't normally need to pass out more than one value, so when they do I either create a custom object or pass out an array. What advantage would you get from creating a generic custom class to pass out 2 values? I disagree with the premise: It is fairly common for a computation to generate more than one result. On the other hand, I agree with the approach: It is (I feel) better to return the multiple results in a single instance of a "holder" class. java.awt.Shape's getBounds() method returns its *four* outputs in a java.awt.Rectangle instance, and there are many similar examples among the standard classes. Occasionally, though, the "out-param" simply cannot be avoided. The various sort() methods of java.util.Arrays, for example, would be a good deal less attractive if they left their argument arrays unmodified and instead returned brand-new arrays as method results ... -- esosman@comcast-dot-net.invalid The Tooth Fairy has gone high-tech, now pays in Bitecoin.