Introducing the Titanium Code Processor

Today we’re here to talk about an exciting new tool being integrated with the Titanium family of products: the Titanium Code Processor. We’re always striving to help you generate optimal code and kickass mobile apps, so we’re delivering the Code Processor to perform in-depth dynamic analysis of your Titanium Mobile projects to optimize your code and help reduce errors. The Code Processor performs various types of error checking, code conformance, and optimizations. It is composed of a core analysis engine that performs generic analysis, and a plugin architecture used to analyze the results from the core analysis engine.

The core analysis engine is a specialized JavaScript engine that has been designed to work in an offline environment. By performing dynamic analysis, instead of the more traditional static analysis, we are able to obtain significantly more accurate results.

The core analysis engine, by itself, can only report runtime exceptions. Plugins tie into the core analysis engine to do things that are of much more value. There are currently 6 analysis plugins, with more on the way:

  • Analysis Coverage: determines how much of a project was analyzed by the code processor. This is useful for finding dead code as well as debugging the Code Processor itself.
  • Require Finder: determines all modules/files required by the project.
  • Titanium Deprecation Finder: throws a warning anytime a deprecated API is used.
  • Titanium Platform Validator: makes sure that all references to platform specific APIs are not referenced by the wrong platforms and throws an error if they are used incorrectly.
  • Titanium API Usage Finder: finds all Titanium APIs used by the project.
  • Titanium Include Finder: determines all files Ti.include()’d by the project.

To show the power of the code processor, consider the following two buggy files:

The code processor, when run with exception recovery enabled and for iphone, gives the following results:

Here we see that the Code Processor correctly identified that Ti.Platform.Android was being invalidly accessed when compiled for iphone. But what happened next? This would normally throw an exception, but since we have exception recovery enabled, it returns an “unknown” value. Since the code processor doesn’t know if it’s truthy or falsey, it evaluates both branches of the if statement, hence the two exceptions listed for a and b being unknown identifiers.

Sound interesting? To start experimenting with the Code Processor, make sure you have the Titanium CLI installed and then install the code processor using npm:

[sudo] npm install -g titanium-code-processor

To run the code processor:

titanium-code-processor -o [platform] [path-to-project]

For more information and advanced usage options, check out the Code Processor on GitHub. Since this product is still beta, we could really use your help! If you find any problems with the code processor, please let us know at the CLI Google Group. There is also a reporting mechanism built into the code processor itself. By adding the -r flag, the results will be uploaded to ACS for us to review. You can also use the -m flag to add a message to the report.

Update: The command usage information in this blog post is outdated. Please visit the titanium-code-processor homepage on GitHub for the most up-to-date information.