SonarLint x Java GC, cancel or not object at the end of execution?

5

I'm passing SonarLint on a somewhat old application and fixing several "problems", but I came across a code snippet like:

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 accused the following error for 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, this makes sense and in other code points where this happened I removed the assignment.

But at the time of Java 5, Java 6 (or many companies today), it was common for more advanced developers to ask for things like "put final at all", "always start an array with a size "and" overrides objects before exiting the method, "the latter being to save memory and make the

asked by anonymous 25.07.2018 / 13:14

1 answer

7

This type of utility does not understand the context of the application. If you use that and do not understand what you're doing, it will start spoiling the application as much as it will improve.

In this case he is covered in reason, who made this null does not understand how the code works. It is not necessary to void anything in the code, except in a static variable, which at some point no longer needs to have stored content. But if that's the case, the error is probably having this variable be static. The affirmation of the question is a non-sense . No matter the size of it.

If you consider these developers more advanced, I regret having bad influences. In fact the vast majority of programmers are bad and do not understand what they are doing, worse when these people influence others. So always look for reliable sources, verifiable by several qualified people.

Collection will occur whenever required on all objects that have no reference to it. What you're doing there is this:

public void fazerAlgo(final String param) {
    int x = 1;
    System.out.println(x);
    x = 0;
}

Do you think this is necessary? It has the same effect.

The best thing about this question is that I can say to give up good practice. Most people do not understand them and use them as cake recipes. If you do not understand why you are having it done in depth, do not use it! And I say, most good practices are simply wrong because they were created by those who do not understand what they are doing. And even in cases where they make sense, but only in a specific context. Its context may be different. I have been giving lectures showing how the lack of context has caused atrocities in organizations.

    
25.07.2018 / 15:41