This document discusses the static code analysis tool Codenarc. It begins by introducing Codenarc and what it is used for. It then discusses why one should use Codenarc to check for coding standard violations and improve code quality. The document outlines Codenarc's rule-based system and categories of rules. It provides instructions on configuring Codenarc in Grails 3 projects and describes how to create custom rules. It concludes that Codenarc makes it easy to enforce coding standards and maintain a unified code style.
4. S O F T WA R E D E V E L O P M E N T
Turnkey
development
Atlassian
Solution
Partner
Consulting and
support in
software
architecture and
analysis
Application
integration
Evolutionary support
of our own
developments or
inherited ones
@alberto_deavila
6. G O A L S
✴ Write better code
✴ Learn good practices
✴ Use only one code style criteria
✴ Fix code errors
✴ Don’t Repeat Yourself
✴ Clean code
@alberto_deavila
7. ✴ What’s Codenarc?
✴ Why should you use Codenarc?
✴ Rules
✴ Config in Grails 3
✴ Custom rules
I N D E X
@alberto_deavila
9. ✴ Static code analysis tool for Groovy
✴ Similar to PMD or Checkstyle
✴ Opensource
✴ Check coding standards and best practices
✴ Flexible: rules, rulesets and custom rules
✴ Generates an HTML or XML report
W H AT ’ S C O D E N A R C ?
@alberto_deavila
10. ✴ Analyzes:
✴ Defects
✴ Bad practices
✴ Inconsistencies
✴ Style issues
✴ …
W H AT ’ S C O D E N A R C ?
@alberto_deavila
11. W H Y S H O U L D Y O U U S E C O D E N A R C ?
Why should you use Codenarc?
12. W H Y S H O U L D I U S E C O D E N A R C ?
@alberto_deavila
✴ In a projects works many programmers
✴ Each person has different style programming
✴ Not all best practices are known
✴ Programmers need tools to check its work
✴ The team can define the rules to apply
✴ Improve your project in many ways
17. R U L E S
@alberto_deavila
A group of rules of the same type
18. ✴ CodeNarc includes 357 rules
✴ Splited in 22 rulesets
✴ You can contribute with new rules:
https://github.com/CodeNarc/CodeNarc
R U L E S
@alberto_deavila
19. R U L E S
@alberto_deavila
What kind of rules are defined?
20. ✴ Basic: empty blocks/classes and many others
✴ DRY: repeated data like strings, numbers…
✴ Exceptions: alert about bad uses of exceptions
✴ Formatting: spaces, docs, blank lines…
✴ Grails: domain with service, duplicate
constraints and mapping, mass assignment…
R U L E S
@alberto_deavila
21. ✴ Groovyism: explicit calls, GString map keys
✴ JUnit: ignoreRest, unnecessary fails…
✴ Naming: validate the files and classes names
✴ Size: methods with too many params,
methods too larges…
✴ Unused: unused variables, methods…
R U L E S
@alberto_deavila
22. ✴ Braces
✴ Concurrency
✴ Convention
✴ Design
✴ Enhanced
✴ Generic
R U L E S
@alberto_deavila
✴ Imports
✴ JDBC
✴ Logging
✴ Security
✴ Serialization
✴ Unnecessary
23. R U L E S
@alberto_deavila
Rules configuration
24. ✴ Common configuration:
✴ enabled = false
✴ applyToClassNames = ‘*Service’
✴ doNotApplyToClassNames = ‘*Spec,*Util’
✴ priority = 1
✴ Some rules can be configured particularly
R U L E S
@alberto_deavila
25. C O N F I G I N G R A I L S 3
Config in Grails 3
26. C O N F I G I N G R A I L S 3
@alberto_deavila
✴ Add plugin to build.gradle :
apply plugin: 'codenarc'
codenarc {
toolVersion = '0.27.0'
configFile = file("${rootProject.projectDir}/config/codenarc/
rules.groovy")
reportFormat = 'html'
ignoreFailures = true
}
27. C O N F I G I N G R A I L S 3
@alberto_deavila
✴ Create the rules file:
ruleset {
description 'Grails-CodeNarc Project RuleSet'
ruleset('rulesets/basic.xml')
ruleset('rulesets/braces.xml')
ruleset('rulesets/grails.xml')
}
28. C O N F I G I N G R A I L S 3
@alberto_deavila
✴ Execute $ ./gradlew app:check
✴ Open test report to check violations:
30. C U S T O M R U L E S
@alberto_deavila
✴ Define the ruleset:
<ruleset xmlns="http://codenarc.org/ruleset/1.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://codenarc.org/ruleset/1.0 http://
codenarc.org/ruleset-schema.xsd"
xsi:noNamespaceSchemaLocation="http://codenarc.org/ruleset-
schema.xsd">
<description>Extra Grails rules</description>
<rule class='org.codenarc.rule.grails.GrailsTransactionalRule'/>
</ruleset>
31. C U S T O M R U L E S
@alberto_deavila
✴ Create the rule config:
class GrailsTransactionalRule extends AbstractAstVisitorRule {
int priority = 2
String name = 'GrailsTransactional'
Class astVisitorClass = GrailsTransactionalVisitor
}
32. C U S T O M R U L E S
@alberto_deavila
✴ Create the rule implementation:
@CompileStatic
class GrailsTransactionalVisitor extends AbstractAstVisitor {
@Override
void visitAnnotations(AnnotatedNode node) {
node.annotations.each { AnnotationNode annotationNode ->
String annotation = annotationNode.classNode.text
if (annotation ==
“org.springframework.transaction.annotation.Transactional”) {
addViolation(node, “Error msg”)
}
}
super.visitAnnotations(node)
}
}
33. C U S T O M R U L E S
@alberto_deavila
✴ Create a Gradle project with that rule
✴ Some other config needed
✴ More info:
http://guides.grails.org/grails-codenarc/guide/
index.html#writingCustomRule
35. C O N C L U S I O N S
@alberto_deavila
✴ Easy to configure and use
✴ Show us a lot of information
✴ Customize the rules to apply
✴ Create your own rules
✴ Maintains a unified code style
✴ You should use it
36. M O R E I N F O
@alberto_deavila
✴ Grails Guide:
http://guides.grails.org/grails-codenarc/
guide/index.html
✴ Codenarc doc:
http://codenarc.sourceforge.net/
✴ Codenarc repo:
https://github.com/CodeNarc/CodeNarc