An Overview of Drupal Coding Standards
Drupal community has created a set of coding standards and guidelines for users of Drupal framework. The guidelines narrate the formatting and style to the Drupal users. The Drupal community strictly follows these coding standards. Hence, it is essential to understand the salient features of these coding standards. These coding standards are not dependent on any version. The Drupal community regularly updates these standards for the users. The developers need to follow the updated standards irrespective of their version. Some coding standards tell users how to style and format the code. The users can also learn about the code’s actual substance/content through these standards. The developers can create maintainable and comprehensible programs with the use of coding standards. It is also easier to spot mistakes on the codes maintaining coding-standard.
Understanding Coding Standards
1.Indentation & Whitespace
Drupal community prefers using tabs to spaces during programming and users need to follow the same rule. Users need to place two spaces during programming without any tab. It is also necessary to avoid extra blank lines throughout the files. There must be no space at the end of any line. The Drupal community forbids the use of any trailing whitespace.
2.Casting
The programmers always need to put a space between the variable and the type while using Drupal.
3.Operators
The binary operators are the operators present between two values. The Drupal users need to put one space before the operator and one space after the operator to maintain reliability. The operators capable of operating on one value are called unary operators. It is essential for the users to put one space between the variable/number and the unary operators.
4.Line Length & Wrapping
Drupal users need to use 80 characters within a line to increase its readability. Many lines contain lengthy function definitions/names, class definitions and variable declarations. The Drupal community allows these lines to exceed the limit of 80 characters. The lengthy codes sometimes become intelligible due to multiple breaking. In this situation, programmers can forego the limit of 80 characters to preserve the comprehensibility.
Drupal Community instructs the developers not to wrap the conditions into multiple lines. The community wants the programmers to separately prepare the conditions after splitting. The developers can use this feature to document underlying reasons for each condition.
5.Control Structures
The Control statement includes different types of control structures such as switch, else, for, if. The programmers need to put one space between opening parenthesis and control keyword. It is also essential to use curly braces to decrease the chance of logical errors. The use of a space along with curly braces can separate the function calls from control statements.
6.Function Calls & Declarations
Developers need to put no space between the function-name, first-parameter, and opening parenthesis. It is essential not to put space between last parameter, semicolon, and closing parenthesis. The programmers may put spaces between each parameter and commas. The developers need to put one space between anonymous functions and the parameters.
7.Arrays
Programmers need to use short array syntax during formatting and use a space to separate each element. It is essential to break each element of arrays containing more than 80 characters into separate lines.
8.Class Constructor Calls
It is essential to include parentheses if developers are calling class constructors without arguments. It let developers maintain consistency between constructors with or without the arguments.
9.String Concatenations
The Drupal community needs programmers to put a space between concatenated parts and dots. It improves the codes’ readability. The programmers need to use single quotes, often. However, they can use double quotes to concatenate the simple variables. It is essential to use a space on each side of the ‘concatenating assignment operator’.
10.Quotes
There are no strict rules for ‘Quotes’ within ‘Drupal Coding Standards’. The programmers need to maintain consistency within each module. They also need to respect the style of each developer. The Drupal community supports the use of single quote strings in most cases. The developers may utilize ‘double quotes’ with in-line variable or with ‘translated strings’.
11.Including Code
The developers need to place ‘require_once()’ during the unconditional inclusion of a ‘class file’. They need to place ‘include_once()’ during the conditional inclusion of a ‘class file’.
12.PHP Code Tags
The developers need to use <?php ?> to delimit the PHP Code. They cannot use it on the shorthand tags.
13.Semicolons
The programmers using Drupal need to use semicolons at the end of every line and code blocks.
14.Coding Standards’ Importance
- The ‘coding standards’ increase the codes’ comprehensibility. It is possible for the Drupal users to understand the components of each code due to the presence of single standard.
- The users spend less time worrying about the codes’ appearance and more time on the coding.
- The use of single ‘coding standard’ unit the Drupal coders belonging to different backgrounds or locations.
- It is possible to use the standards to create personalized API documentation sites. The Drupal’s API module can parse the data automatically in order to create a documentation site.
15.Method of Use
- The coding standards change frequently and it is essential to thoroughly read the standards before implementation.
- It is also necessary to review the code’s viability with the help of PHP CodeSniffer or Coder.
- It is essential to let the fellow members of Drupal community review a code. The team-work may inform the developer regarding the drawbacks of the code before implementation.
- The developers need to use full PHP syntax in order to write PHP files in Drupal.
- The programmers also need to learn about the proper application of Hooks and operators.