June 10, 2014

What is clean code? At last night's SFDUG I gave a brief introduction to two complexity metrics—cyclomatic complexity and npath complexity—and how we can use these metrics to gain insight into the readability and maintainability of one's code.

PHPMD is a tool that analyzes and warns about overly complex code. The metrics alone are useful, but having a tool to crunch the numbers automatically is extremely handy. In addition to PHPMD, I recommended using a plugin for one's editor and/or IDE, such as Syntastic for Vim.

One can refactor code in order to simplify it. Since Drupal 7's hook system lends itself to imperative programming, the extract method refactoring pattern is used in order to rip out conditional statements and flow control statements into atomic functions. By using atomic functions and the functional programming paradigm, one can create simple, composible, re-usable, testable, readable and maintainable code.

Keeping it Simple Slides

Here is some recommended reading on the topics discussed: