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

A

antonyliu2002

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.
 
J

John C. Bollinger

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?

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.
 
P

Patricia Shanahan

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.

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
 
C

Chris Uppal

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top