Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

findbugs complains about Eclipse's auto-generated code

Here's the hashCode() method that Eclipse 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:

Method ...hashCode() stores return result in local before immediately returning it [Scariest(2), Normal confidence]

Who is right here? Findbugs or Eclipse? Is this dodgy?

I cannot for the life of me see why this is anything for findbugs to get upset about. The code is perfectly clear; storing it in a local before returning it doesn't make it harder to read or harder to maintain; and unless the compiler is very badly written then it won't make any difference to performance either.

And yet this is categorised as Scariest!

Am I missing something?

(Clearly the code could be simplified in some respects, and it's come out that way because as far as Eclipse is concerned, there might have been other fields going into the hash function. But it's specifically the issue of storing a value and then immediately returning it that I'm asking about here, because that's what findbugs is complaining about.)

like image 256
chiastic-security Avatar asked Jan 13 '15 20:01

chiastic-security


People also ask

What is FindBugs based on Eclipse?

It is based on Eclipse 4.6. 1. What is FindBugs FindBugs is an open source project for a static analysis of the Java bytecode to identify potential software bugs. Findbugs provides early feedback about potential errors in the code. This helps the developer to access these problems early in the development phase.

How to customize the bugs analysis strategy in Eclipse?

Eclipse Configuration FindBugs plugin makes it easy to customize the bugs analysis strategy, by offering various ways to filter warning and limit the strictness of the results. You can check the configuration interface by going to Window -> Preferences -> Java -> FindBugs:

How to configure the FindBugs plugin to fail?

The FindBugs plugin can also be configured to fail under some circumstances – by adding the execution goal check to our configuration: The effort – when maxed out, performs a more complete and precise analysis, revealing more bugs in the code, though, it consumes more resources and takes more time to complete.

How do I find bugs in a Java project?

Simply create an empty Java project and attach archives to the project classpath. Having that, you can now right click the archive node in Package Explorer and select the option labeled "Find Bugs". If you additionally configure the source code locations for the binaries, FindBugs will also link the generated warnings to the right source files.


Video Answer


1 Answers

Who is right here? Findbugs or Eclipse? Is this dodgy?

Nighter. Code style is mostly a matter of personal taste. It's useful to have uniform code style across project or even whole world, but as long there is no official guideline or rules generally adopted by whole community there is no better or worse style.

Sometimes some rules have practical benefits. In some cases Eclipse's style is more readable (if you have multiple fields). On the other hand as mentioned by @RomanC firebug's rule have advantage of fitting contract of not using variables if not necessary.

Sometimes some inspection rules in code style software are too officious, but usually you can turn them off or reduce their importance level. Also IDE I'm using usually puts code stale warning on code it itself generates.

Note: Code-style checking tools are configurable. You may enable only those rules which would reduce costs of maintaining code for you, your team or organization.

like image 197
NiematojakTomasz Avatar answered Nov 15 '22 01:11

NiematojakTomasz