Slide 1

Slide 1 text

Codenarc Revisited Jenn Strater @codeJENNerator

Slide 2

Slide 2 text

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

Slide 3

Slide 3 text

About Me

Slide 4

Slide 4 text

About Me • Senior Consultant at Object Partners, Inc.

Slide 5

Slide 5 text

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

Slide 6

Slide 6 text

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

Slide 7

Slide 7 text

Background

Slide 8

Slide 8 text

Background • Spring Boot with Groovy

Slide 9

Slide 9 text

Background • Spring Boot with Groovy • Ratpack with Groovy

Slide 10

Slide 10 text

Background • Spring Boot with Groovy • Ratpack with Groovy • Grails

Slide 11

Slide 11 text

Background • Spring Boot with Groovy • Ratpack with Groovy • Grails • Anything Else?

Slide 12

Slide 12 text

What is ?

Slide 13

Slide 13 text

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.”

Slide 14

Slide 14 text

No content

Slide 15

Slide 15 text

• Website • codenarc.sourceforge.net

Slide 16

Slide 16 text

• Website • codenarc.sourceforge.net • Current Version • 0.25.2

Slide 17

Slide 17 text

No content

Slide 18

Slide 18 text

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

Slide 19

Slide 19 text

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

Slide 20

Slide 20 text

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

Slide 21

Slide 21 text

No content

Slide 22

Slide 22 text

.equals instead of ==

Slide 23

Slide 23 text

No content

Slide 24

Slide 24 text

No content

Slide 25

Slide 25 text

def someMethod(def something, def somethingElse) {
 if (something) {
 doSomething()
 }
 else if(somethingElse) {
 doSomethingElse()
 }
 }

Slide 26

Slide 26 text

def someMethod(def something, def somethingElse) {
 if (something) {
 doSomething()
 }
 else if(somethingElse) {
 doSomethingElse()
 }
 }

Slide 27

Slide 27 text

def someMethod(def something, def somethingElse) {
 if (something) {
 doSomething()
 }
 else if(somethingElse) {
 doSomethingElse()
 }
 }

Slide 28

Slide 28 text

The Convert

Slide 29

Slide 29 text

No content

Slide 30

Slide 30 text

println “This is an unnecessary Groovy String”


Slide 31

Slide 31 text

No content

Slide 32

Slide 32 text

this.something.getThisThing()

Slide 33

Slide 33 text

New Team Member

Slide 34

Slide 34 text

if(true) { } vs if (true) { } vs if (ready){ }

Slide 35

Slide 35 text

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

Slide 36

Slide 36 text

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

Slide 37

Slide 37 text

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

Slide 38

Slide 38 text

No content

Slide 39

Slide 39 text

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

Slide 40

Slide 40 text

Basics https://flic.kr/p/oiAEx6

Slide 41

Slide 41 text

What is a rule?

Slide 42

Slide 42 text

What is a rule? MissingBlankLineAfterPackage

Slide 43

Slide 43 text

What is a rule? MissingBlankLineAfterPackage package org.codenarc
 import java.util.Date // violation
 
 class MyClass {
 void go() { /* ... */ }
 }

Slide 44

Slide 44 text

What is a ruleset?

Slide 45

Slide 45 text

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

Slide 46

Slide 46 text

Rule Configuration

Slide 47

Slide 47 text

Rule Configuration • Priority

Slide 48

Slide 48 text

Rule Configuration • Priority • ViolationMessage

Slide 49

Slide 49 text

Rule Configuration • Priority • ViolationMessage • Description

Slide 50

Slide 50 text

Rule Configuration • Priority • ViolationMessage • Description • Name

Slide 51

Slide 51 text

Rule Configuration

Slide 52

Slide 52 text

Rule Configuration • Parameters

Slide 53

Slide 53 text

Rule Configuration • Parameters • applyToClassNames

Slide 54

Slide 54 text

