Inconsistencies [in your coding style] are jarring and require more time to read and comprehend. Consistency is such a valued quality that developers often abide by a coding standard even if they dislike the coding standard itself. —Chris Shiflett and Sean Coates
I consider following a coding standard a sure sign of a craftmanship. A craftsman knows his tools and languages well, and is consistent in his coding style. Ego and personal preference give way to consistency and best practices.
If you develop on your own, create a coding style and stick with it. There are many practical reasons to abide by code standards when developing on your own. It enforces good habits and helps avoid simple syntax bugs. It speeds up your development process by giving you structure and taking the guesswork out of naming and spacing tasks. And, possibly most importantly, it ensures your code is readable to your future self.
This is all fine and dandy when you’re the only one touching the code. When you share and collaborate with other developers, however, following a coding standard is not a choice—it is mandatory. If you develop for a platform, use the coding standards of the platform. Even if you don’t agree with the standard, you should follow it anyway so that your code is understandable and usable for other developers. Chances are the standards were put in place for very good reasons, both practical and philosophical.
To learn the coding standards for a project you work with, start by looking at other people’s code. Put the standards into practice in your own code and don’t be afraid to ask for a review from someone else. Check your code against similar structures in your software’s codebase, or popular modules, themes, and plugins for the software.
A big part of my job as Theme Wrangler at Automattic is reviewing themes. Lots of themes from lots of designers and developers. Everyone seems to have their own coding style, which makes it difficult to review code and find errors quickly. As a result I spend a lot of time cleaning up themes to match WordPress coding standards before I can even begin the actual work of reviewing and updating the theme.
For that reason code style consistency is especially important to me. It enforces best practices, produces consistent code, and speeds up development between collaborators.
- Use standard syntax.
- Remove extra whitespace and line endings.
- Use consistent spacing and indentation.
- Use human-readable labels and names.
Following are examples of specific coding standards that I follow in my everyday development.
- CakePHP coding standards
- WordPress coding standards
For a top-notch example of coding standards within a company, see the Fellowship Technologies Code Standards. That, friends, is a well-crafted coding standard that we can all learn from.