SlideShare a Scribd company logo
Salenda
Let Codenarc check if you

write good code
Greach ‘18
Alberto De Ávila Hernández
A B O U T M E
✴ Software Engineer
✴ Team Lead at Salenda
✴ Groovy & Grails dev
@alberto_deavila
S A L E N D A
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
G O A L S
Goals
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
✴ What’s Codenarc?
✴ Why should you use Codenarc?
✴ Rules
✴ Config in Grails 3
✴ Custom rules
I N D E X
@alberto_deavila
W H AT ’ S C O D E N A R C ?
What’s Codenarc?
✴ 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
✴ Analyzes:
✴ Defects
✴ Bad practices
✴ Inconsistencies
✴ Style issues
✴ …
W H AT ’ S C O D E N A R C ?
@alberto_deavila
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?
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
R U L E S
Rules
R U L E S
@alberto_deavila
What’s a rule?
R U L E S
@alberto_deavila
A function that looks at our code to check if
it meets some conditions
R U L E S
@alberto_deavila
What’s a ruleset?
R U L E S
@alberto_deavila
A group of rules of the same type
✴ 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
R U L E S
@alberto_deavila
What kind of rules are defined?
✴ 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
✴ 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
✴ Braces
✴ Concurrency
✴ Convention
✴ Design
✴ Enhanced
✴ Generic
R U L E S
@alberto_deavila
✴ Imports
✴ JDBC
✴ Logging
✴ Security
✴ Serialization
✴ Unnecessary
R U L E S
@alberto_deavila
Rules configuration
✴ Common configuration:
✴ enabled = false
✴ applyToClassNames = ‘*Service’
✴ doNotApplyToClassNames = ‘*Spec,*Util’
✴ priority = 1
✴ Some rules can be configured particularly
R U L E S
@alberto_deavila
C O N F I G I N G R A I L S 3
Config in Grails 3
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
}
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')
}
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:
C U S T O M R U L E S
Custom rules
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>
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
}
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)
}
}
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
C O N C L U S I O N S
Conclusions
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
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
SalendaThank you!
@alberto_deavila

More Related Content

PPT
Teori gestalt ppt
PDF
GR8Conf 2011: CodeNarc and GMetrics
PDF
How to write maintainable code without tests
PDF
Software Mistakes and Tradeoffs 1st Edition Tomasz Lelek
PDF
Getting Ahead of Delivery Issues with Deep SDLC Analysis by Donald Belcham
PDF
Mine Your Own Code
KEY
Frozen rails 2012 - Fighting Code Smells
PPTX
Integreation
Teori gestalt ppt
GR8Conf 2011: CodeNarc and GMetrics
How to write maintainable code without tests
Software Mistakes and Tradeoffs 1st Edition Tomasz Lelek
Getting Ahead of Delivery Issues with Deep SDLC Analysis by Donald Belcham
Mine Your Own Code
Frozen rails 2012 - Fighting Code Smells
Integreation

Similar to Let Codenarc check if you write good Groovy code (20)

PDF
Effective code reviews
PDF
Reliability Patterns for Distributed Applications
ODP
Path dependent-development (PyCon India)
PDF
Supercharging project health check
PDF
Systems se
PPTX
“One man” development process model
PDF
Clean Code V2
PDF
Andrea Lattuada, Gabriele Petronella - Building startups on Scala
PDF
OpenChain Webinar - AboutCode - Practical Compliance in One Stack – Licensing...
PPTX
Slides for Houston iPhone Developers' Meetup (April 2012)
 
PPTX
CodeQuality.pptx
PDF
Code Review
PDF
Effective code reviews
PDF
Agileee Developers Toolkit In The Agile World
ODP
Path Dependent Development (PyCon AU)
PDF
Code quality par Simone Civetta
PDF
Bára Bühnová: Naučte se taktizovat s pomocí bad code smells a quality tactics
PPTX
A sweet taste of clean code and software design
PDF
JDD2014: Enforcing architecture patterns with static code analysis - Pablo Ba...
PDF
Clean Code .Net Cheetsheets
Effective code reviews
Reliability Patterns for Distributed Applications
Path dependent-development (PyCon India)
Supercharging project health check
Systems se
“One man” development process model
Clean Code V2
Andrea Lattuada, Gabriele Petronella - Building startups on Scala
OpenChain Webinar - AboutCode - Practical Compliance in One Stack – Licensing...
Slides for Houston iPhone Developers' Meetup (April 2012)
 
