Search code examples
return-valueconceptmethod-call

Assign return value to variable warning


In netbeans I use to call a method that returns a value, but I am directly calling it where I have to pass a parameter to the function i.e. Function(getValue()) where getValuue() returns String. So what I want to know is that what is more efficient way to call this method whether should I assign value to a string first and then pass that value to parameter, as netbeans suggests me and shows a warning there, or calling it directly is good? I know code runs fine but keeping in mind the efficiency or rules of coding should I consider this thing or not? Or how badly can it effect if I ignore it?


Solution

  • If you are only using that value once, then calling it directly where it is used as a parameter is fine.

    In Java, this is fine:

    MyClass myClass = new MyClass();
    myFunction(myClass.getSomeValue());
    

    Whereas in the following case:

    MyClass myClass = new MyClass();
    MyOtherClass myOtherClass = myClass.someLongComputation();
    Int value = myFunction(myOtherClass);
    anotherFunction(value, myOtherClass);
    

    It may be better to have a local variable so that you avoid calling a long running calculation twice. However, for simple getValue()s, it really doesn't matter.