On Github beckje01 / gr8eu-2013-static-talk
http://bit.ly/gr8-static
Information about your code that is properties of your code. Mostly obtained by looking at your code statically not looking at runtime or dynamic data.
Items there are 'easy' rules to enforce.
In JavaScript doing:
if(x==y)
vs
if(x===y)
The architectural decisions you have made. For example choosing to always use mixins for adding shared functionality to controllers vs using inheritance. Just architectural flaws in general.
In general these tools are not going to help you from doing something silly like having a great distributed system that that all depends on a single MySQL node.
"CodeNarc analyzes Groovy code for defects, bad practices, inconsistencies, style issues and more." - CodeNarc
There is a grails plugin that you can use to give you CodeNarc. BuildConfig:
plugins { compile ":codenarc:0.18.1" ... }
codenarc.processTestUnit = false codenarc.processTestIntegration = false codenarc.propertiesFile='grails-app/conf/codenarc.properties' codenarc.ruleSetFiles = "rulesets/basic.xml,rulesets/exceptions.xml, rulesets/imports.xml,rulesets/grails.xml, rulesets/unused.xml, rulesets/size.xml, rulesets/concurrency.xml,rulesets/convention.xml,rulesets/design.xml,rulesets/groovyism.xml,rulesets/imports.xml,rulesets/logging.xml" codenarc.reports = { MyXmlReport('xml') { // The report name "MyXmlReport" is user-defined; Report type is 'xml' outputFile = 'target/test-reports/CodeNarcReport.xml' // Set the 'outputFile' property of the (XML) Report title = 'CodeNarc' // Set the 'title' property of the (XML) Report } MyHtmlReport('html') { // Report type is 'html' outputFile = 'target/test-reports/CodeNarcReport.html' title = 'CodeNarc HTML' } }
I use the properties file for adjusting individual rules.
GrailsPublicControllerMethod.enabled=false CatchException.enabled=false CatchThrowable.enabled=false ThrowException.enabled=false ThrowRuntimeException.enabled=false GrailsStatelessService.enabled=false NestedBlockDepth.maxNestedBlockDepth=3
You will want the first line as it is the work around for GPCODENARC-30
Some rules will need some extra config, adding GMetrics will allow many rules of the size.xml file to work.
dependencies { compile 'org.gmetrics:GMetrics:0.6' ... }
Use an annotation to suppress any false positives
@SuppressWarnings('DuplicateStringLiteral') class MyClass { def x = 'x' def y = 'x' }
A code coverage tool for the jvm, used via the code coverage grails plugin.
plugins { test ":code-coverage:1.2.6" ... }
In BuildConfig.groovy you can set up exclusions like the following:
coverage { exclusions = [ '**/radar/**', '**/FacebookConfig*' ] }
Most of the time you will be running this on the CI which you should be passing the xml flag.
grails test-app -coverage -xml
C.R.A.P. Metric With both CodeNarc and Cobertura we can get the CRAP metric working.
In the codenarc.properties file set the following:
CrapMetric.coberturaXmlFile=file:target/test-reports/cobertura/coverage.xml
Warning: JSLint will hurt your feelings. - JSLint
JavaScript plays a big part of most web applications so we need to treat it with the same rigor as our Groovy code.
In a JSLintConfig.groovy file
jslint.options = "white" jslint.directory = "web-app/js" jslint.includes = "**/*.js" jslint.excludes = "**/*.min.js, **/i18n/**/*.js, **/prototype/*.js,**/*-min.js,**/*.pack.js" jslint.haltOnFailure = false jslint.preDef = "\$" jslint.reports = { MyXmlReport('xml') { // The report name "MyXmlReport" is user-defined; Report type is 'xml' destfile = 'target/test-reports/jslint.xml' // Set the 'outputFile' property of the (XML) Report } MyHtmlReport('report') { // Report type is 'html' destfile = 'target/test-reports/jslint.html' } }
Use Jenkins paired with a number of plugins to really trend and deal with all this information.
This will parse JSLint and CodeNarc xml files allowing us to trend the number of warnings. It will group the type of violation into high, normal, and low priorities.
The plugin also allows us to set acceptable levels of violations.
In Jeknins you see sunny, stormy, or yellow ball. Sunny is great while stormy is there is something wrong while yellow is considered unstable.
Turn off rules that are too hard to fix at this time or you have already made a decision that is against a rule.
Look for general trends. As well as cutting out any junk from the tools.