Findbugs complains about auto-generated Eclipse code

Here's the hashCode() method that Eclipse has kindly generated for me:

 @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + (int) (id ^ (id >>> 32)); return result; } 

When I run findbugs on this, it complains about the last line:

The method ... hashCode () saves the result of returning to local before returning immediately [Scariest (2), Normal Confidence]

Who is there? Findbugs or Eclipse? Is it dodgy?

I can’t understand for life why this is something for findbugs to get upset. The code is perfectly clear; keeping it in local mode before returning it does not make it difficult to read or more difficult to maintain; and if the compiler is not very poorly written, it will also not affect performance.

And yet it is classified as Scariest !

Did I miss something?

(Obviously, the code can be simplified in some respects, and this is because, with respect to Eclipse, there might be other fields included in the hash function, but in particular the problem of storing the value and then immediately returning it, which I ask here because what the error spoons complain about.)

+7
java eclipse local-variables findbugs
source share
4 answers

Who is there? Findbugs or Eclipse? Is it dodgy?

Nighter. The style of the code mainly depends on personal taste. It is useful to have a uniform code style throughout the project or even the whole world, but as long as there are no official rules or rules that are generally accepted by the whole community, there is no better or worse style.

Sometimes some rules have practical advantages. In some cases, the Eclipse style is more readable (if you have multiple fields). On the other hand, as stated in the firebug @RomanC rule, the advantage is that the contract for the use of variables is adjusted unnecessarily.

Sometimes some validation rules in code style software are too official, but usually you can disable them or reduce the importance level. Also, the IDE that I use usually puts the code in an obsolete warning on the code that it generates.

Note. Code style checking tools are customizable. You can include only those rules that will reduce the cost of maintaining the code for you, your team or organization.

+1
source share

The code should be simplified as it does unnecessary things that you reported to firebug. This inspection is valid.

 @Override public int hashCode() { return 31 + (int) (id ^ (id >>> 32)); } 
0
source share

Eclipse code is not so bad, but it is too clumsy. In such trivial cases, it is often better to write this manually. Your code is essentially equivalent to this:

 @Override public int hashCode() { return Long.hashCode(id); } 
0
source share

This is less about simplification and more about the possibility that you have forgotten something. If you save the result, then this is most likely because you want to do something with it. If not, you will simply return it.

FindBugs warns you that you most likely wanted to do something with the saved value, but you probably forgot.

 public int hashCode() { final int prime = 31; int result = 1; result = prime * result + (int) (id ^ (id >>> 32)); // I meant to do something more here with result, but forgot... return result; } 
0
source share

All Articles