Menu

#1245 False Positive for Law of Demeter

PMD-5.1.3
closed
None
PMD
3-Major
Bug
maven-pmd-plugin-3.1
2019-12-15
2014-08-15
Martin
No

Class factory methods trigger this rule, e.g.

        // Create a Connection
        final Connection connection = connectionFactory.createConnection();
        connection.start();

Discussion

  • Andreas Dangel

    Andreas Dangel - 2014-08-22
    • status: open --> closed
    • assigned_to: Andreas Dangel
    • Milestone: New Tickets --> PMD-next
     
  • Andreas Dangel

    Andreas Dangel - 2014-08-22

    With the next release, objects that are created by something called "...factory..." won't trigger this anymore.

     
  • Alix Warnke

    Alix Warnke - 2015-10-07

    This is also a problem using the Builder pattern.

    There are many discussions on this topic, e.g. http://stackoverflow.com/questions/67561/do-fluent-interfaces-violate-the-law-of-demeter

    It’s sad that such a good rule as LOD triggers so many false-positives.

    It would be great if PMD could handle both these two issues (factories, builders) without relying on naming conventions but if that is very hard I would like it if an exception would be made for variables ending with “Builder” as well

     

    Last edit: Alix Warnke 2015-10-07
  • Alix Warnke

    Alix Warnke - 2015-10-09

    Suggestion: A more general solution to identify the Builder pattern is to check if the last method name is "build()". Example:

    javax.ws.rs.core.Response.<http status="">().build()

    Alternatively make it possible to configure class exceptions for this rule

     

    Last edit: Alix Warnke 2015-10-09
  • Linus Fernandes

    Linus Fernandes - 2019-12-15
     

    Last edit: Andreas Dangel 2019-12-20

Log in to post a comment.