Automated Drupal Code Improvements

Posted on Tue, 10/22/2013 - 22:19

The Coder module ( is well known for assisting developers in producing code up to snuff with the community defined standards. Such standards have been integral in helping the community grow in a consistent manner.

The ultimate goal is to find an automated way to help developers out. Such examples include a code review and performing routine code manipulation. The standard for analyzing code leverages known static analysis approaches. There are two principle goals for code: consistency and simplicity



To understand what consistent code looks like, we need to understand Drupal itself. Drupal has APIs that are intended to provide consistency for events (the hook system) and core functionality (functions and libraries of code). Consistent code can then be viewed as code that properly leverages the APIs. For example, someone who doesn't understand the Database API may be inclined to use PHP's native mysql_query function. Use of this function would not be consistent with other Drupal modules.

Additionally, the Drupal community has adopted development standards (see: This defines how code should be formatted, documented, etc. Participating in such standards welcomes community involvement and makes your code more readable to others who leverage Drupal.



One of my favorite presentations from DrupalCon Denver was "Development By The Numbers" ( During this presentation, Anthony Ferrara demonstrates metrics for evaluating code complexity. His main claim is that limiting complexity increases quality (and, in this case, simplicity). 

Complexity can be measured in a number of different ways (seriously, check out the slides). One of the more well-known metrics is cyclomatic complexity which statically analyzes code to find methods/functions with a high number of potential branches. This is a useful metric for identifying code that would be difficult to test. If code is difficult to test, its even more difficult to guarantee correctness. Complex code then becomes prime candidates for refactoring.



While Coder has some really nice tools to use within Drupal itself, I believe some of it's most powerful features happen with Drush. Drush commands can then be integrated into a development workflow within a continuous integration solution. Let's consider the following:


drush coder-format <custom module name here> 

This is a slick one liner to consistently format all code in a module. Boom! John Madden style.


drush coder-review <custom module name here> 

Again, another slick one liner to identify improper use of Drupal's APIs and a myriad of other things to ensure consistency.


drush coder <custom module name here> --sniffer

By running that command, this returns the cyclomatic complexity of a module to analyze simplicity (enabled by this patch: Note: Ferrara also mentions the command line tool PHPLOC as an option.



While some may argue about the ways and means of making code more consistent and simplistic, few could disagree with the benefits of doing so. Hopefully this post shed some light into some potential automated means of solving these challenges.





development drupal