27

I would like to search a large project containing many classes, pages, components and triggers for unused methods and variables. How should I do that?

Thanks!

2 Answers 2

22

The Tooling API can be used to retrieve the SymbolTable for each apex class. This Symbol Table will tell you the methods that are defined in the apex class, and the references to methods that are consumed in the externalReferences.

By scanning the methods and corresponding references in each apex classes symbol table you can find methods that aren't called by other apex classes.

Happily, Andrew Fawcett has already made such a tool as a canvas app. See Spring Cleaning Apex Code with the Tooling API.

Note, as this is only scanning the apex classes it won't detect declarative references from Visualforce markup.

2
  • Thanks Daniel, for the plug, i would love to move this tool on, however i am waiting as you say for the VF references to show up and also avoid the need to recompile everything in the org to get at the SymbolTable external references. I'll keep a close eye on it and am also in contact with the Tooling API developer, so will keep pushing as am I am keen to deliver a tool like this and enable other fancy static analysis tools such as code complexity reports. Though the other thought i also have is Salesforce making the Apex grammer available directly.... ;-) Apr 29, 2014 at 10:23
  • I've been after an Apex grammar for some time, ref Grammar for creating an Apex parser. I get the appeal of the Tooling API, but surely they must have a defined grammar somewhere internally. I'll continue to to wait patiently on the Tooling API :) Apr 29, 2014 at 23:51
-2

Check out this nice plugin for Eclipse: http://codescan.villagechief.com/eclipse/:

The VillageChief Apex Code Quality tools are the only tool to manage code quality for the Force.com platform. It offers rich visual reporting, lots of metrics, and time-lines to track improvements. The Enterprise version can also be used as a continous integration (CI) tool to continuously run tests on your Force.com Apex unit tests.

Examples of Warnings:

  • Unused fields
  • Long class, field and method names
  • SOQL in loops
  • Duplicate literals
  • Coding standards: naming conventions, fields at top of file, etc
  • Redundant code
  • Code simplification suggestions
2
  • Didn't work. Hosed my Eclipse and I had to uninstall. I've tried emailing these Village Chief people and even calling them in Australia but no luck getting a response.
    – Andrew
    Jun 9, 2014 at 1:38
  • I eventually got in touch w/the VillageChief guys and got it working. There are a few versions of their CodeScan tool floating around. One is an Eclipse plugin - don't use it. Another is an AWS EC2 AMI - don't use it. You want the standalone sonarqube plugin.
    – Andrew
    Jul 24, 2014 at 15:21

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .