3
I am passing Sonarlint in a somewhat old application and fixing several "problems", but I came across a code snippet similar to:
public void fazerAlgo(final String param) {
MeuObjeto m = new MeuObjeto();
m.setVar(param.toLowerCase());
myBusiness.atualizarAlgumaCoisa(m);
m = null; // <- Para liberar memória e ajudar o GC
}
And Sonarlint charged the error below for the line m = null
:
Remove this useless assignment to local variable
(Remova essa atribuição inútil à variável local)
The sonar point of view is that this object "m"
is not used after the assignment, so the set
becomes useless, it makes sense and at other code points where it happened I removed the assignment.
However, at the time of Java 5, Java 6 (that is, today in many companies), it was common for more advanced developers to request things like "put final
in everything", "always start a array with a size" and "cancels objects before exiting the method", the latter being to save memory and do the Garbage Collector Java clean this object faster.
Which of the two is more right? Should I keep the m = null
, because where this code is happening the objects are very large, or I should remove this assignment because it does not influence anything in the performance of the application and Sonarlint is always right?