Development:Coding style: Difference between revisions
Line 24: | Line 24: | ||
===PHP tags=== | ===PHP tags=== | ||
Always use full php tags: <?php | Always use full php tags: '''<?php''' | ||
To avoid whitespace problems, DO NOT include the closing tag at the end: ?> | To avoid whitespace problems, DO NOT include the closing tag at the end: '''?>''' | ||
===Indentation=== | ===Indentation=== |
Revision as of 05:05, 24 November 2008
This is a new draft of the coding guidelines that I'm working on. The structure was based on the Zend guidelines and our old Development:Coding page.
Overview
Scope
This document describes coding standards and guidelines for developers working on or with Moodle.
Goals
Good coding standards are important in any development project, particularly when multiple developers are working on the same project. Having coding standards helps to ensure that the code is of high quality, has fewer bugs, and is easily maintained.
Abstract goals we strive for:
- extreme simplicity
- tool friendliness, such as use of method signatures, constants, and patterns that support IDE tools and auto-completion of method, class, and constant names.
When considering the goals above, each situation requires an examination of the circumstances and balancing of various trade-offs.
Note that much of the existing Moodle code may not follow all of these guidelines - we continue to upgrade this code when we see it.
File Formatting
PHP tags
Always use full php tags: <?php
To avoid whitespace problems, DO NOT include the closing tag at the end: ?>
Indentation
Use an indent of 4 spaces with no tab characters. Editors should be configured to treat tabs as spaces in order to prevent injection of new tab characters into the source code.
Maximum Line Length
Aim for 80 characters if it is convenient. However, you can go longer if it helps overall readability. The maximum line length is 120 characters.
Line Termination
Use standard Unix text format. Lines must end only with a linefeed (LF). Linefeeds are represented as ordinal 10, or hexadecimal 0x0A.
Do not use carriage returns (CR) like Macintosh computers (0x0D).
Do not use the carriage return/linefeed combination (CRLF) as Windows computers (0x0D, 0x0A).
Lines should not contain trailing spaces. In order to facilitate this convention, most editors can be configured to strip trailing spaces, such as upon a save operation.
Naming Conventions
Filenames
Filenames should :
- be whole english words
- be as short as possible
- use lowercase letters only
- end in .php, .html, .js, or .xml
Classes
Class names should always be lowercase english words, separated by underscores:
class some_custom_class {
function class_method() {
echo "foo";
}
}
Functions and Methods
Function names should be simple English lowercase words, and start with the name of the module to avoid conflicts between modules. Words should be separated by underscores.
Verbosity is encouraged: function names should be as illustrative as is practical to enhance understanding.
Note there is no space between the function name and the following (brackets).
function forum_set_display_mode($mode=0) {
global $USER, $CFG;
if ($mode) {
$USER->mode = $mode;
} else if (empty($USER->mode)) {
$USER->mode = $CFG->forum_displaymode;
}
}
Function Parameters
Parameters are always simple lowercase English words (sometimes more than one, like $initialvalue), and should always have sensible defaults if possible.
Use "null" as the default value instead of "false", for situations like this where a default value isn't needed.
public function foo($required, $optional = null)
However, if an optional parameter is boolean, and its logical default value should be true, or false, then using true or false is acceptable.