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.

1f28a0c1988421be3268026bd6bb6f49?s=128

jlstrater

May 28, 2019
Tweet

More Decks by jlstrater

Other Decks in Technology

Transcript

  1. Codenarc Revisited Jenn Strater @codeJENNerator

  2. @codeJENNerator About Me

  3. @codeJENNerator About You

  4. @codeJENNerator What is ?

  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
  6. @codeJENNerator

  7. @codeJENNerator • codenarc.sourceforge.net

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

    2019
  9. @codeJENNerator Why use Codenarc? • Improves readability • Saves time

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

    converting to Groovy • New Team Members • YOU!
  11. @codeJENNerator Demo

  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') }
  13. @codeJENNerator Other Codenarc Plugins • IntelliJ (recently updated!!) • Eclipse

    • Grails 2.x Plugin • Maven • Hudson/Jenkins
  14. @codeJENNerator Approaches

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

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

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

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

    a violation limit and fail builds that add go above the limit
  22. https://flic.kr/p/rmit9T

  23. @codeJENNerator 1.0 (Sept 2017) • Updates several library versions •

    Updated Infrastructure • Lots of bug fixes
  24. @codeJENNerator 1.3 (Jan 2019) • Lots of new Javadoc rules

    • Fixes for some of the problematic JUnit rules
  25. @codeJENNerator 1.4 (May 26, 2019) • Enforcing Static Compilation •

    More Javadoc formatting rules
  26. img src: https://flic.kr/p/rehEf5 I don’t like all of the Codenarc

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

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

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

  30. @codeJENNerator No Def • Great for Spring Boot and other

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

    Groovy only projects • Problematic with Grails https://flic.kr/p/apRkJh
  32. @codeJENNerator Good in Theory

  33. @codeJENNerator Good in Theory • Unnecessary Return

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

  35. @codeJENNerator Debatable

  36. @codeJENNerator Debatable • Unnecessary Groovy String

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

  38. @codeJENNerator Debatable

  39. @codeJENNerator Debatable • Misordered Static Imports

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

  41. @codeJENNerator WARNING! • Enhanced Ruleset does not work with the

    gradle codenarc plugin • https://objectpartners.com/2016/03/16/resolving- codenarc-compilation-warnings/
  42. @codeJENNerator Demo

  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)
 }
 }
 }
 }
 }
  44. @codeJENNerator Adding Custom Rules to Your Project • Follow the

    Grails Guide! https://guides.grails.org/ grails-codenarc/guide/index.html
  45. @codeJENNerator Conclusion

  46. @codeJENNerator Read the docs for more on.. • Rule Specifics

    • Ant Task • Command Line
  47. @codeJENNerator

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

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

    Contribute on GitHub: https://github.com/codenarc/ codenarc
  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
  51. @codeJENNerator Questions? https://flic.kr/p/5DeuzB https://speakerdeck.com/jlstrater/codenarc-revisited-gr8conf-eu-2019