« Emotional Design, Affective Computing and Social Animals | Main | Citibank: Your touch targets are too small »
Wednesday
Nov232011

Better code searching with ack

Have you ever needed to find all references to a certain Apex class across an entire code base? If you're a Force.com developer, I'm sure you have. And what if you're not in the Eclipse-based IDE...perhaps you're using the Force.com Migration tool on the command line? I'm guessing you've used a tool like grep.

While grep is still the gold standard, recently I've been using another tool called ack in its place. The ack web site defines it as "designed for programmers with large trees of heterogeneous source code." I've found it to be significantly faster at searching through files, and also has a clean, simple way to search within only certain types of files.

Example

Let's say I installed the LinkForce app into a developer edition org and wanted to explore the code. After setting up this org's code in a new Eclipse project, I can navigate to the workspace directory on the command line and run the following:

ack Short_URL__c

So, again, the above command outputs a simple list of all files that reference that particular Custom Object.

That's pretty good, but let's imagine what I really want is to find custom code (Apex and Visualforce) that refers to that object. Rather than piping the output of a separate find command as an input, I can simply execute the following:

ack --type=apex Short_URL__c

I've configured a file type called apex that ack interprets as Apex Classes/Triggers and Visualforce Pages/Components. This way, I avoid superfluous search results like the definition of the Custom Object itself. ack natively understands many common file types, and can easily support any number of custom types.

Oddly enough, a blog post can't illustrate one of ack's greatest strengths: speed. If you search through large source trees often, you'll be amazed at how quickly the results are returned. The vast majority of my searches complete in less than one second.

Installation

The main ack web site has information about how to install the tool, and I actually used the AckMate plugin for TextMate since I'm a Mac user. In addition to enabling ack as the search tool within TextMate, it's possible to use that same script on the command line by creating a special symbolic link.

The Force.com-specific configuration I created can be found on GitHub as well.

The first line defines the apex file type as consisting of Apex Classes, Triggers, Visualforce Pages and Components. The second line defines another type simply called force for other metadata like Objects, Apps and Profiles. You can treat this as a starting point and break out more or less file types to include Workflows, Page Layouts, etc. Finally, the third line of the configuration specifies that search results will be presented one page at a time using a command line tool called less (which also supports color coding).

Conclusion

If you develop on the Force.com platform and find the command line more efficient, ack can really speed up your work. I'd encourage you to try it if you've never ventured beyond what Eclipse offers.

References (5)

References allow you to track sources for this article, as well as articles that were written in response to this article.

Reader Comments

There are no comments for this journal entry. To create a new comment, use the form below.

PostPost a New Comment

Enter your information below to add a new comment.
Author Email (optional):
Author URL (optional):
Post:
 
All HTML will be escaped. Textile formatting is allowed.