CodeQuality.pptx
Code Review
Effective code reviews
Agileee Developers Toolkit In The Agile World
Path Dependent Development (PyCon AU)
Code quality par Simone Civetta
Bára Bühnová: Naučte se taktizovat s pomocí bad code smells a quality tactics
A sweet taste of clean code and software design
JDD2014: Enforcing architecture patterns with static code analysis - Pablo Ba...
Clean Code .Net Cheetsheets
Ad

More from Alberto De Ávila Hernández (6)

PDF
Spock data tables plugin for IntelliJ + How to create you own plugin
PDF
Graalvm with Groovy and Kotlin - Madrid GUG 2019
PDF
Graalvm with Groovy and Kotlin - Greach 2019
PDF
Nitro for your Grails App: how to improve performance. Greach '18
PDF
Testing Grails 3, the goob (unit), the bad (integration) and the ugly (functi...
PDF
De Java a Swift pasando por Groovy
Spock data tables plugin for IntelliJ + How to create you own plugin
Graalvm with Groovy and Kotlin - Madrid GUG 2019
Graalvm with Groovy and Kotlin - Greach 2019
Nitro for your Grails App: how to improve performance. Greach '18
Testing Grails 3, the goob (unit), the bad (integration) and the ugly (functi...
De Java a Swift pasando por Groovy
Ad

Recently uploaded (20)

PPTX
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
PPTX
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
PPTX
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
PDF
Digital Logic Computer Design lecture notes
PPTX
CYBER-CRIMES AND SECURITY A guide to understanding
PDF
Automation-in-Manufacturing-Chapter-Introduction.pdf
PPTX
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
PDF
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
DOCX
573137875-Attendance-Management-System-original
PPT
Introduction, IoT Design Methodology, Case Study on IoT System for Weather Mo...
PPTX
UNIT 4 Total Quality Management .pptx
PDF
Unit I ESSENTIAL OF DIGITAL MARKETING.pdf
PDF
Operating System & Kernel Study Guide-1 - converted.pdf
PPT
Mechanical Engineering MATERIALS Selection
PPTX
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
PPTX
Geodesy 1.pptx...............................................
PDF
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
PPT
introduction to datamining and warehousing
PDF
Well-logging-methods_new................
PDF
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
Digital Logic Computer Design lecture notes
CYBER-CRIMES AND SECURITY A guide to understanding
Automation-in-Manufacturing-Chapter-Introduction.pdf
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
573137875-Attendance-Management-System-original
Introduction, IoT Design Methodology, Case Study on IoT System for Weather Mo...
UNIT 4 Total Quality Management .pptx
Unit I ESSENTIAL OF DIGITAL MARKETING.pdf
Operating System & Kernel Study Guide-1 - converted.pdf
Mechanical Engineering MATERIALS Selection
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
Geodesy 1.pptx...............................................
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
introduction to datamining and warehousing
Well-logging-methods_new................
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks

Let Codenarc check if you write good Groovy code

  • 1. Salenda Let Codenarc check if you
 write good code Greach ‘18 Alberto De Ávila Hernández
  • 2. A B O U T M E ✴ Software Engineer ✴ Team Lead at Salenda ✴ Groovy & Grails dev @alberto_deavila
  • 3. S A L E N D A
  • 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
  • 5. G O A L S Goals
  • 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
  • 8. W H AT ’ S C O D E N A R C ? What’s Codenarc?
  • 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
  • 13. R U L E S Rules
  • 14. R U L E S @alberto_deavila What’s a rule?
  • 15. R U L E S @alberto_deavila A function that looks at our code to check if it meets some conditions
  • 16. R U L E S @alberto_deavila What’s a ruleset?
  • 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:
  • 29. C U S T O M R U L E S Custom rules
  • 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
  • 34. C O N C L U S I O N S Conclusions
  • 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