This document contains the coding standards for Gazelle. This document is a work-in-progress and is subject to change. NOTE: The standards defined in this document will likely differ from what is actually seen in the Gazelle code. This document is the first step in properly enforcing coding standards throughout the project. == TABLE OF CONTENTS == 1. LINTING 2. FILE FORMATTING 3. CODE STYLING 3.1 Code styling for PHP and JavaScript 3.2 Code styling for CSS 3.3 Code styling for SQL 4. NAMING CONVENTIONS 4.1 Function names 4.2 Variable names 4.3 Class names 4.4 SQL names 4.5 Miscellaneous names 5. COMMENTS 6. USER INTERFACE 7. EXAMPLES 7.1 PHP examples 7.2 CSS examples 7.3 SQL examples # LINTING To help ensure adherence to this guide, we have setup linting for PHP and SCSS code. They can be run through make: make lint-php make lint-css These steps are also run on committing. # FILE FORMATTING Spaces shall always be used for indentation. 4-space indents are used. Files shall be encoded in UTF-8. Files shall always use Unix-style line endings (LF). The program dos2unix will take care of this for you, if you have files that use Windows-style line endings (CR+LF) or old Mac-style line endings (CR). Files must end with a new-line character. Use a proper editor that performs this automatically. File names for PHP, CSS, and JavaScript files may not contain spaces. # CODE STYLING ## Code styling for PHP and JavaScript All statement blocks, including functions, shall have the opening brace at the end of the same line with a space before the brace. The astute reader will note that this is K&R style with the exception of the opening brace in function definitions. There shall be a space between a control structure statement (e.g. `if`, `elseif` in PHP, `for`) and the following parenthesis. PHP files shall not end with a trailing `?>`. There shall be a space around conditional operators. When using ternary operators, spaces shall be used around the operators. For PHP conditional blocks, `elseif` is to be used instead of `else if`. In loops and conditional blocks, there shall be braces even if there is only one statement. In loops and conditional blocks, the statement(s) shall be placed on separate lines. When opening a PHP statement, ` there shall be spaces at either end of the block (so that it is easy to grab the contents in vim with e.g. `yE`). When declaring an array, the short syntax `[]` shall be used, instead of the longer `array()`. In general, any syntax as of PHP 7.3 is permitted, and probably recommended, in the interest of brevity and clarity. Trailing commas in lists are allowed, and recommended. Use indents as appropriate to aid readability. When functions with many parameters are laid out over multiple lines, the subsequent lines are indented 4 characters (and not to the opening parenthesis above). $result = someFunction( anotherFunc(1, 2, 3), yetAnotherFunc(4, 5 6), ); Switch cases in index files shall not contain substantial code. The use of include statements is acceptable. When declaring JavaScript variables, `var` shall always be used. When manually type casting, whitespace should not be used between the cast operator and the variable. ## Code styling for CSS `property: value;` pairs shall be separated by a space, and the value shall be followed by a semi-colon. Multiple, related CSS selectors with the same declarations shall appear on multiple lines to improve readability. The opening brace shall be on the same line as the last related selector with a space between the selector and the brace. ## Code styling for SQL Long SQL queries shall be separated on multiple lines. Short SQL queries may be on a single line. All SQL keywords shall be uppercase. `count()`, `sum()`, `now()` are not keywords, they are functions. All queries that have external inputs shall use placeholders. Queries with arbitrary numbers of external inputs should use placeholders(). Aliases shall be used to refer to any columns whenever multiple tables are joined in an SQL query. They shall be using the `tbl_name AS alias_name` syntax. Table aliases must be used for single-table queries (because one day a second table will be added to the query). Table aliases can be constructed from the first letter of each word in the name (`users_main` becomes `um`). The primary SQL keywords should be at the same indentation level. (`SELECT`, `FROM`, 'INNER JOIN`, `WHERE`, `GROUP BY`, `HAVING`, `ORDER BY`). The SQL keywords `INNER JOIN`, `RIGHT JOIN`, `LEFT JOIN` are at the same level as the `SELECT` statement. The `JOIN` abbreviation for `INNER JOIN` is not accepted. `NATURAL JOIN` is forbidden. Join clauses must be parenthesized: `a INNER JOIN b IN (a.foo = b.kek)` When the column names match `USING` is preferred `a INNER JOIN b USING (kek)`. The SQL keyword `AND` must be indented once from the `WHERE` (and similar) statements. `UPDATE` that update multiple columns have one column per line. The `SET` clause is kept on the same line as the `UPDATE` statement, except in the case of a multi-table update against a derived table, where it sits on a line by itself. The "not equal to" operator `!=` must be used instead of the alternative operator `<>`. ## Naming conventions for new tables (Legacy tables are exempt). Names are always singular. The primary key is named `