7

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.)

chiastic-security
  • 20,430
  • 4
  • 39
  • 67
  • I would argue that unnecessary lines DO make code harder to read and maintain - FindBugs feels the same way. – colti Jan 13 '15 at 20:57
  • This post is similar to what you are asking and has some more answers that are not listed here. http://stackoverflow.com/questions/15078153/storing-value-before-return-in-a-variable – austin wernli Jan 13 '15 at 21:05
  • 1
    I can't help but notice that `findbugs` is complaining about code that could be made simpler by just returning the expression directly. But it apparently thinks that multiplying by a variable that is known to be 1 is just peachy--nothing un-simple about that! My gut feeling is that trying to use an automated tool to detect style problems in code generated by another automated tool shouldn't be done except for amusement. – ajb Jan 13 '15 at 21:21
  • 4
    Findbugs is seldom "right" about anything; it just highlights things that don't meet the configured criteria. In my experience, FB needs so much tweaking to be practical that it becomes, well, impractical. – E-Riz Jan 13 '15 at 21:21
  • @ajb It's not so advanced tool. Mostly it's just a bung of plugins matching single pattern each. (of course there are few more advanced) – NiematojakTomasz Jan 13 '15 at 21:38
  • Would offer this dodge: if you can use Project Lombok, you won't need to use eclipse to generate that method and FindBugs won't report the error. Less is more, anyway. http://projectlombok.org/ – unigeek Jan 13 '15 at 21:39

4 Answers4

1

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.

NiematojakTomasz
  • 2,433
  • 20
  • 23
  • So how do you enforce uniform code style on code auto-generated by some other tool? – ajb Jan 13 '15 at 21:16
  • It's difficult. You can adopt single code-style advising tool like finbugs, PMD, checkstyle (with build tool like maven integration), sonar, something built into IDE, etc... with ruleset you choose to use. However, there is no seamless integration between code-style and code generating tools. Programmers usually need to review generated code if you want to maintain code adjusted to code-style rules. Code inspection built in IDE might be great help and allow to do it with few shortcut clicks when configured. – NiematojakTomasz Jan 13 '15 at 21:27
0

The code should be simplified because it's doing unnecessary things that you have been informed by firebug. This inspection is valid.

@Override
public int hashCode() {
    return 31 + (int) (id ^ (id >>> 32));
}
Roman C
  • 49,761
  • 33
  • 66
  • 176
  • The "unnecessary" things do not hamper performance at all though. The most anyone could really say on this would be opinion based. – austin wernli Jan 13 '15 at 20:58
  • I've clarified in the question that it's specifically the issue of storing a value and then returning it immediately that I'm asking about, because that's what findbugs is complaining about. I do agree that the rest of it could have been written more simply. – chiastic-security Jan 13 '15 at 21:01
  • 1
    Is the "Scariest" category also the correct category? Is there a potential bug here? What bug? Because just like the OP, I don't get it. – MarnixKlooster ReinstateMonica Jan 13 '15 at 21:01
  • Local variable requires stack to store it's values, you can keep without them, you can also count the operations using bytecode and compare two implementations. there's no performance bottleneck but a little overhead. – Roman C Jan 13 '15 at 21:03
  • A compiler with decent optimization capability should be able to figure out that the operation to store into a local variable on the stack can be eliminated in this case. – ajb Jan 13 '15 at 21:11
  • @chiastic-security Stored value is not used in the code rather than in return statement, that's what firebug complains. – Roman C Jan 13 '15 at 21:11
  • @ajb Sometimes post-optimization of generated code may lead to producing really unreadable code. Even if sticking to code formatting and style rules. People still argue either to use tabs or spaces for indention. – NiematojakTomasz Jan 13 '15 at 21:20
  • Everything in bytecode requires stack to store a value, be it a named variable or an anonymous intermediate result. There is no difference. – Marko Topolnik Jan 13 '15 at 21:22
  • That's some unreadable code right there. Readable (and thus, maintainable) code is far more important than minimal code. – E-Riz Jan 13 '15 at 21:23
  • @E-Riz It's perfectly readable to my eyes. But that just proves how individual this is and how fruitless it is to try to base any objective assessments on it. – Marko Topolnik Jan 13 '15 at 21:26
0

Eclipse code is not that bad but it is unnecessarily clumsy. In such trivial cases it is often better to write it by hand. Your code is essentially equivalent to this:

@Override public int hashCode() {
    return Long.hashCode(id);
}
assylias
  • 321,522
  • 82
  • 660
  • 783
0

It's less about simplification and more about the possibility that you forgot something. If you store the result, then that's most likely because you want to do something with it. If not, you'd just return it.

FindBugs is warning you that you most likely meant to do something with the stored value, but 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;
}
martinez314
  • 12,162
  • 5
  • 36
  • 63