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 Slide

  2. @codeJENNerator
    About Me

    View Slide

  3. @codeJENNerator
    About You

    View Slide

  4. @codeJENNerator
    What
    is ?

    View 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 Slide

  6. @codeJENNerator

    View Slide

  7. @codeJENNerator
    • codenarc.sourceforge.net

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  11. @codeJENNerator
    Demo

    View 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 Slide

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

    View Slide

  14. @codeJENNerator
    Approaches

    View Slide

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

    View Slide

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

    View Slide

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

    View 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 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 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 Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  32. @codeJENNerator
    Good in Theory

    View Slide

  33. @codeJENNerator
    Good in Theory
    • Unnecessary Return

    View Slide

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

    View Slide

  35. @codeJENNerator
    Debatable

    View Slide

  36. @codeJENNerator
    Debatable
    • Unnecessary Groovy String

    View Slide

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

    View Slide

  38. @codeJENNerator
    Debatable

    View Slide

  39. @codeJENNerator
    Debatable
    • Misordered Static Imports

    View Slide

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

    View 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 Slide

  42. @codeJENNerator
    Demo

    View 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 Slide

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

    View Slide

  45. @codeJENNerator
    Conclusion

    View Slide

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

    View Slide

  47. @codeJENNerator

    View Slide

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

    View Slide

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

    View 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 Slide

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

    View Slide