Module Development

Concept: Coding Standards for Drupal 8, 9, and 10

Coding standards are vital in collaborative environments like Drupal. These guidelines ensure consistency, readability, and maintainability of the codebase. Drupal has its own coding standards that outline best practices and formatting guidelines, promote code quality, simplify code reviews, and enhance the project's overall health.

In this tutorial, we'll:

  • Explore the use of coding standards in Drupal.
  • Highlight basic practices and sources for detailed information.

By the end of this tutorial you should be able to explain why coding standards are important and where to find more info about Drupal's coding standards.

Goal

Get familiar with Drupal's coding standards and their use in Drupal contribution.

Prerequisites

  • None.

Why coding standards matter

Coding standards are not just about making code look pretty. They serve practical purposes in software development.

  • Coding standards ensure that all code looks and feels the same, making it easier for developers to understand and work with code they didn't write.
  • Well-formatted code is easier to read and understand, reducing the time needed to grasp what a piece of code does.
  • Consistent and readable code is easier to maintain and debug, as developers can quickly locate and understand the logic.
  • With standardized code, reviews can focus on the logic and implementation rather than formatting, leading to more productive discussions.
  • Following Drupal's guidelines helps to avoid common security pitfalls.

Drupal's coding standards encompass language-specific formatting, documentation practices, naming conventions, and file organization.

Getting started with Drupal coding standards

The definitive guide for Drupal's coding standards is available on Drupal.org. The community discusses and evolves these standards in the Coding Standards project.

Drupal employs PHP CodeSniffer (phpcs) with custom rules to lint code. For an introduction to using phpcs in your projects, see Using PHP CodeSniffer with Drupal.

Configure your IDE to warn you about coding standards

Most IDEs can be configured to warn you about coding standards violations in real-time and to automatically format code according to Drupal's standards. Learn how to configure PhpStorm, or Visual Studio Code, or search for guides on setting up your IDE of choice.

Recap

Drupal's coding standards are essential for the codebase's quality and consistency. Adhering to these guidelines contributes to a more maintainable, readable, and secure Drupal project.

Further your understanding

  • Read Drupal's coding standards on Drupal.org.
  • Practice writing Drupal-compliant code and use PHP CodeSniffer for verification.

Additional resources

Drupal Module Developer Guide