Coding Standard - kulbakin/anahita GitHub Wiki
Code Indent
Use 4 spaces for indenting. DO NOT use tabs.
PHP
As basic coding standard use PSR-1 and PSR-2. For the areas which are not covered by PSR-1 and PSR-2, refer to Zend Coding Standard.
JavaScript
Though there is an inclination to keep coding styles for PHP and JavaScript similar, they are NOT the same. JavaScript is a language with unique specifics (e.g. rather a bad quality of optional semicolon), thus requiring a good coding practise of its own resulting in coding style. As basic coding standard use Code Conventions suggested by Douglas Crockford
CSS and LESS
As a basic rules use CSS Coding Standards. LESS standards are the same, just exterpolated on for new syntax introduced by it, such as nested definitions. Samples provided for LESS are good guidence as well provided 4 spaces are used for indent.
Special Mention
Though all the following can be derived from referenced coding standard documentation, the listed cases deserve a special mention due to anahita codebase being very inconsistent toward them.
-
There must be NO trailing white spaces.
-
Concatenation operator ('.') must have NO spaces around it as opposed to other operators ('+', '&&' etc.)
// WRONG
$x = $y . $z;
// RIGHT
$x = $y.$z;
- Control structures (if, for etc.) must always have operator parentheses.
// WRONG
if ($condition) $x = $y;
// WRONG
if ($condition)
$x = $y;
// RIGHT
if ($condition) {
$x = $y;
}
- Control structures (if, foreach etc.) must have space before opening perenthesis of condition statement, opening curl bracket must be placed on the same line with closing perenthesis of condition statement and have single space between them.
// WRONG
if($condition){
// code
}
// RIGHT
if ($condition) {
// code
}
- There must be NO spaces AFTER OPENING parenthesis and BEFORE CLOSING parenthesis, applies to control structures, function calls etc.
// WRONG
$x = str_replace( 'a', 'b', $str );
// RIGHT
$x = str_replace('a', 'b', $str);
- There must be a space after comma.
// WRONG
$x = str_replace('a','b',$str);
// RIGHT
$x = str_replace('a', 'b', $str);
- When using alternative syntax for control structures, do not put space before colon
<!-- WRONG -->
<?php if ($condition) : ?>
<!-- code -->
<?php endif ?>
<!-- RIGHT -->
<?php if ($condition): ?>
<!-- code -->
<?php endif ?>
- Boolean NOT operator ! must have single space before and after
// WRONG
$x = !$y;
// WRONG
if (!$condition) {
// code
}
// RIGHT
$x = ! $y;
// RIGHT
if ( ! $condition) {
// code
}
- Comparison operators, especially '==', when constant is involved SHOULD have it on left side
// NOT RECOMMENDED
if ($x == 1) {
// code
}
// RECOMMENDED
if (1 == $x) {
// code
}