Rule Configuration • Parameters • applyToClassNames • applyToFileNames

Slide 55

Slide 55 text

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

Slide 56

Slide 56 text

Config Options

Slide 57

Slide 57 text

Config Options • configFile • ignoreFailures • maxPriority1Violations • maxPriority2Violations • maxPriority3Violations

Slide 58

Slide 58 text

Config Options

Slide 59

Slide 59 text

Config Options • reportFormat • reportsDir • sourceSets • toolVersion

Slide 60

Slide 60 text

No content

Slide 61

Slide 61 text

codenarc {
 toolVersion = '0.25.2'
 }
 
 codenarcMain {
 configFile file('config/codenarc/codenarc.groovy')
 }
 
 codenarcTest {
 configFile file('config/codenarc/codenarcTest.groovy')
 }

Slide 62

Slide 62 text

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

Slide 63

Slide 63 text

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 . . . }

Slide 64

Slide 64 text

Turning Off Rules - Comments ruleset { /* // rulesets/braces.xml
 ElseBlockBraces(bracesRequiredForElseIf: true)
 ForStatementBraces
 IfStatementBraces
 WhileStatementBraces */ }

Slide 65

Slide 65 text

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

Slide 66

Slide 66 text

Suppress Warnings 
 
 @SuppressWarnings('DuplicateStringLiteral')
 class MyClass {
 def y = 'x'
 def z = 'x'
 
 @SuppressWarnings(['IfStatementBraces', 'ThrowException'])
 int getCount() {
 if (!ready) throw new Exception('Not ready')
 }
 }

Slide 67

Slide 67 text

https://flic.kr/p/rmit9T

Slide 68

Slide 68 text

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

Slide 69

Slide 69 text

0.22 • No Def • Unnecessary Safe Navigation Operator

Slide 70

Slide 70 text

0.23 • Nested For Loop

Slide 71

Slide 71 text

0.25 • No Tab Character • Trailing Comma

Slide 72

Slide 72 text

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

Slide 73

Slide 73 text

Rules Worth Talking About

Slide 74

Slide 74 text

File Ends Without NewLine

Slide 75

Slide 75 text

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

Slide 76

Slide 76 text

Dry • DuplicateListLiteral • DuplicateMapLiteral • DuplicateNumberLiteral • DuplicateStringLiteral

Slide 77

Slide 77 text

Could Be Elvis Lots of false positives

Slide 78

Slide 78 text

JUnit Ruleset Doesn’t work well with Spock

Slide 79

Slide 79 text

No Def https://flic.kr/p/apRkJh

Slide 80

Slide 80 text

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

Slide 81

Slide 81 text

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

Slide 82

Slide 82 text

Good in Theory

Slide 83

Slide 83 text

Good in Theory • Unnecessary Return

Slide 84

Slide 84 text

Good in Theory • Unnecessary Return • Line Length

Slide 85

Slide 85 text

Debatable

Slide 86

Slide 86 text

Debatable • Unnecessary Groovy String

Slide 87

Slide 87 text

Debatable • Unnecessary Groovy String • Unnecessary Getter

Slide 88

Slide 88 text

Debatable

Slide 89

Slide 89 text

Debatable • Misordered Static Imports

Slide 90

Slide 90 text

Debatable • Misordered Static Imports • No Wildcard Imports

Slide 91

Slide 91 text

Still Deciding

Slide 92

Slide 92 text

Still Deciding • Complexity Metrics

Slide 93

Slide 93 text

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

Slide 94

Slide 94 text

Reports

Slide 95

Slide 95 text

build/reports/codenarc/main.html

Slide 96

Slide 96 text

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)
 }
 }
 }
 }
 }

Slide 97

Slide 97 text

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

Slide 98

Slide 98 text

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

Slide 99

Slide 99 text

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

Slide 100

Slide 100 text

Conclusion

Slide 101

Slide 101 text

Questions? https://flic.kr/p/5DeuzB