PMD is a well known Static code analyzer for Java and many other languages. It also supports Salesforce Apex as well.

Source code analysis is important during development cycle to point out weaknesses in code and to improve overall quality. Examples are:

  • Code Complexity Measures (e.g. Lines of Code, Nesting Level, Code Clones)
  • Dynamic Related Measures (e.g. Code Coverage, Bugs per Line of Code, Feature Usage)

For Salesforce Apex, the freeware PMD is a recommended tool to carry out Static Code Analysis. It provides an easy to use Plug-in for Eclipse and comes with a prebuilt rule set which is deployable right after installation:

  • ApexUnit:        These rules deal with different problems that can occur with Apex unit tests. (Rules: ApexUnitTestClassShouldHaveAssertsApexUnitTestShouldNotUseSeeAllDataTrue)
  • Braces:            The Braces ruleset contains rules regarding the use and placement of braces. (Rules: IfStmtsMustUseBracesWhileLoopsMustUseBracesIfElseStmtsMustUseBracesForLoopsMustUseBraces)
  • Complexity:    The Complexity ruleset contains rules that find problems related to code size or complexity. (Rules: AvoidDeeplyNestedIfStmts, ExcessiveParameterList, ExcessiveClassLength, NcssMethodCount, NcssTypeCount, NcssConstructorCount, StdCyclomaticComplexity, TooManyFields, ExcessivePublicCount)
  • Performance: The Performance ruleset contains a collection of good practices which should be followed. (Rules: AvoidSoqlInLoopsAvoidDmlStatementsInLoops)

General information on PMD and the rules available for APEX:

Installation:

There are multiple ways to use PMD in Salesforce Apex code, however this blog will focus on #1 to set-up standalone static code analysis in Eclipse

  • We can run static code analysis standalone
  • It can be part of ANT build to generate error reports
  • Jenkins can use it to generate nice report around code quality
  • Eclipse can use it as a plug-in to generate report

Steps to be followed during Installation and set-up PMD in Eclipse:

  1. In Eclipse, click on Help -> Install New Software.
  2. Click on Add.
  3. Enter the following:

and click OK.

  1. You should see PMD for Eclipse 4. Select the checkbox next to it and click Next >.
  2. You’ll need to accept the license and confirm you want to install a plugin that is not digitally signed. Go ahead and install it anyway.
  3. Restart eclipse. 

Run PMD:

 

Right click on project -> Property -> Select PMD

 

It’ll show you the code violation.

 

Read More

Welcome back! This is the second part of our Conga journey. If you haven’t yet, make sure to read the first part: Conga’ing our way to automated documents.

Quick recap: In the first part we created the button/link that we’re going to use as well as the contract template itself that we will send out to employees.

Continue reading Sign the Conga line!

Read More