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. @codeJENNerator About Me • Senior Consultant at Object Partners, Inc.

    • Co-Founder of Gr8Ladies • 2016 - 2017 Fulbright US Student Program Selectee to Denmark
  2. @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.”
  3. @codeJENNerator Why use Codenarc? • Improves readability • Saves time

    with code reviews • Code with fewer bugs • Help onboard new team members
  4. @codeJENNerator def someMethod(def something, def somethingElse) {
 if (something) {


    doSomething()
 }
 else if(somethingElse) {
 doSomethingElse()
 }
 }
  5. @codeJENNerator def someMethod(def something, def somethingElse) {
 if (something) {


    doSomething()
 }
 else if(somethingElse) {
 doSomethingElse()
 }
 }
  6. @codeJENNerator def someMethod(def something, def somethingElse) {
 if (something) {


    doSomething()
 }
 else if(somethingElse) {
 doSomethingElse()
 }
 }
  7. @codeJENNerator Summary • Useful For: • Interns/Junior Devs • Java

    Devs converting to Groovy • New Team Members • YOU!
  8. @codeJENNerator Approaches • Start with one project or team •

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

    a violation limit and fail builds that add go above the limit
  13. @codeJENNerator codenarc {
 toolVersion = '0.25.2'
 }
 
 codenarcMain {


    configFile file('config/codenarc/codenarc.groovy')
 }
 
 codenarcTest {
 configFile file('config/codenarc/codenarcTest.groovy')
 }
  14. @codeJENNerator • Grails Projects Version < 3.0 • runs with

    grails codenarc http://grails.org/plugin/codenarc Plugin
  15. @codeJENNerator Other Codenarc Plugins • IDEs • IntelliJ • Eclipse

    • Sonar • Maven • Jenkins (formerly Hudson)
  16. @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 . . . }
  17. @codeJENNerator Turning Off Rules - Comments ruleset { /* //

    rulesets/braces.xml
 ElseBlockBraces(bracesRequiredForElseIf: true)
 ForStatementBraces
 IfStatementBraces
 WhileStatementBraces */ }
  18. @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')
 }
 }
  19. @codeJENNerator 0.21 • No Wildcard Imports • Lots of New

    Formatting Rules • Consecutive Blank Lines • Blank Line Before Package • FileEndsWithoutNewline
  20. @codeJENNerator WARNING! • Enhanced Ruleset does not work with the

    gradle codenarc plugin • https://objectpartners.com/2016/03/16/resolving- codenarc-compilation-warnings/
  21. @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)
 }
 }
 }
 }
 }
  22. @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