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. @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
  2. @codeJENNerator Why use Codenarc? • Improves readability • Saves time

    with code reviews • Code with fewer bugs • Help onboard new team members
  3. @codeJENNerator Who needs Codenarc? • Interns/Junior Devs • Java Devs

    converting to Groovy • New Team Members • YOU!
  4. @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') }
  5. @codeJENNerator Approaches • Start with one project or team •

    Turn on all rules • Look at all of the violations
  6. @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
  7. @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
  8. @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
  9. @codeJENNerator Approaches • Turn on all the rules • Set

    a violation limit and fail builds that add go above the limit
  10. @codeJENNerator 1.3 (Jan 2019) • Lots of new Javadoc rules

    • Fixes for some of the problematic JUnit rules
  11. @codeJENNerator No Def • Great for Spring Boot and other

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

    Groovy only projects • Problematic with Grails https://flic.kr/p/apRkJh
  13. @codeJENNerator WARNING! • Enhanced Ruleset does not work with the

    gradle codenarc plugin • https://objectpartners.com/2016/03/16/resolving- codenarc-compilation-warnings/
  14. @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)
 }
 }
 }
 }
 }
  15. @codeJENNerator Adding Custom Rules to Your Project • Follow the

    Grails Guide! https://guides.grails.org/ grails-codenarc/guide/index.html
  16. @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