Experience Eclipse plugin PMD

the other day there was one post about useful plugins for Eclipse user gAmUssA, which was mentioned including PMD. I like this plugin seemed interesting and I decided to try to incite the pet on your project...

At the start of the project is fairly little java project in 2000+ files of code. So to study this plugin was that. It was written and spelled it for about 4 years and no less than 10 different programmers, both our and foreign.
In the specifics of the project will not go — no special meaning it now has.
Well, it started... Put it without problems and superfluous questions. I launched it, and 5-7 minutes he was engaged in a deep analysis of all the nonsense that we with colleagues wrote. As a result, in Volations Overview View was issued the result: the plate pokegama and classes in which he found "errors". At the same time Volations in Outline View you can see the description of these "mistakes" and from there you can go to a specific line of code.
In the plugin there are 5 priorities "error":
    the
  1. Error High
  2. the
  3. Error
  4. the
  5. Warning High
  6. the
  7. Warning
  8. the
  9. Information

From 3rd to 5th too insignificant to mention are more related to disputed kinds of recommendations like "A method argument that is never assigned can be declared final" (of course it can be declared final, but not much point in this, I think), and unused imports.
So I immediately turn off the display of "errors" these priorities (plugin easily allows you to do this by pressing the appropriate buttons in Volations Overview).
So, only the most terrible from the point of view of the PMD of the error.
Among them, I singled out the spread in my code. Given the scope of this code, I believe the resulting data fairly representative sample to talk about similar laws in other projects. :)

The most common errors were as follows:

    An empty method in an abstract class should be abstract instead
    I think a controversial statement... Well, I don't need that the heirs were required to override this method. What I'm going to do it abstract?

    System.out.print is used
    There certainly will not argue... Someone something was debugging and forgot to remove. Ashamed. If you have a live logger.

    Overridable method called during object construction
    This warning is very common my code. And it is also highly questionable. I understand why it is wary... they Say do not have time to initialize the object and will have to pull and all will fall. But PMD swears this warning for every class method called from the constructor, be it setter or something else (of course, if the method is not private). That is, from their point of view, designers need to present only the direct initialization of internal settings plus all the associated code. But if this code a lot? And if he is not once used? In General, if you strictly follow this rule, it solidly makes life difficult.

    Variable Naming Conventions
    Everyone understands what it is and why it is wrong to name the variables properly.:) But there are situations when this is difficult to do. In my case most often it was the use of an underscore as a prefix or suffix. But of course, this should be avoided.

    Avoid reassigning parameters
    (referring to the reinitialize input parameters of a method inside a method)
    From my point of view it's not pretty. Indeed, it is better to use temporary local variables. Transparent code.

    A class which only has private constructors should be final
    And why is that? That in one that in other case the class does not inherit. That is, not a hindrance. Maybe I do not understand?

    Do not use the short type
    Well here it is hard to disagree. He uses only to save memory, but the computation still envelope to int and back. So I see the point to use only constants of this data type. Where Xxx is a String, Integer, Long, Boolean... they all Have valueOf () and string, do nothing. By and large — the same eggs, only in profile. The only thing that wrappers for numeric types initializing using valueOf() according to them "is more memory friendly". Well, believe it.

    Do not explicitly trigger a garbage collection
    Do not consider a direct challenge to the garbage collector a good tone, but still sometimes necessary.



Conclusions



Well, in principle, useful thing that would catch small cockroaches from the code. But not more than. I can't say that the results of the plugin I'm sort of surprised or upset. Just became again clear that the work is not without flaws and that is something to strive for.
I would like to mention that there is the possibility of writing my validation rules in code. Personally try haven't had time yet, but I think there is some room for imagination.

Thank you for your attention. I hope this article was useful to someone.
Article based on information from habrahabr.ru

Популярные сообщения из этого блога

Approval of WSUS updates: import, export, copy

Kaspersky Security Center — the fight for automation

The Hilbert curve vs. Z-order