-
Notifications
You must be signed in to change notification settings - Fork 132
Code style
Trang edited this page Mar 1, 2020
·
4 revisions
It's good enough if you try to stay consistent with the style of the file you are editing. When in doubt, just go with your personal preference. In the end we prefer that you focus on the code than on the code style.
Below are some of the basic rules that we tried to follow.
- We use 4 spaces for indentation. No tabs.
- Lines should not exceed 85 characters.
- Brackets for
class
andfunction
are on a new line. All other brackets are on the same line as the statement. - It is not allowed to omit brackets for
if
's, and it is not allowed to use ternary operator. - We use underscores for function names in the controllers, camelCase in the models. CSS is a mess, but if you'll be creating new CSS classes, use hyphens.
- There's no actual rule regarding the use of single vs. double quotes. Just use the type of quote that makes the most sense to you.
Below is a piece of code that illustrates the main rules.
<?php
class MyController extends SomeController
{
public $uses = array('SomeModel');
/**
* Documentation.
*
* @param string $a Description of $a.
* @param string $b Description of $b.
*
* @return void
*/
public function controller_actions_have_underscores($a = null, $b = null)
{
$value = null;
if ($a) {
$value = $this->_some_private_controller_function();
} else if ($b) {
$value = $this->Model->modelFunctionsAreCamelCase();
} else {
$value = __("Doesn't matter.", true);
}
$this->set('value', $value);
}
/**
* Private functions start with an underscore.
*
* @return int
*/
private function _some_private_controller_function()
{
$array1 = array('a', 'b', 'c');
// If the array is too long, indent it like this.
$array2 = array(
'key1' => 'value1'
'key2' => array(
'subkey1' => 'subvalue1',
'subkey2' => 'subvalue2'
)
);
switch ($number) {
case '1':
// some code
break;
case '2':
// some code
break;
default:
// some code
break;
}
while ($number < $otherNumber) {
$number++;
}
for ($i; $i < $number; $i++) {
// some code
}
return 42;
}
}
?>
https://github.com/johnpapa/angular-styleguide/blob/master/a1/README.md
If you have any question, if something in this page was not clear enough for you, or if you have suggestions to improve it, please let us know: [email protected].