Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Codenarc Revisited GR8Conf EU 2019

Codenarc Revisited GR8Conf EU 2019

When was the last time you looked at code quality? Do you have some old codenarc configuration with half(or more!) of the rules turned off because they were reporting false positives? Or maybe you have @SupressWarnings all over your code? Or perhaps you want to do something small like to enforce tabs vs spaces for all members of your team?

Codenarc is a static analysis tool for Groovy that enforces style, detects bad practices, and suggests improvements. If it has been awhile since you last looked at codenarc, there might be new rules or configuration options to get you back on the path to better code quality. This session will explore what is new with codenarc, options for specific groovy ecosystem projects such as grails, and walk through how to create custom rules for your own project.

jlstrater

May 28, 2019
Tweet

More Decks by jlstrater

Other Decks in Technology

Transcript

  1. Codenarc Revisited
    Jenn Strater
    @codeJENNerator

    View full-size slide

  2. @codeJENNerator
    About Me

    View full-size slide

  3. @codeJENNerator
    About You

    View full-size slide

  4. @codeJENNerator
    What
    is ?

    View full-size slide

  5. @codeJENNerator
    “CodeNarc is a static analysis tool for Groovy
    source code, enabling monitoring and
    enforcement of many coding standards and best
    practices…
    CodeNarc is similar to popular static analysis
    tools such as PMD or Checkstyle. Unlike those
    tools which analyze Java code, CodeNarc
    analyzes Groovy code.”
    https://github.com/CodeNarc/CodeNarc

    View full-size slide

  6. @codeJENNerator

    View full-size slide

  7. @codeJENNerator
    • codenarc.sourceforge.net

    View full-size slide

  8. @codeJENNerator
    • codenarc.sourceforge.net
    • Latest Release: 1.4.0 on May 26, 2019

    View full-size slide

  9. @codeJENNerator
    Why use Codenarc?
    • Improves readability
    • Saves time with code reviews
    • Code with fewer bugs
    • Help onboard new team members

    View full-size slide

  10. @codeJENNerator
    Who needs Codenarc?
    • Interns/Junior Devs
    • Java Devs converting to Groovy
    • New Team Members
    • YOU!

    View full-size slide

  11. @codeJENNerator
    Demo

    View full-size slide

  12. @codeJENNerator
    Configure the Gradle
    Plugin
    plugins {
    id 'codenarc'
    }
    codenarc {
    toolVersion = '1.4'
    }
    codenarcMain {
    configFile file('config/codenarc/codenarc.groovy')
    }
    codenarcTest {
    configFile file('config/codenarc/codenarcTest.groovy')
    }

    View full-size slide

  13. @codeJENNerator
    Other Codenarc Plugins
    • IntelliJ (recently updated!!)
    • Eclipse
    • Grails 2.x Plugin
    • Maven
    • Hudson/Jenkins

    View full-size slide

  14. @codeJENNerator
    Approaches

    View full-size slide

  15. @codeJENNerator
    Approaches
    • Start with one project or team

    View full-size slide

  16. @codeJENNerator
    Approaches
    • Start with one project or team
    • Turn on all rules

    View full-size slide

  17. @codeJENNerator
    Approaches
    • Start with one project or team
    • Turn on all rules
    • Look at all of the violations

    View full-size slide

  18. @codeJENNerator
    Approaches
    • Start with one project or team
    • Turn on all rules
    • Look at all of the violations
    • Turn off rules that don’t make sense for your team

    View full-size slide

  19. @codeJENNerator
    Approaches
    • Start with one project or team
    • Turn on all rules
    • Look at all of the violations
    • Turn off rules that don’t make sense for your team
    • Fix all the problems

    View full-size slide

  20. @codeJENNerator
    Approaches
    • Start with one project or team
    • Turn on all rules
    • Look at all of the violations
    • Turn off rules that don’t make sense for your team
    • Fix all the problems
    • Once you have a good set of rules with minimal false
    positives, set that as the standard for all teams using
    Groovy

    View full-size slide

  21. @codeJENNerator
    Approaches
    • Turn on all the rules
    • Set a violation limit and fail builds that add go
    above the limit

    View full-size slide

  22. https://flic.kr/p/rmit9T

    View full-size slide

  23. @codeJENNerator
    1.0 (Sept 2017)
    • Updates several library versions
    • Updated Infrastructure
    • Lots of bug fixes

    View full-size slide

  24. @codeJENNerator
    1.3 (Jan 2019)
    • Lots of new Javadoc rules
    • Fixes for some of the problematic JUnit rules

    View full-size slide

  25. @codeJENNerator
    1.4 (May 26, 2019)
    • Enforcing Static Compilation
    • More Javadoc formatting rules

    View full-size slide

  26. img src: https://flic.kr/p/rehEf5
    I don’t like all of the Codenarc Rules!

    View full-size slide

  27. @codeJENNerator
    Dry
    • DuplicateListLiteral
    • DuplicateMapLiteral
    • DuplicateNumberLiteral
    • DuplicateStringLiteral

    View full-size slide

  28. @codeJENNerator
    JUnit Ruleset
    Doesn’t work well with Spock

    View full-size slide

  29. @codeJENNerator
    No Def
    https://flic.kr/p/apRkJh

    View full-size slide

  30. @codeJENNerator
    No Def
    • Great for Spring Boot and other
    Groovy only projects
    https://flic.kr/p/apRkJh

    View full-size slide

  31. @codeJENNerator
    No Def
    • Great for Spring Boot and other
    Groovy only projects
    • Problematic with Grails
    https://flic.kr/p/apRkJh

    View full-size slide

  32. @codeJENNerator
    Good in Theory

    View full-size slide

  33. @codeJENNerator
    Good in Theory
    • Unnecessary Return

    View full-size slide

  34. @codeJENNerator
    Good in Theory
    • Unnecessary Return
    • Line Length

    View full-size slide

  35. @codeJENNerator
    Debatable

    View full-size slide

  36. @codeJENNerator
    Debatable
    • Unnecessary Groovy String

    View full-size slide

  37. @codeJENNerator
    Debatable
    • Unnecessary Groovy String
    • Unnecessary Getter

    View full-size slide

  38. @codeJENNerator
    Debatable

    View full-size slide

  39. @codeJENNerator
    Debatable
    • Misordered Static Imports

    View full-size slide

  40. @codeJENNerator
    Debatable
    • Misordered Static Imports
    • No Wildcard Imports

    View full-size slide

  41. @codeJENNerator
    WARNING!
    • Enhanced Ruleset does not work with the gradle
    codenarc plugin
    • https://objectpartners.com/2016/03/16/resolving-
    codenarc-compilation-warnings/

    View full-size slide

  42. @codeJENNerator
    Demo

    View full-size slide

  43. @codeJENNerator
    Create a Custom Rule
    import org.codenarc.rule.AbstractRule

    import org.codenarc.source.SourceCode


    /**

    * Sample rule. Checks for static fields.

    */

    class MyStaticFieldRule extends AbstractRule {

    String name = 'MyStaticField'

    int priority = 2


    void applyTo(SourceCode sourceCode, List violations) {

    sourceCode.ast.classes.each { clazz ->

    clazz.fields.each { fieldNode ->

    if (fieldNode.static) {

    violations << createViolation(sourceCode, fieldNode)

    }

    }

    }

    }

    }

    View full-size slide

  44. @codeJENNerator
    Adding Custom Rules to
    Your Project
    • Follow the Grails Guide! https://guides.grails.org/
    grails-codenarc/guide/index.html

    View full-size slide

  45. @codeJENNerator
    Conclusion

    View full-size slide

  46. @codeJENNerator
    Read the docs for more
    on..
    • Rule Specifics
    • Ant Task
    • Command Line

    View full-size slide

  47. @codeJENNerator

    View full-size slide

  48. @codeJENNerator
    • Subscribe to the mailing list: https://sourceforge.net/
    p/codenarc/mailman

    View full-size slide

  49. @codeJENNerator
    • Subscribe to the mailing list: https://sourceforge.net/
    p/codenarc/mailman
    • Contribute on GitHub: https://github.com/codenarc/
    codenarc

    View full-size slide

  50. @codeJENNerator
    Example Rulesets
    • Grails
    • https://github.com/jlstrater/gr8data/tree/master/
    config/codenarc
    • Spring Boot
    • https://github.com/jlstrater/groovy-spring-boot-
    restdocs-example/tree/master/gradle/codenarc

    View full-size slide

  51. @codeJENNerator
    Questions?
    https://flic.kr/p/5DeuzB
    https://speakerdeck.com/jlstrater/codenarc-revisited-gr8conf-eu-2019

    View full-size slide