{"id":5787,"date":"2016-11-08T18:01:07","date_gmt":"2016-11-08T18:01:07","guid":{"rendered":"http:\/\/www.jitendrazaa.com\/blog\/?p=5787"},"modified":"2016-11-08T18:04:20","modified_gmt":"2016-11-08T18:04:20","slug":"automated-code-review-for-apex-in-salesforce-static-code-analysis-video","status":"publish","type":"post","link":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/automated-code-review-for-apex-in-salesforce-static-code-analysis-video\/","title":{"rendered":"Automated Code review for Apex in Salesforce &#8211; Static code analysis &#8211; Video"},"content":{"rendered":"<p style=\"text-align: justify;\">PMD is very well known source code analyzer for Java, android and many more languages. Good news for us (Salesforce developers) is , that it supports now <a href=\"https:\/\/pmd.github.io\/pmd-5.5.2\/pmd-apex\/index.html\">Apex<\/a>. You might be thinking how can we make PMD as part of our daily life ?<\/p>\n<p>There are multiple ways<\/p>\n<ol>\n<li>We can run static code analysis standalone<\/li>\n<li>It can be part of ANT build to generate error reports<\/li>\n<li><a href=\"https:\/\/www.jitendrazaa.com\/blog\/tag\/continuous-integration\/\">Jenkins <\/a>can use it to generate nice report around code quality<\/li>\n<li>Eclipse can use it as a plugin to generate report<\/li>\n<\/ol>\n<p style=\"text-align: justify;\">In this blog post, we will discuss option 1, that is running it as a standalone application to generate code quality report.<\/p>\n<p style=\"text-align: justify;\">First\u00a0<strong>Step<\/strong> is to download jar file of latest PMD distribution from <a href=\"https:\/\/pmd.github.io\/\">here<\/a>.<!--more--><\/p>\n<p style=\"text-align: justify;\">Next step is to define some rules for your code base. Sample rules can be seen <a href=\"https:\/\/pmd.github.io\/pmd-5.5.2\/pmd-apex\/rules\/index.html\">here<\/a>. We can also create our\u00a0own rule as per need. For this blog post, I would be using below rules.<\/p>\n<p><span style=\"text-decoration: underline;\"><strong>Apex Rules.xml<\/strong><\/span><\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;?xml version=&quot;1.0&quot;?&gt;\r\n&lt;ruleset name=&quot;Apex Rules&quot; xmlns=&quot;http:\/\/pmd.sourceforge.net\/ruleset\/2.0.0&quot; xmlns:xsi=&quot;http:\/\/www.w3.org\/2001\/XMLSchema-instance&quot; xsi:schemaLocation=&quot;http:\/\/pmd.sourceforge.net\/ruleset\/2.0.0 http:\/\/pmd.sourceforge.net\/ruleset_2_0_0.xsd&quot;&gt;\r\n\t \r\n\t&lt;rule name=&quot;AvoidDeeplyNestedIfStmts&quot; message=&quot;Avoid creating deeply nested if-then statements since they are harder to read and error-prone to maintain.&quot; class=&quot;net.sourceforge.pmd.lang.apex.rule.complexity.AvoidDeeplyNestedIfStmtsRule&quot;&gt;\r\n      &lt;description&gt;\r\n      Avoid creating deeply nested if-then statements since they are harder to read and error-prone to maintain.\r\n      &lt;\/description&gt;\r\n        &lt;priority&gt;3&lt;\/priority&gt; \r\n      &lt;example&gt;\r\n\t\t&lt;!&#x5B;CDATA&#x5B; public class Foo { public void bar(Integer x, Integer y, Integer z) { if (x&gt;y) {\r\n\t\t\t\t\t\tif (y&gt;z) {\r\n\t\t\t\t\t\t\tif (z==x) {\r\n\t\t\t\t\t\t\t\t\/\/ !! too deep\r\n\t\t\t\t\t\t\t}\r\n\t\t\t\t\t\t}\r\n\t\t\t\t\t}\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\t]]&gt;\r\n      &lt;\/example&gt;\r\n    &lt;\/rule&gt;\r\n\t\r\n\t&lt;rule name=&quot;ExcessiveParameterList&quot; message=&quot;Methods with numerous parameters are a challenge to maintain, especially if most of them share the same datatype. These situations usually denote the need for new objects to wrap the numerous parameters.&quot; class=&quot;net.sourceforge.pmd.lang.apex.rule.complexity.ExcessiveParameterListRule&quot;&gt;\r\n      &lt;description&gt;\r\n      Methods with numerous parameters are a challenge to maintain, especially if most of them share the same datatype. These situations usually denote the need for new objects to wrap the numerous parameters.\r\n      &lt;\/description&gt;\r\n        &lt;priority&gt;3&lt;\/priority&gt; \r\n      &lt;example&gt;\r\n\t\t&lt;!&#x5B;CDATA&#x5B; \r\n\/\/ too many arguments liable to be mixed up \r\npublic void addPerson(int birthYear, int birthMonth, int birthDate, int height, int weight, int ssn) \r\n{ ... } \r\n\r\n\/\/ preferred approach public void addPerson(Date birthdate, BodyMeasurements measurements, int ssn) \r\n{ ... } \r\n]]&gt;\r\n      &lt;\/example&gt;\r\n    &lt;\/rule&gt;\r\n\t\r\n\t &lt;rule name=&quot;ExcessiveClassLength&quot; message=&quot;Excessive class file lengths are usually indications that the class may be burdened with excessive responsibilities that could be provided by external classes or functions. In breaking these methods apart the code becomes more managable and ripe for reuse.&quot; class=&quot;net.sourceforge.pmd.lang.apex.rule.complexity.ExcessiveClassLengthRule&quot;&gt;\r\n      &lt;description&gt;\r\n      Excessive class file lengths are usually indications that the class may be burdened with excessive responsibilities that could be provided by external classes or functions. In breaking these methods apart the code becomes more managable and ripe for reuse.\r\n      &lt;\/description&gt;\r\n        &lt;priority&gt;3&lt;\/priority&gt; \r\n      &lt;example&gt;\r\n\t\t&lt;!&#x5B;CDATA&#x5B; \r\npublic class Foo { public void bar(Integer x, Integer y, Integer z) { if (x&gt;y) {\r\n\t\t\t\t\t\tif (y&gt;z) {\r\n\t\t\t\t\t\t\tif (z==x) {\r\n\t\t\t\t\t\t\t\t\/\/ !! too deep\r\n\t\t\t\t\t\t\t}\r\n\t\t\t\t\t\t}\r\n\t\t\t\t\t}\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\t]]&gt;\r\n      &lt;\/example&gt;\r\n    &lt;\/rule&gt;\r\n\t\r\n\t&lt;rule name=&quot;NcssMethodCount&quot; message=&quot;This rule uses the NCSS (Non-Commenting Source Statements) algorithm to determine the number of lines of code for a given method. NCSS ignores comments, and counts actual statements. Using this algorithm, lines of code that are split are counted as one.&quot; class=&quot;net.sourceforge.pmd.lang.apex.rule.complexity.NcssMethodCountRule&quot;&gt;\r\n      &lt;description&gt;\r\n      This rule uses the NCSS (Non-Commenting Source Statements) algorithm to determine the number of lines of code for a given method. NCSS ignores comments, and counts actual statements. Using this algorithm, lines of code that are split are counted as one.\r\n      &lt;\/description&gt;\r\n        &lt;priority&gt;3&lt;\/priority&gt; \r\n      &lt;example&gt;\r\n\t\t&lt;!&#x5B;CDATA&#x5B; \r\npublic class Foo extends Bar \r\n{ \r\n\/\/this method only has 1 NCSS lines \r\npublic Integer methd() { \r\nsuper.methd(); \r\nreturn 1; \r\n} } \r\n]]&gt;\r\n      &lt;\/example&gt;\r\n    &lt;\/rule&gt; \r\n&lt;\/ruleset&gt;\r\n<\/pre>\n<p style=\"text-align: justify;\">In above sample rule file, if we observe carefully, each rule is represented by\u00a0<strong>rule<\/strong> tag. we can give it any\u00a0<strong>name\u00a0<\/strong>and then\u00a0<strong>message\u00a0<\/strong>to be displayed in report.\u00a0<strong>class<\/strong> represents respective rule written in Java class by PMD and this information would be available <a href=\"https:\/\/pmd.github.io\/pmd-5.5.2\/pmd-apex\/rules\/index.html\">here<\/a>.<\/p>\n<p style=\"text-align: justify;\"><strong>priority<\/strong> can set level of violations and this would differ as per your project. We can categorize level of violations for each rule and it helps us in reporting code quality of developer team.<\/p>\n<p style=\"text-align: justify;\">Final step is to run below command from shell \/ command prompt. <a href=\"http:\/\/pmd.sourceforge.net\/pmd-5.4.1\/usage\/running.html\">This documentation<\/a> will help to understand all parameters needed to run PMD from command prompt.<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\npmd -d &quot;path of folder where all apex files exists&quot; -f html -R &quot;path of rule file in our case above file ApexRules.xml&quot; -reportfile &quot;path of report to be generated&quot;\r\n<\/pre>\n<p>Following image shows sample command run<\/p>\n<figure id=\"attachment_5802\" aria-describedby=\"caption-attachment-5802\" style=\"width: 558px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2016\/11\/Sample-PMD-Command-line.jpg?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-5802\" src=\"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2016\/11\/Sample-PMD-Command-line.jpg?resize=558%2C85&#038;ssl=1\" alt=\"Sample command to run PMD from console\" width=\"558\" height=\"85\" srcset=\"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2016\/11\/Sample-PMD-Command-line.jpg?w=558&amp;ssl=1 558w, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2016\/11\/Sample-PMD-Command-line.jpg?resize=300%2C46&amp;ssl=1 300w\" sizes=\"auto, (max-width: 558px) 100vw, 558px\" \/><\/a><figcaption id=\"caption-attachment-5802\" class=\"wp-caption-text\">Sample command to run PMD from console<\/figcaption><\/figure>\n<p>Below is\u00a0sample code quality report generated<\/p>\n<figure id=\"attachment_5803\" aria-describedby=\"caption-attachment-5803\" style=\"width: 840px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2016\/11\/Sample-PMD-report-output.jpg?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" class=\"size-large wp-image-5803\" src=\"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2016\/11\/Sample-PMD-report-output.jpg?resize=840%2C260&#038;ssl=1\" alt=\"Apex code quality report\" width=\"840\" height=\"260\" srcset=\"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2016\/11\/Sample-PMD-report-output.jpg?resize=1024%2C317&amp;ssl=1 1024w, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2016\/11\/Sample-PMD-report-output.jpg?resize=300%2C93&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2016\/11\/Sample-PMD-report-output.jpg?resize=768%2C238&amp;ssl=1 768w, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2016\/11\/Sample-PMD-report-output.jpg?resize=1200%2C372&amp;ssl=1 1200w, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2016\/11\/Sample-PMD-report-output.jpg?w=1905&amp;ssl=1 1905w\" sizes=\"auto, (max-width: 840px) 100vw, 840px\" \/><\/a><figcaption id=\"caption-attachment-5803\" class=\"wp-caption-text\">Apex code quality report<\/figcaption><\/figure>\n<p><strong>Youtube video<\/strong><\/p>\n<p><iframe loading=\"lazy\" title=\"How to conduct automated Apex code review - Salesforce\" width=\"500\" height=\"281\" src=\"https:\/\/www.youtube.com\/embed\/LqhjI4i1eyo?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Using open source PMD tool to generate code quality report for Apex classes<\/p>\n","protected":false},"author":1,"featured_media":5801,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"advanced_seo_description":"","jetpack_seo_html_title":"","jetpack_seo_noindex":false,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"jz_research_post":"","_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[9],"tags":[337,348,294],"class_list":["post-5787","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-salesforce","tag-apex","tag-best-practices","tag-continuous-integration"],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2016\/11\/Automated-Code-review-for-Apex-in-Salesforce-Static-code-analysis.jpg?fit=1138%2C423&ssl=1","jetpack_sharing_enabled":true,"jetpack-related-posts":[{"id":7817,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/automate-and-conquer-how-task-json-can-supercharge-your-salesforce-development\/","url_meta":{"origin":5787,"position":0},"title":"Automate and Conquer: How Task.json Can Supercharge Your Salesforce Development","author":"Jitendra","date":"June 1, 2023","format":false,"excerpt":"Discover the game-changing power of Task.json in Salesforce development with Visual Studio Code (VSCode) and Salesforce DX (SFDX). Streamline your workflow, automate tasks, and boost productivity as you dive into the world of Salesforce development. Explore how Task.json simplifies development with VSCode, accelerates SFDX deployments, and ensures consistency across your\u2026","rel":"","context":"In &quot;Salesforce&quot;","block_context":{"text":"Salesforce","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/salesforce\/"},"img":{"alt_text":"SFDX + Task.json","src":"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2023\/06\/SFDX-Task.json_.png?fit=1200%2C675&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2023\/06\/SFDX-Task.json_.png?fit=1200%2C675&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2023\/06\/SFDX-Task.json_.png?fit=1200%2C675&ssl=1&resize=525%2C300 1.5x, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2023\/06\/SFDX-Task.json_.png?fit=1200%2C675&ssl=1&resize=700%2C400 2x, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2023\/06\/SFDX-Task.json_.png?fit=1200%2C675&ssl=1&resize=1050%2C600 3x"},"classes":[]},{"id":3537,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/consuming-external-webservice-in-apex\/","url_meta":{"origin":5787,"position":1},"title":"Consuming External Web Service in Apex &#8211; Salesforce","author":"Jitendra","date":"October 13, 2013","format":false,"excerpt":"One of the feature we have in Salesforce is that we can easily consume External Web Services. In this article, we will learn step by step demo of consuming Web Service in Apex. There are many public websites available to consume Web Service and one of them, I am using\u2026","rel":"","context":"In &quot;Apex&quot;","block_context":{"text":"Apex","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/salesforce\/apex\/"},"img":{"alt_text":"Generating Apex from WSDL in Salesforce","src":"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2013\/10\/Generating-Apex-from-WSDL-in-Salesforce-1024x345.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2013\/10\/Generating-Apex-from-WSDL-in-Salesforce-1024x345.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2013\/10\/Generating-Apex-from-WSDL-in-Salesforce-1024x345.png?resize=525%2C300&ssl=1 1.5x"},"classes":[]},{"id":2817,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/gantt-chart-in-salesforce-using-jquery-and-json\/","url_meta":{"origin":5787,"position":2},"title":"Gantt Chart in Salesforce using JQuery and JSON","author":"Jitendra","date":"April 18, 2012","format":false,"excerpt":"Tutorial and Example of creating the Gantt Chart in Salesforce using JQuery and JSON","rel":"","context":"In &quot;Apex&quot;","block_context":{"text":"Apex","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/salesforce\/apex\/"},"img":{"alt_text":"Gantt Chart in Salesforce","src":"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2012\/04\/Gantt-Chart-in-Salesforce.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2012\/04\/Gantt-Chart-in-Salesforce.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2012\/04\/Gantt-Chart-in-Salesforce.png?resize=525%2C300&ssl=1 1.5x"},"classes":[]},{"id":4597,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/advance-apex-debugging-in-salesforce-and-best-practices-videos\/","url_meta":{"origin":5787,"position":3},"title":"Advance Apex debugging in Salesforce and best practices &#8211; Videos","author":"Jitendra","date":"June 27, 2015","format":false,"excerpt":"How to resolve apex debug log size limit issue, Difference between Eclipse and developer console, Interactive Apex Debugging, Keyboard shortcuts for Developer console and Other best practices","rel":"","context":"In &quot;Salesforce&quot;","block_context":{"text":"Salesforce","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/salesforce\/"},"img":{"alt_text":"Salesforce Advance Apex debugging","src":"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2015\/06\/Salesforce-Advance-Apex-debugging.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2015\/06\/Salesforce-Advance-Apex-debugging.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2015\/06\/Salesforce-Advance-Apex-debugging.png?resize=525%2C300&ssl=1 1.5x, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2015\/06\/Salesforce-Advance-Apex-debugging.png?resize=700%2C400&ssl=1 2x"},"classes":[]},{"id":6152,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/call-salesforce-rest-api-from-apex\/","url_meta":{"origin":5787,"position":4},"title":"Call Salesforce REST API from Apex","author":"Jitendra","date":"February 27, 2014","format":false,"excerpt":"Use Apex to call Salesforce REST API with sample source code","rel":"","context":"In &quot;Salesforce&quot;","block_context":{"text":"Salesforce","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/salesforce\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":5478,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/delete-components-using-ant-migration-tool-video\/","url_meta":{"origin":5787,"position":5},"title":"Delete Components using Ant Migration tool &#8211; Video","author":"Jitendra","date":"April 27, 2016","format":false,"excerpt":"This video tutorial shows that how we can delete components like Apex class, trigger from Salesforce using ANT migration tool.","rel":"","context":"In &quot;Salesforce&quot;","block_context":{"text":"Salesforce","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/salesforce\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/img.youtube.com\/vi\/3kMGy6OeJAc\/0.jpg?resize=350%2C200","width":350,"height":200},"classes":[]}],"jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts\/5787","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/comments?post=5787"}],"version-history":[{"count":5,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts\/5787\/revisions"}],"predecessor-version":[{"id":5807,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts\/5787\/revisions\/5807"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/media\/5801"}],"wp:attachment":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/media?parent=5787"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/categories?post=5787"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/tags?post=5787"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}