Developer Area/Coding guidelines: Difference between revisions
From Mahara Wiki
< Developer Area
m (→switch/case statements: updating syntax tags) |
(syntax highlight) |
||
(3 intermediate revisions by the same user not shown) | |||
Line 74: | Line 74: | ||
Good: | Good: | ||
<syntaxhighlight lang="php"> | |||
$found = false; | |||
$ponies = 0; | |||
$servername = $overkill; | |||
// Only in relation to controlling for/while loops | |||
for ($i = 10; $i > 5; $i--) { | |||
echo $i; | |||
} | |||
// Aligning = signs | |||
$mine = 0; | |||
$yours = 0; | |||
$theirs = 0; | |||
// Only if the number of elements to initialise is small. | |||
$array = array(1 => 'hello'); | |||
// If large number of elements to initialise: | |||
$array = array( | |||
1 => 'hello', | |||
2 => 'goodbye', | |||
10 => 'erm' | |||
); | |||
</syntaxhighlight> | |||
Bad: | Bad: | ||
<syntaxhighlight lang="php"> | |||
// Negative name | |||
$notfound = true; | |||
// Underscores | |||
$my_variable = ''; | |||
// Hungarian | |||
$count_i = 6; | |||
// Uppercase letters | |||
$myFish = $yourFish; | |||
$array = array( | |||
1 => 'hello', | |||
2 => 'goodbye', | |||
10 => 'erm' | |||
); | |||
</syntaxhighlight> | |||
==Binary Operators== | ==Binary Operators== | ||
Always have a space on each side: | Always have a space on each side: | ||
<syntaxhighlight lang="php"> | |||
echo 1 + 2; | |||
if (1 == 2) | |||
echo 'hello' . ' world'; | |||
</syntaxhighlight> | |||
< | <syntaxhighlight lang="text" inline>||</syntaxhighlight> and <syntaxhighlight lang="text" inline>&&</syntaxhighlight> will always be used for <syntaxhighlight lang="text" inline>or</syntaxhighlight> and <syntaxhighlight lang="text" inline>and</syntaxhighlight> respectively. Resolve priority collisions with extra parentheses as required. | ||
==Classes, Functions and Methods== | ==Classes, Functions and Methods== | ||
Line 95: | Line 134: | ||
Classes should start with upper case character, then each word capitalized. | Classes should start with upper case character, then each word capitalized. | ||
< | <syntaxhighlight lang="php"> | ||
class ExampleWhizbang { | class ExampleWhizbang { | ||
// ... | // ... | ||
} | } | ||
</ | </syntaxhighlight> | ||
Leave double empty line between classes blocks in the code. | Leave double empty line between classes blocks in the code. | ||
Functions should not use camel case, and instead use underscores as word | Functions should not use camel case, and instead use underscores as word separators | ||
< | <syntaxhighlight lang="php"> | ||
function do_something(){ | function do_something(){ | ||
// ... | // ... | ||
} | } | ||
</ | </syntaxhighlight> | ||
NOT | NOT | ||
< | <syntaxhighlight lang="php"> | ||
function doSomething() { | function doSomething() { | ||
// ... | // ... | ||
} | } | ||
</ | </syntaxhighlight> | ||
Function name must start with a letter (no _ character before function name, unless you declare magic method where double underscore is used). | Function name must start with a letter (no _ character before function name, unless you declare magic method where double underscore is used). | ||
Line 128: | Line 167: | ||
Good: | Good: | ||
< | <syntaxhighlight lang="php"> | ||
$a = new stdClass(); | $a = new stdClass(); | ||
$b = new RssBlock(); | $b = new RssBlock(); | ||
</ | </syntaxhighlight> | ||
Bad: | Bad: | ||
< | <syntaxhighlight lang="php"> | ||
// stdClass written incorrectly | // stdClass written incorrectly | ||
$a = new StdClass(); | $a = new StdClass(); | ||
Line 144: | Line 183: | ||
// lowercase user-created class | // lowercase user-created class | ||
$c = new fancyblock(); | $c = new fancyblock(); | ||
</ | </syntaxhighlight> | ||
'''Declaration:''' | '''Declaration:''' | ||
<syntaxhighlight lang="php"> | |||
/** | |||
* PHPDoc comment describing the function | |||
* | |||
* Note how the default value has no spaces | |||
* | |||
* @param $var type What it is for. | |||
* @param $b Class Example var that is of a specific class. | |||
*/ | |||
function foo($a, Class $b, $c='') { | |||
// source | |||
return $value; | |||
} | |||
class FooBar extends Bar { | |||
/** | |||
* PHPDoc for the field | |||
* | |||
* For this project, do not use doThis. | |||
*/ | |||
public function do_this($c, $d) { | |||
// source | |||
} | |||
} | |||
</syntaxhighlight> | |||
'''Calling:''' | '''Calling:''' | ||
<syntaxhighlight lang="php"> | |||
< | $result = foo($bar, $baz); | ||
$mine = $object->method($a, $b); | |||
</syntaxhighlight> | |||
=Javascript= | =Javascript= | ||
All files must adhere to the [[BasicJSFileTemplates]]. | All files must adhere to the [[BasicJSFileTemplates]]. | ||
Javascript files should roughly follow [http://javascript.crockford.com/code.html Crockford's code conventions]. | Javascript files should roughly follow [http://javascript.crockford.com/code.html Crockford's code conventions]. |
Latest revision as of 15:07, 25 February 2022
This page documents the coding conventions used by Mahara. Everything that ends up in the git repository (other than third-party code) should follow these standards.
You can also have a look at the Reviewer's manual to see what reviewers will look at when looking at code submissions.
Separate pages with more information:
- PHP Files
- JavaScript Files (or snippets)
- TPL Files (may change in future if move away from smarty, ie to twig)
Accessibility
Mahara should be accessible to anybody. We are aiming to follow the WCAG 2.1 AA principles. There are a areas that we identified that require changes to achieve that aim. New and changed code should always be written with accessibility principles and guidelines in mind rather than tagged on 'when there is time'. Writing code accessible from the start makes it better code and requires less refactoring later on.
The style guide, accessible via 'yourmaharadomain/theme/styleguide.php', gives useful basic information for a lot of style elements.
Since the field of accessibility is changing, certain elements may require updates over time. You can find more information on our 'Accessibility' page.
File Headers
All files must adhere to the BasicPHPFileTemplates. There are slightly different templates depending on whether the script is to be included or whether it is to be directly hit.
Closing php tags ?> should be omitted.
Whitespace
Required:
- Indentation is using four (4) spaces, not tabs.
- Line endings should be "Unix-style" (\n), not "Windows-style" (\r\n).
- Avoid extra whitespace at the end of modified lines.
- Avoid editing whitespaces unrelated to your specific code change, when submitting a patch. It makes reviewing the patch more difficult and add unnecessary noise to the change history.
Recommended:
- There is no requirement that the scripts are wrapped at 80 columns, however developers are asked to wrap long lines sensibly.
Language Constructs
switch/case statements
"case" should use multiline formatting:
switch ($var) {
case 1:
a = 25;
b = 100;
break;
default:
a = 0;
b = 0;
}
NOT
case 1: a = 25; b = 100; break;
Variables and Arrays
Variables should be named with no underscores, and no capital letters. This policy may become more lenient in future if variable names are discovered that are not easily readable without underscores.
Do not name variables as negatives where possible - always use positive names and logical inversion (!) where required.
Hungarian notation is forbidden.
Aligning = signs for variable assignment is up to the developer, although they should use common sense in such decisions.
Hardcoded binary data is not allowed for security reasons.
Hardcoded serialised data is not allowed for security reasons.
Good:
$found = false;
$ponies = 0;
$servername = $overkill;
// Only in relation to controlling for/while loops
for ($i = 10; $i > 5; $i--) {
echo $i;
}
// Aligning = signs
$mine = 0;
$yours = 0;
$theirs = 0;
// Only if the number of elements to initialise is small.
$array = array(1 => 'hello');
// If large number of elements to initialise:
$array = array(
1 => 'hello',
2 => 'goodbye',
10 => 'erm'
);
Bad:
// Negative name
$notfound = true;
// Underscores
$my_variable = '';
// Hungarian
$count_i = 6;
// Uppercase letters
$myFish = $yourFish;
$array = array(
1 => 'hello',
2 => 'goodbye',
10 => 'erm'
);
Binary Operators
Always have a space on each side:
echo 1 + 2;
if (1 == 2)
echo 'hello' . ' world';
||
and &&
will always be used for or
and and
respectively. Resolve priority collisions with extra parentheses as required.
Classes, Functions and Methods
Nameing
Classes should start with upper case character, then each word capitalized.
class ExampleWhizbang {
// ...
}
Leave double empty line between classes blocks in the code.
Functions should not use camel case, and instead use underscores as word separators
function do_something(){
// ...
}
NOT
function doSomething() {
// ...
}
Function name must start with a letter (no _ character before function name, unless you declare magic method where double underscore is used).
Leave single empty line between two function blocks and between function block and code if function is used in non-library file.
The built-in PHP class "stdClass"
should be written with a lowercase s, uppercase C, because that is its actual name. Other capitalizations, such as "StdClass" will not cause an error because invocations of classnames and functions are case-insensitive in PHP. However, it is good style to use the same capitalization as the declaration of the class/function.
Constructors without arguments should be called with empty parentheses after them.
Good:
$a = new stdClass();
$b = new RssBlock();
Bad:
// stdClass written incorrectly
$a = new StdClass();
// no parentheses after constructor
$b = new RssBlock;
// lowercase user-created class
$c = new fancyblock();
Declaration:
/**
* PHPDoc comment describing the function
*
* Note how the default value has no spaces
*
* @param $var type What it is for.
* @param $b Class Example var that is of a specific class.
*/
function foo($a, Class $b, $c='') {
// source
return $value;
}
class FooBar extends Bar {
/**
* PHPDoc for the field
*
* For this project, do not use doThis.
*/
public function do_this($c, $d) {
// source
}
}
Calling:
$result = foo($bar, $baz);
$mine = $object->method($a, $b);
Javascript
All files must adhere to the BasicJSFileTemplates. Javascript files should roughly follow Crockford's code conventions.