{"id":17045,"date":"2015-01-31T23:12:36","date_gmt":"2015-01-31T17:42:36","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=17045"},"modified":"2015-07-24T16:58:42","modified_gmt":"2015-07-24T11:28:42","slug":"grails-clean-code-configure-codenarc-plugin","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/grails-clean-code-configure-codenarc-plugin\/","title":{"rendered":"Grails Clean Code &#8211; Configure Codenarc Plugin"},"content":{"rendered":"<p>Grails\u00a0gives us the freedom to create web applications quickly and with maximum productivity, but with the fast development, we often leave the basic code quality rules and after a few months of project we end up so much messy code that even we are not able to read it, forget about understanding it.<\/p>\n<p>Codenarc really helps to identify what is not expected from our code. We can setup the <a title=\"Grails codenarc plugin\" href=\"http:\/\/grails.org\/plugin\/codenarc\" target=\"_blank\"> codenarc plugin<\/a> easily, but the default rulesets of plugins are sometimes not useful so download the <a title=\"Default codenarc ruleset\" href=\"http:\/\/codenarc.sourceforge.net\/StarterRuleSet-AllRulesByCategory.groovy.txt\" target=\"_blank\">default <\/a>rulesets and save the file in grails-app\/conf\/CodeNarcRules.groovy and then configure the plugin by adding following code in BuildConfig.groovy.<\/p>\n<p>[code]<br \/>\ncodenarc{<br \/>\n\truleSetFiles = &quot;file:grails-app\/conf\/CodeNarcRules.groovy&quot; \/\/ Ruleset file path<br \/>\n\treports = {<br \/>\n\t  HtmlReport(&#8216;html&#8217;) {    \/\/ Report type is &#8216;html&#8217;<br \/>\n\t\toutputFile = &#8216;target\/CodeNarcReport.html&#8217; \/\/Output file name<br \/>\n\t\ttitle = &#8216;My Test Code Narc Report&#8217; \/\/ Title of the file<br \/>\n\t  }<br \/>\n\t}<br \/>\n}<br \/>\n[\/code]<\/p>\n<p>After running the grails conarc command you will see some of the rules doesn&#8217;t fit to <a title=\"Grails Development Services\" href=\"http:\/\/www.tothenew.com\/grails-application-development\">Grails infrastructure<\/a>, so we can just ignore them and update some rules in our CodeNarcRules.groovy file which can make our code quality bit better e.g; the following changes<\/p>\n<p>[code]<br \/>\nEmptyMethod{<br \/>\n   doNotApplyToClassNames = &quot;*Controller&quot;<br \/>\n}<br \/>\nGrailsPublicControllerMethod{<br \/>\n\tenabled = false<br \/>\n}<br \/>\nMethodSize {<br \/>\n        maxLines = 7<br \/>\n        applyToClassNames = &#8216;*Controller&#8217;<br \/>\n}<br \/>\nMethodSize {<br \/>\n        maxLines = 12<br \/>\n        doNotApplyToClassNames = &#8216;*Controller&#8217;<br \/>\n}<br \/>\n[\/code]<\/p>\n<p>We can supress some warning on specific methods as well by just adding an annotation<\/p>\n<p>[code]<br \/>\n@SuppressWarnings([&#8216;MethodSize&#8217;, &#8216;LineLength&#8217;])<br \/>\ndef someMethod(){<br \/>\n\/\/Code<br \/>\n}<br \/>\n[\/code]<\/p>\n<p>We can also integrate it with our build system so that if the code doesn&#8217;t pass the minimum criteria the build gets failed by updating your configurations in BuildConfig.groovy<\/p>\n<p>[code]<br \/>\ncodenarc{<br \/>\n\truleSetFiles = &quot;file:grails-app\/conf\/CodeNarcRules.groovy&quot;<br \/>\n        maxPriority1Violations = 3<br \/>\n        systemExitOnBuildException = true<br \/>\n\treports = {<br \/>\n\t  HtmlReport(&#8216;html&#8217;) {                  \/\/ Report type is &#8216;html&#8217;<br \/>\n\t\toutputFile = &#8216;target\/CodeNarcReport.html&#8217;<br \/>\n\t\ttitle = &#8216;My Test Code Narc Report&#8217;<br \/>\n\t  }<br \/>\n\t}<br \/>\n}<br \/>\n[\/code]<\/p>\n<p>It always good to have tool which can show you your mistakes and guide you write more readable and maintainable code. Because of this plugin I was able to delete hunderds of lines of unused code in my project.<\/p>\n<p>Hope it helps<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Grails\u00a0gives us the freedom to create web applications quickly and with maximum productivity, but with the fast development, we often leave the basic code quality rules and after a few months of project we end up so much messy code that even we are not able to read it, forget about understanding it. Codenarc really [&hellip;]<\/p>\n","protected":false},"author":11,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":10},"categories":[7],"tags":[1614,1615,442,1589],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/17045"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/11"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=17045"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/17045\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=17045"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=17045"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=17045"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}