I always had the doubt between whether it is better to create a object
and then use a method of that object or not to create the object, examples:
- Without having created an object of the class that would return
otroMetodo()
:
Sistema.algunMetodo().otroMetodo().print(algo, "Hola");
Sistema.algunMetodo().otroMetodo().print(algo, "¿Cómo");
Sistema.algunMetodo().otroMetodo().print(algo, "Estás?");
- With an object:
ClassDeOtroMetodo claseDeOtroMetodo = Sistema.algunMetodo().otroMetodo();
claseDeOtroMetodo.print(algo, "Hola");
claseDeOtroMetodo.print(algo, "¿Cómo");
claseDeOtroMetodo.print(algo, "Estás?");
Is there a big difference or is it the same?
It mainly depends on whether the output of the methods will be the same or not on successive calls. If they can vary, then it is necessary to do the first. It also depends on where you define
ClassDeOtroMetodo claseDeOtroMetodo;
in case of a very high use of times (it is better outside of a loop than inside, or as a private variable of the class than as a local of a method, etc).Going into details:
We are executing roughly (the most expensive):
T1
Sistema
: Call the calling methodalgunMetodo()
. Returns the instance of an object.T2
: Call the methodotroMetodo()
of the instance returned by the previous call. Returns the instance of an object.T3
: Call the methodprint()
of the instance returned by the previous call.This is seen repeated three times
3·(T1 + T2 + T3) = 3·T1 + 3·T2 + 3·T3
. The return values of a method cannot be optimized, they must be called over and over again.Storing the result of the first two calls:
We are doing on the first line
T1 + T2
and then repeat three timesT3
:T1 + T2 + 3·T3
.Variable and instance access times and assignments are negligible with respect to method call times.
empirical measures
I have developed this little proof of concept. In the code I'm cheating a bit because I get the response value of
algunMetodo()
during the constructor execution, that's why the differences are so small. As a general rule, a method must do some kind of treatment of its parameters or the state of the class to return a result.Result of two consecutive runs:
As can be seen there is a considerable gain between using the temporary variable, even having it inside the loop (although performance is slightly reduced).
Neither way is better or worse, that completely depends on the use case and context. Also in both ways you create an object, the difference is whether you store the reference in a variable or not.
That means that you not only have a difference in the form, you also have an ambiguity in the theme if both codes do the same thing. Why?
This is the interface that returns the object you are working with. What is not known is whether this method returns a
Singleton
, meaning that it would always guarantee you the same object with each execution, or whether the method prompts you for a new object with each call.In the first case, your codes would do the same, in the second case not.
Understood?