Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

which one to choose between calling a function twice and storing the return value in a variable?

I have the following scenario.. and I come across the similar scenario many a times. Which is more preferable of the following two options?

Option-1:

String result = ( getDetails(...) == null ) ? "" : getDetails(...);

Option-2:

String returnValue = getDetails(...);
String result = ( returnValue == null ) ? "" : returnValue;

Which is more preferable and/or a good practice.?

like image 451
subbu Avatar asked Jul 12 '13 11:07

subbu


People also ask

What happens if you call a function with a return value without storing or using the returned value?

Yes, You can call a function but never store the returned value by that function in java. But it will just unnecessarily consume processing time. Because you will not be achieving what you want to achieve by first declaring that you want some processing to be done by method and return a final output.

How do you call a method that returns a value?

To use the return value when calling a method, it must be stored in a variable or used as part of an expression. The variable data type must match the return type of the method.

Can you call a function multiple times in python?

A Function is the Python version of the routine in a program. Some functions are designed to return values, while others are designed for other purposes. We pass arguments in a function, we can pass no arguments at all, single arguments or multiple arguments to a function and can call the function multiple times.


1 Answers

Imho The second one is better because it's avoiding calling getDetails(...) method twice.

like image 187
Suresh Atta Avatar answered Nov 04 '22 14:11

Suresh Atta