Keep Moving Forward | X-Team Magazine

Writing Elegant Code in Drupal 7

Written by gerald-villorente | May 2, 2014 4:00:00 AM

I’m a  Drupal developer: Why should I really care about writing elegant code?

It’s very important that we are following the standards in every technology we are working on. In Drupal, coding standards are strictly imposed and treated as a foundation of creating better software. Besides that, having everybody write code in the same fashion improves the stability and readability of the code and makes it less error-prone and maintainable.

Adhering to the standards not only makes your code more readable and effective but also offers consistency and greater chance for a more productive collaboration. This is even more important when someone joins in the middle of development of a project, as the time and effort needed in reviewing the existing code is very limited.

But creating highly-maintainable, readable code requires considerable investment in time, effort, and other resources.

With the advent of version control (for example, Git or SVN), writing standard-compliant code is not really that hard. Using hooks, it is now easier to write standard-compliant code. Hooks are little scripts you can place in your $PROJECT_ROOT/.git/hooks directory to trigger actions at precisely timed events.

**Git Pre-Commit Hook **

This hook is invoked by git commit, and can be bypassed with the –no-verify option. It takes no parameter, and is invoked before obtaining the proposed commit log message and making a commit. Exiting with non-zero status from this script causes the git commit to abort. The default pre-commit hook, when enabled, catches the introduction of lines with trailing whitespaces and aborts the commit when such a line is found.

Note that the pre-commit hook is only one the several hooks that you could utilize (see http://githooks.com/), and you could use Bash, Python, or your preferred scripting language. For example, you could have a custom hook that will execute every time a developer pushes a commit in the target server.

**Coder plus Git Pre-Commit **

****Coder is a Drupal module that checks your Drupal code against coding standards and other best practices. Coder shipped with Code Sniffer, a set of coding standard for PEAR’s PHP_CodeSniffer based on the Drupal Coding Standard.

To be able to use the pre-commit hook you need to have PHP_CodeSniffer installed. Below are the installation steps:

  1. Install PHP Pear.
  2. Install PHP_CodeSniffer.
    $ pear install PHP_CodeSniffer
  3. Create a symlink of Drupal coding standard in PHP CodeSniffer directory.
    $ ln -sv /path/to/coder/coder_sniffer/Drupal $(pear config-get php_dir)/PHP/CodeSniffer/Standards/Drupal
  4. Download the pre-commit script here and put it in $GIT_DIR/hooks. Ensure that the file is executable.

If the commit contains any violations of the following type:

  1. PHP syntax error
  2. PHP coding standard
  3. JavaScript coding standard
  4. CSS coding standard

…it will exit before the commit gets indexed and will display the offending file(s). It will display the line of code and the filename that contain bad code. The developer must resolve the problem first in order to stage the commit.

If you’re really sure that it is OK to commit the changes without resolving the coding standard problem detected by the script, you can skip or bypass the validation by using –no-verify

Ex: $ git commit -am “#1452 Commit message | Bypassing validation process” –no-verify

In a nutshell, the idea behind the Drupal Pre-Commit hook script is to fetch the output of the ‘git status’ command, then get the list of added or modified files, then one by one check for syntax and coding standards’ validation.

One good thing also is that the script will warn you about the debug-related codes that might have gone unnoticed like dpm(), console.log(), var_export(), and so on. It’s an ongoing project and you might want to contribute also.

If you want to check your code without Git pre-commit script follow the guide here.

Finally, a quote from Idan Gazil:

“Part of being a good steward to a successful project is realizing that writing code for yourself is a Bad Idea. If thousands of people are using your code, then write your code for maximum clarity, not your personal preference of how to get clever within the spec.”

/Five