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

Codenarc Revisited - Gr8Conf US 2016

Codenarc Revisited - Gr8Conf US 2016

Codenarc Revisited
------------
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.
------------
Jennifer “Jenn” Strater is a software engineer with a passion for developing and designing applications using new and innovative technologies. Her strengths are in the service layer including building RESTful APIs. Jenn also has experience with relational and NoSQL databases, devops, front-end, and mobile in the healthcare and transportation industries. She learns new tools and systems quickly and introduces new technology learned through local community groups, involvement in the international Groovy community, and when speaking at or attending regional, national, and international conferences.

Jenn is the co-founder of the organization Gr8Ladies and has organized Gr8Workshops for developers interested in an overview and crash course in Groovy technologies. She has presented at various Minnesota tech events, the Grace Hopper Celebration of Women in Computing, Greach, Gr8Conf EU, Gr8Conf US, and Devoxx Belgium.

Starting in September, Jenn will be a master's student at the Technical University of Denmark studying static analysis and compilers with a focus on Groovy with funding from the Fulbright U.S. student program.

jlstrater

July 28, 2016
Tweet

More Decks by jlstrater

Other Decks in Technology

Transcript

  1. Codenarc Revisited
    Jenn Strater
    @codeJENNerator

    View Slide

  2. @codeJENNerator
    Follow Along
    • https://github.com/jlstrater/gr8data/tree/master/config/codenarc
    • https://github.com/jlstrater/groovy-spring-boot-restdocs-
    example/tree/master/gradle/codenarc

    View Slide

  3. @codeJENNerator
    About Me

    View Slide

  4. @codeJENNerator
    About Me
    • Senior Consultant at Object Partners, Inc.

    View Slide

  5. @codeJENNerator
    About Me
    • Senior Consultant at Object Partners, Inc.
    • Co-Founder of Gr8Ladies

    View Slide

  6. @codeJENNerator
    About Me
    • Senior Consultant at Object Partners, Inc.
    • Co-Founder of Gr8Ladies
    • 2016 - 2017 Fulbright US Student Program Selectee
    to Denmark

    View Slide

  7. @codeJENNerator
    Background

    View Slide

  8. @codeJENNerator
    Background
    • Spring Boot with Groovy

    View Slide

  9. @codeJENNerator
    Background
    • Spring Boot with Groovy
    • Ratpack with Groovy

    View Slide

  10. @codeJENNerator
    Background
    • Spring Boot with Groovy
    • Ratpack with Groovy
    • Grails

    View Slide

  11. @codeJENNerator
    Background
    • Spring Boot with Groovy
    • Ratpack with Groovy
    • Grails
    • Anything Else?

    View Slide

  12. @codeJENNerator
    What is ?

    View Slide

  13. @codeJENNerator
    https://github.com/CodeNarc/CodeNarc
    “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.”

    View Slide

  14. @codeJENNerator

    View Slide

  15. @codeJENNerator
    • Website
    • codenarc.sourceforge.net

    View Slide

  16. @codeJENNerator
    • Website
    • codenarc.sourceforge.net
    • Current Version
    • 0.25.2

    View Slide

  17. @codeJENNerator

    View Slide

  18. @codeJENNerator
    • Mailing List
    • https://sourceforge.net/p/codenarc/mailman

    View Slide

  19. @codeJENNerator
    • Mailing List
    • https://sourceforge.net/p/codenarc/mailman
    • Project Admin
    • Chris Mair

    View Slide

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

    View Slide

  21. View Slide

  22. @codeJENNerator
    .equals instead of ==

    View Slide

  23. View Slide

  24. @codeJENNerator

    View Slide

  25. @codeJENNerator
    def someMethod(def something, def somethingElse) {

    if (something) {

    doSomething()

    }

    else if(somethingElse) {

    doSomethingElse()

    }

    }

    View Slide

  26. @codeJENNerator
    def someMethod(def something, def somethingElse) {

    if (something) {

    doSomething()

    }

    else if(somethingElse) {

    doSomethingElse()

    }

    }

    View Slide

  27. @codeJENNerator
    def someMethod(def something, def somethingElse) {

    if (something) {

    doSomething()

    }

    else if(somethingElse) {

    doSomethingElse()

    }

    }

    View Slide

  28. @codeJENNerator
    The Convert

    View Slide

  29. @codeJENNerator

    View Slide

  30. @codeJENNerator
    println “This is an unnecessary Groovy String”


    View Slide

  31. @codeJENNerator

    View Slide

  32. @codeJENNerator
    this.something.getThisThing()

    View Slide

  33. @codeJENNerator
    New Team Member

    View Slide

  34. @codeJENNerator
    if(true) { }
    vs
    if (true) { }
    vs
    if (ready){ }

    View Slide

  35. @codeJENNerator
    Leaning Tower of Code
    https://flic.kr/p/de4pmb

    View Slide

  36. @codeJENNerator
    Summary
    • Useful For:
    • Interns/Junior Devs
    • Java Devs converting to Groovy
    • New Team Members
    • YOU!

    View Slide

  37. @codeJENNerator
    Approaches

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  41. @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

  42. @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

  43. @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

  44. View Slide

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

    View Slide

  46. @codeJENNerator
    Basics
    https://flic.kr/p/oiAEx6

    View Slide

  47. @codeJENNerator
    What is a rule?

    View Slide

  48. @codeJENNerator
    What is a rule?
    MissingBlankLineAfterPackage

    View Slide

  49. @codeJENNerator
    What is a rule?
    MissingBlankLineAfterPackage
    package org.codenarc

    import java.util.Date // violation


    class MyClass {

    void go() { /* ... */ }

    }

    View Slide

  50. @codeJENNerator
    What is a ruleset?

    View Slide

  51. @codeJENNerator
    What is a ruleset?
    Formatting Rules ("rulesets/formatting.xml")

    View Slide

  52. @codeJENNerator
    Rule Configuration

    View Slide

  53. @codeJENNerator
    Rule Configuration
    • Priority

    View Slide

  54. @codeJENNerator
    Rule Configuration
    • Priority
    • ViolationMessage

    View Slide

  55. @codeJENNerator
    Rule Configuration
    • Priority
    • ViolationMessage
    • Description

    View Slide

  56. @codeJENNerator
    Rule Configuration
    • Priority
    • ViolationMessage
    • Description
    • Name

    View Slide

  57. @codeJENNerator
    Rule Configuration

    View Slide

  58. @codeJENNerator
    Rule Configuration
    • Custom Parameters

    View Slide

  59. @codeJENNerator
    Rule Configuration
    • Custom Parameters
    • applyToClassNames

    View Slide

  60. @codeJENNerator
    Rule Configuration
    • Custom Parameters
    • applyToClassNames
    • applyToFileNames

    View Slide

  61. @codeJENNerator
    Ways to add codenarc
    to your project

    View Slide

  62. @codeJENNerator
    • runs with gradle check
    https://docs.gradle.org/current/userguide/codenarc_plugin.html

    View Slide

  63. @codeJENNerator
    Config Options

    View Slide

  64. @codeJENNerator
    Config Options
    • configFile
    • ignoreFailures
    • maxPriority1Violations
    • maxPriority2Violations
    • maxPriority3Violations

    View Slide

  65. @codeJENNerator
    Config Options

    View Slide

  66. @codeJENNerator
    Config Options
    • reportFormat
    • reportsDir
    • sourceSets
    • toolVersion

    View Slide

  67. @codeJENNerator

    View Slide

  68. @codeJENNerator
    codenarc {

    toolVersion = '0.25.2'

    }


    codenarcMain {

    configFile file('config/codenarc/codenarc.groovy')

    }


    codenarcTest {

    configFile file('config/codenarc/codenarcTest.groovy')

    }

    View Slide

  69. @codeJENNerator
    • Grails Projects Version < 3.0
    • runs with grails codenarc
    http://grails.org/plugin/codenarc
    Plugin

    View Slide

  70. @codeJENNerator
    Other Codenarc Plugins

    View Slide

  71. @codeJENNerator
    Other Codenarc Plugins
    • IDEs
    • IntelliJ
    • Eclipse

    View Slide

  72. @codeJENNerator
    Other Codenarc Plugins
    • IDEs
    • IntelliJ
    • Eclipse
    • Sonar

    View Slide

  73. @codeJENNerator
    Other Codenarc Plugins
    • IDEs
    • IntelliJ
    • Eclipse
    • Sonar
    • Maven

    View Slide

  74. @codeJENNerator
    Other Codenarc Plugins
    • IDEs
    • IntelliJ
    • Eclipse
    • Sonar
    • Maven
    • Jenkins (formerly Hudson)

    View Slide

  75. @codeJENNerator
    Reports

    View Slide

  76. @codeJENNerator
    build/reports/codenarc/main.html

    View Slide

  77. @codeJENNerator
    Jenkins Violations Plugin
    img src: https://leanjavaengineering.wordpress.com/2010/08/25/grails-codenarc-hudson/

    View Slide

  78. @codeJENNerator
    Starter Ruleset
    http://codenarc.sourceforge.net/StarterRuleSet-
    AllRulesByCategory.groovy.txt
    https://flic.kr/p/c8QjRC

    View Slide

  79. @codeJENNerator
    config/codenarc/codenarc.groovy
    ruleset {

    // rulesets/basic.xml

    AssertWithinFinallyBlock

    AssignmentInConditional

    BigDecimalInstantiation

    BitwiseOperatorInConditional

    BooleanGetBoolean

    BrokenNullCheck

    BrokenOddnessCheck

    ClassForName

    ComparisonOfTwoConstants

    ComparisonWithSelf

    ConstantAssertExpression

    ConstantIfExpression

    ConstantTernaryExpression

    .
    .
    .
    // rulesets/braces.xml

    ElseBlockBraces(bracesRequiredForElseIf: true)

    ForStatementBraces

    IfStatementBraces

    WhileStatementBraces
    .
    .
    .
    }

    View Slide

  80. @codeJENNerator
    Turning Off Rules - Comments
    ruleset {
    /*
    // rulesets/braces.xml

    ElseBlockBraces(bracesRequiredForElseIf: true)

    ForStatementBraces

    IfStatementBraces

    WhileStatementBraces
    */
    }

    View Slide

  81. @codeJENNerator
    Turning Off Rules -
    Enabled:False
    ruleset {
    ruleset('rulesets/basic.xml') {
    CatchThrowable(enabled:false)
    }
    }

    View Slide

  82. @codeJENNerator
    Suppress Warnings


    @SuppressWarnings('DuplicateStringLiteral')

    class MyClass {

    def y = 'x'

    def z = 'x'


    @SuppressWarnings(['IfStatementBraces', 'ThrowException'])

    int getCount() {

    if (!ready) throw new Exception('Not ready')

    }

    }

    View Slide

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

    View Slide

  84. @codeJENNerator
    0.21
    • No Wildcard Imports
    • Lots of New Formatting Rules
    • Consecutive Blank Lines
    • Blank Line Before Package
    • FileEndsWithoutNewline

    View Slide

  85. @codeJENNerator
    0.22
    • No Def
    • Unnecessary Safe Navigation Operator

    View Slide

  86. @codeJENNerator
    0.23
    • Nested For Loop

    View Slide

  87. @codeJENNerator
    0.25
    • No Tab Character
    • Trailing Comma

    View Slide

  88. @codeJENNerator
    Each Release Improves Existing Rules
    https://flic.kr/p/3mPXcD

    View Slide

  89. @codeJENNerator
    Rules Worth Talking
    About

    View Slide

  90. @codeJENNerator
    File Ends Without
    NewLine

    View Slide

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

    View Slide

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

    View Slide

  93. @codeJENNerator
    Could Be Elvis
    Lots of false positives

    View Slide

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

    View Slide

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

    View Slide

  96. @codeJENNerator
    No Def
    • Great for Spring Boot
    https://flic.kr/p/apRkJh

    View Slide

  97. @codeJENNerator
    No Def
    • Great for Spring Boot
    • Problematic with Grails
    https://flic.kr/p/apRkJh

    View Slide

  98. @codeJENNerator
    Good in Theory

    View Slide

  99. @codeJENNerator
    Good in Theory
    • Unnecessary Return

    View Slide

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

    View Slide

  101. @codeJENNerator
    Debatable

    View Slide

  102. @codeJENNerator
    Debatable
    • Unnecessary Groovy String

    View Slide

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

    View Slide

  104. @codeJENNerator
    Debatable

    View Slide

  105. @codeJENNerator
    Debatable
    • Misordered Static Imports

    View Slide

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

    View Slide

  107. @codeJENNerator
    Still Deciding

    View Slide

  108. @codeJENNerator
    Still Deciding
    • Complexity Metrics

    View Slide

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

    View Slide

  110. @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

  111. @codeJENNerator
    Contribute
    • http://codenarc.sourceforge.net/codenarc-
    developer-guide.html
    • https://github.com/CodeNarc/CodeNarc

    View Slide

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

    View Slide

  113. @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

  114. @codeJENNerator
    Conclusion

    View Slide

  115. @codeJENNerator
    Questions?
    https://flic.kr/p/5DeuzB

    View Slide