TWS Blog

Writing better Apex with PMD support in The Welkin Suite

The Welkin Suite
Posted by username
Apr 25, 2017 757

Salesforce can be regarded a unique development platform from a lot of different points of view. First off, it is hard to overstate the importance of the performance in Salesforce owing to its cloud nature, and I can hardly find any platforms with a similar accent on performance - maybe, microelectronics? Second, compared to other platforms, it is much easier to find a 5+ years old organization (even, 10+ years), which is not treated as a "legacy" project, but is continuously updated to the latest Salesforce versions, and which has business logic that is constantly being updated and changed.

I believe that even these 2 characteristics are absolutely enough to bring the topic of code quality to one of the highest important positions in Salesforce development. Frankly speaking, it is strange that a platform with such high code quality requirements barely has any tools in this area compared to other ones - for example .NET, Java, or even C++. The only available tool for Apex that allows you to do some kind of code quality checks is PMD Apex (not linking the latest version because documentation for the latest version is not yet available).

Taking this into account, it gets pretty obvious that The Welkin Suite should provide out-of-the-box support for these tools - to allow those, who care about their code quality, performance, and maintainability, to do this much easier.
In general, PMD is a standalone source code scanning tool designed for finding various potential issues in your codebase according to the ruleset that you configure. PMD Apex in its turn is a set of rules, which you can use for Apex code inspection.
Working with PMD can be described in a couple steps:
  1. Install/unpack PMD itself.
  2. Define a ruleset which you'd like to use.
  3. Point PMD to your files, and get scan results.
  4. Analyze the results and resolve the issues.

While we can't help (for now) with the last step, we can easily handle everything else, so let's go through the details of how we are going to do this.

Installing PMD

For all "fresh" installations, we will bundle the latest stable and tested version of PMD as a part of TWS installation in the same way as we are doing with KDiff, so you won't need to worry about this. We are not yet sure how to handle this in the best way for the existing users and for the future updates because of the size of the PMD (about 60MB), but we will definitely find out a good way to do this.

Ruleset configuration

While you can configure a ruleset manually with just a text editor, this will surely consume some time. Hence, we don't think this would be the best way of doing things. This is why we will provide a ruleset configurator, which will have the information about all the available PMD Apex rules in a friendly manner, allowing you to select which rules you would like to use in the ruleset, and which parameters each rule should have. So instead of going through the documentation and extracting the information from there - you'll be able to create the best ruleset for you and your team within a couple minutes.

PMD Apex Ruleset configurationPMD Apex ruleset configuration

As another option, The Welkin Suite will be bundled with the default ruleset used by the Code Climate Engine, as well as our own ruleset, which we find to be suitable for most developers and projects.

Scanning your code

There are a lot of different ways to trigger the scan process from the IDE, however, for now, we will only discuss two of them.

The first approach implies automatically running PMD before the build process for all the changed files. This way, if any of the files would have PMD issues, the build won't occur, and the potentially "smelly" code won't appear on the organization. Isn't this a good way to save time for the "future you"? (smile) However, such an "aggressive" way of running code scan requires some attention from your side in terms of which rules should trigger build errors, and which rules should be treated just as warnings.

Another option will be to manually execute PMD for any file or a set of files in your project. This will come in very handy for those developers who just start using PMD, and might potentially have some quality or styling issues in the existing codebase. In this case, you just need to periodically execute PMD for some of your classes or triggers, and resolve the issues in the code one-by-one.

The Welkin Suite options PMDThe Welkin Suite options PMD

What is interesting here is that we will provide you with an option to configure different rulesets for different projects, as well as to define a "global" ruleset that can be used if not project-level configuration is found. And you will also be able to set PMD as a pre-build action on a per-project or global basis.

Project properties PMDProject properties PMD

Analyzing scan results

While we won't analyze and fix results of the scan for you, we will do our best to provide you the scan results in the best way possible - they will be shown in the Error List, and highlighted in the Apex Editor.

PMD Report The Welkin SuitePMD Report The Welkin Suite

In addition to the Error List, we will implement an extra tool window with scan results, where you will be able to see all the issues, read more details about each of them, and easily navigate to the code that violates a PMD rule. As it is usual for TWS, there will also be options to filter the results, or just narrow them down to a certain category that you are interested in.

We hope that PMD support, together with our new Apex Code Assistance functionality coming in our Spring release on May 22nd will help you to write better code with less effort!

PMD Apex Support

Your comment may be the first

    Please log in to post a comment