8

I started my adventure with Sonar ;)

Sonar with it's default configuration has PMD UnnecessaryLocalBeforeReturn error set on major level.

    List<Todo> filtered = em.createQuery(query).getResultList();
    return filtered;

It means for me that I should change this code above in one line.

It's really interesting for me because I recommend my colleagues to make this "unnecessary" local before return.

I think it ease debugging. When I set up breakpoint on return line, I'm sure that when I get there this value will be ready and I don't have to make selection over my statement or do "Step over Expression".

Beside I believe it has positive impact on reducing return points in methods.

My question is: Are there some kind of explanations/discussions why errors from projects such as Checkstyle, PMD, FindBugs, etc. were acknowledged as errors?

zimi
  • 1,586
  • 12
  • 27
  • 1
    It's not bad, just unnecessary. These static analysis tools don't simply flag bugs. Instead, they provide toolboxes for checking all kinds of things, the selection of which depends on the kind of software you are developing. Your project may require you to do everything in as few lines of code as possible, or maybe your special compiler doesn't optimize this case and you can't affort the extra clock cycle ... then this check is for you. Else you can safely turn it off. – barfuin Nov 25 '13 at 12:25
  • This is a good related question: http://stackoverflow.com/questions/31733811/local-variables-before-return-statements-does-it-matter/31734226#31734226 – Fred Porciúncula Dec 07 '15 at 13:28

1 Answers1

1

If your point is only viewing the content of the List, you may just as well either put the break point in the caller of the method. The other option is to put a break point and evaluate the value (Eclipse & IntelliJ do it nicely).

Why is it consedered as a bad practice ?

You just add a reference to a variable while it is not necessary.

This just increase the workload on the Garbage Collector.

tsmets
  • 91
  • 4