Skip to content

[java] UnnecessaryLocalBeforeReturn enhancement is overly restrictive -- method order matters #310

Closed

Description

@epkugelmass

Rule Set:
UnnecessaryLocalBeforeReturn

Description:
The enhancement in #240 was ill-advised. The change in behavior should have been formulated as a new rule or an option on the existing rule, not added to an existing rule in a "bug-fix" release.

There are many cases where order does matter even for variables that are declared and then only referenced a single time.

Code Sample demonstrating the issue:

int i = compute(); // might throw
markComputationDone();
return i;
Mutable m = ...;
int i = compute(m);
sideEffect(m);
return i;

Activity

  1. jsotuyod commented on Mar 26, 2017

    @jsotuyod
    Member

    Thanks for your report!

    Your concerns have been partially raised before on #282, which was fixed on #294 (for next release cycle). These changes cover your second example.

    As for the first example, I can't think of a valid example that doesn't point to a design issue (ie: the called methods are nor truly independent and should therefore be merged). If you can think of such scenario, please let us know.

    A minor note. PMD doesn't follow semantic versioning, although there are open discussions to start doing so.

  2. epkugelmass commented on Mar 28, 2017

    @epkugelmass
    Author

    Awesome, glad to see the fix for example number two. Example one would be better with a listener pattern (which would wrap the mark...() method up in the first call) but there are reasonable cases where you might not want to do that (at least not immediately). This seems gray enough that it warrants a separate rule or rule option rather than being part of what I see as a very black-and-white rule.

  3. jsotuyod commented on Mar 28, 2017

    @jsotuyod
    Member

    @epkugelmass that's a very reasonable scenario. Let me think this over and come back to you.

  4. added this to the 5.6.0 milestone on Mar 28, 2017
  5. added
    a:bug
    on Mar 28, 2017
  6. self-assigned this
    on Mar 28, 2017
  7. changed the title [-][java] UnnecessaryLocalBeforeReturn enhancement is overly restrctive -- method order matters[/-] [+][java] UnnecessaryLocalBeforeReturn enhancement is overly restrictive -- method order matters[/+] on Apr 22, 2017
  8. self-assigned this
    on Apr 22, 2017
  9. adangel commented on Apr 22, 2017

    @adangel
    Member

    @epkugelmass @jsotuyod
    I've added for now a new property "statementOrderMatters" for this rule. It is by default enabled and preserves the old behavior. If this property is set to false, it switches to the new improved implementation, that would detect detect again cases like your first example - we do not have a way to know about the side-effects of "markComputationDone".
    We might improve the rule with #241, if possible.

  10. added 2 commits that reference this issue on Apr 22, 2017
    05308ef
    b6aae55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

a:bug

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions