Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / March 2006

Tip: Looking for answers? Try searching our database.

If method B uses a value returned by method A .....

Thread view: 
antonyliu2002@yahoo.com - 10 Mar 2006 03:31 GMT
I've been puzzled by this for a long time.

Say if I define a method as follows:

// methodA returns a String

public String methodA(some arguments)
{  // do something and return a String
}

Now suppose, another method (methodB) will use the String returned from
methodA.  Should I call methodA inside methodB in order to get the
String, or should I call methodA outside methodB (in the main) and pass
the returned value to methodB as an argument?

Which approach is considered better according to you professional
programmers/developers?  What are the differences?

Thanks.
John C. Bollinger - 10 Mar 2006 05:16 GMT
> I've been puzzled by this for a long time.
>
[quoted text clipped - 13 lines]
> Which approach is considered better according to you professional
> programmers/developers?  What are the differences?

Neither approach is inherently better than the other.  Which one you
should use depends somewhat on the details of methodA's and methodB's
designs, and somewhat on your stylistic preferences.  For instance, if
method doesn't have enough information to choose appropriate arguments
with which to invoke methodA, then it shouldn't need to do.  If methodA
is expensive to invoke but idempotent, and method is invoked frequently,
then it makes sense to invoke methodA once, and give the result to
method each time.  On the other hand, if use of methodA can reasonably
be construed as an implementation detail of method, then it probably
should remain inside method.  This is not an exhaustive list of scenarios.

Signature

John Bollinger
jobollin@indiana.edu

Patricia Shanahan - 10 Mar 2006 05:42 GMT
> I've been puzzled by this for a long time.
>
[quoted text clipped - 15 lines]
>
> Thanks.

There isn't one right answer that applies in all cases. Is calling
methodB the only use methodA can possibly have? Is a methodA result the
only possible parameter value for methodB? Does the combined method make
sense?

In general, you want a method to be a cohesive unit, doing one task,
with the simplest possible interconnection with the rest of the program.

There is a practical test that works quite well. Try to name each
method, and write a precise comment describing what each does. If you
have a good design, they will have obvious natural identifiers and it
will be easy to write nice, simple, clear descriptions of at least the
basic intent of each method. If you have trouble writing the comments,
and especially if you find yourself wanting to use "and" in an
identifier, you have a problem.

In any case, don't worry too much about this unless you are defining an
interface that will be so widely used that refactoring is not possible.
If it is just within your program, do whatever makes most sense now, but
be prepared to change your mind and reorganize it later. The combination
of hindsight and good software development tools can be very powerful.

Patricia
Chris Uppal - 10 Mar 2006 08:12 GMT
> Now suppose, another method (methodB) will use the String returned from
> methodA.  Should I call methodA inside methodB in order to get the
> String, or should I call methodA outside methodB (in the main) and pass
> the returned value to methodB as an argument?

Just to add to what Patricia and John have already said:

It's pretty rare for the choice to be open in any particular example.  Normally
(not always, but normally) it's clear whether methodB() has to use the result
of methodA() /specifically/ or whether it should use just any old String and
doesn't care where it comes from (even though the caller both knows and cares
that it comes from methodA()).

   -- chris


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.