Skip to content
Trang edited this page Mar 1, 2020 · 4 revisions

We don't have a strict code style

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.

Just for reference

Below are some of the basic rules that we tried to follow.

PHP

  1. We use 4 spaces for indentation. No tabs.
  2. Lines should not exceed 85 characters.
  3. Brackets for class and function are on a new line. All other brackets are on the same line as the statement.
  4. It is not allowed to omit brackets for if's, and it is not allowed to use ternary operator.
  5. 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.
  6. 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;
    }
}
?>

AngularJS

https://github.com/johnpapa/angular-styleguide/blob/master/a1/README.md