Gazelle/docs/CodingStandards.txt

299 lines
7.9 KiB
Plaintext
Raw Normal View History

2013-03-02 08:00:34 +00:00
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.
2013-07-04 08:00:56 +00:00
# Table of Contents
1. FILE FORMATTING
2. CODE STYLING
1. Code styling for PHP and JavaScript
2. Code styling for CSS
3. Code styling for SQL
3. NAMING CONVENTIONS
1. Function names
2. Variable names
3. Class names
4. SQL names
5. Miscellaneous names
4. COMMENTS
5. USER INTERFACE
6. EXAMPLES
1. PHP examples
2. CSS examples
3. SQL examples
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.
2013-03-02 08:00:34 +00:00
== TABLE OF CONTENTS ==
1. FILE FORMATTING
2. CODE STYLING
2.1 Code styling for PHP and JavaScript
2.2 Code styling for CSS
2.3 Code styling for SQL
3. NAMING CONVENTIONS
3.1 Function names
3.2 Variable names
3.3 Class names
3.4 SQL names
3.5 Miscellaneous names
4. COMMENTS
5. USER INTERFACE
6. EXAMPLES
6.1 PHP examples
6.2 CSS examples
6.3 SQL examples
2013-07-04 08:00:56 +00:00
# FILE FORMATTING
2013-03-02 08:00:34 +00:00
2013-05-27 08:00:58 +00:00
Tabs shall always be used for indentation.
2013-03-02 08:00:34 +00:00
Files shall be encoded in ASCII.
Files shall always use Unix-style line endings (LF). The program dos2unix
2013-07-24 08:00:46 +00:00
will take care of this for you, if you have files that use Windows-style
2013-03-02 08:00:34 +00:00
line endings (CR+LF) or old Mac-style line endings (CR).
File names for PHP, CSS, and JavaScript files shall be all lowercase and
use underscores instead of spaces.
2013-07-04 08:00:56 +00:00
# CODE STYLING
2013-03-02 08:00:34 +00:00
2013-07-04 08:00:56 +00:00
## Code styling for PHP and JavaScript
2013-03-02 08:00:34 +00:00
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
2013-12-12 08:01:01 +00:00
reader will note that this is K&R style with the exception of the opening
brace in function definitions.
2013-03-02 08:00:34 +00:00
2013-07-04 08:00:56 +00:00
There shall be a space between a control structure statement (e.g. `if`,
2013-07-24 08:00:46 +00:00
`elseif` in PHP, `for`) and the following parenthesis.
2013-03-02 08:00:34 +00:00
There shall be a space around conditional operators.
When using ternary operators, spaces shall be used around the operators.
2013-07-24 08:00:46 +00:00
For PHP conditional blocks, `elseif` is to be used instead of `else if`.
2013-03-02 08:00:34 +00:00
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 the
following lines.
2013-10-23 08:01:03 +00:00
When opening a PHP statement, `<?` shall be used instead of `<?php`. In
other words, use PHP's short tags.
2013-03-02 08:00:34 +00:00
Switch cases in index files shall not contain substantial code. The use
of include statements is acceptable.
2013-07-04 08:00:56 +00:00
When declaring JavaScript variables, `var` shall always be used.
2013-03-02 08:00:34 +00:00
2013-07-24 08:00:46 +00:00
When manually type casting, whitespace should not be used between the
cast operator and the variable.
2013-07-04 08:00:56 +00:00
## Code styling for CSS
2013-03-02 08:00:34 +00:00
2013-07-04 08:00:56 +00:00
`property: value;` pairs shall be separated by a space, and the value
2013-03-02 08:00:34 +00:00
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.
2013-07-04 08:00:56 +00:00
## Code styling for SQL
2013-03-02 08:00:34 +00:00
Long SQL queries shall be separated on multiple lines.
Short SQL queries may be on a single line.
2013-11-17 08:00:47 +00:00
All SQL keywords shall be uppercase.
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. No table aliases shall be used for single-table queries.
2013-03-02 08:00:34 +00:00
The primary SQL keywords should be at the same indentation level unless
part of a JOIN or other complex statement.
Use indents as appropriate to aid readability.
2013-07-04 08:00:56 +00:00
The SQL keywords `JOIN`, `RIGHT JOIN`, `LEFT JOIN` must be indented once from
the `SELECT` statement.
2013-03-02 08:00:34 +00:00
2013-11-17 08:00:47 +00:00
The SQL keyword `AND` must be indented once from the `WHERE` (and similar)
2013-05-27 08:00:58 +00:00
statements.
2013-07-04 08:00:56 +00:00
The "not equal to" operator `!=` must be used instead of the alternative
operator `<>`.
2013-05-27 08:00:58 +00:00
2013-03-02 08:00:34 +00:00
2013-07-04 08:00:56 +00:00
# NAMING CONVENTIONS
2013-03-02 08:00:34 +00:00
Function, variable, and class names shall always be descriptive.
2013-07-04 08:00:56 +00:00
## Function names
2013-03-02 08:00:34 +00:00
2013-07-04 08:00:56 +00:00
PHP function names shall be written in `lowercase_with_underscores`.
2013-03-02 08:00:34 +00:00
2013-07-04 08:00:56 +00:00
JavaScript function names shall be written in `camelCase` with a leading
2013-03-02 08:00:34 +00:00
lowercase letter.
2013-07-04 08:00:56 +00:00
## Variable names
2013-03-02 08:00:34 +00:00
2013-07-04 08:00:56 +00:00
PHP variable names shall be written in `CamelCase` with a leading
2013-03-02 08:00:34 +00:00
uppercase letter.
2013-07-04 08:00:56 +00:00
JavaScript global-scope variables shall be written in `camelCase` with a
2013-03-02 08:00:34 +00:00
leading lowercase letter.
JavaScript local-scope variables shall be written in
2013-07-04 08:00:56 +00:00
`lowercase_with_underscores`.
2013-03-02 08:00:34 +00:00
2013-07-04 08:00:56 +00:00
## Class names
2013-03-02 08:00:34 +00:00
2013-07-04 08:00:56 +00:00
PHP class names shall be written in `CamelCase` with a leading uppercase
2013-03-02 08:00:34 +00:00
letter.
2013-07-04 08:00:56 +00:00
PHP class constants shall be written in `CamelCase` with a leading
2013-03-02 08:00:34 +00:00
uppercase letter.
2013-07-04 08:00:56 +00:00
## SQL names
2013-03-02 08:00:34 +00:00
All SQL keywords shall be written in all UPPERCASE.
2013-07-04 08:00:56 +00:00
All SQL table names shall be written in `lowercase_with_underscores`.
2013-03-02 08:00:34 +00:00
2013-07-04 08:00:56 +00:00
All SQL column names shall be written in `CamelCase` with a leading
2013-03-02 08:00:34 +00:00
uppercase letter.
2013-07-04 08:00:56 +00:00
All automatically-incremented ID columns shall be named `ID`, while the
other columns for Identification numbers shall have names like `RequestID`, `TorrentID`,
2013-03-02 08:00:34 +00:00
etc.
2013-07-04 08:00:56 +00:00
## Miscellaneous names
2013-03-02 08:00:34 +00:00
2013-07-04 08:00:56 +00:00
PHP global constants shall be written in `ALL_CAPS`.
2013-03-02 08:00:34 +00:00
2013-07-04 08:00:56 +00:00
PHP constants `true`, `false`, and `null` shall be written in all lowercase.
2013-03-02 08:00:34 +00:00
2013-07-04 08:00:56 +00:00
# COMMENTS
2013-03-02 08:00:34 +00:00
2013-07-04 08:00:56 +00:00
Use C89-style `/* ... */` comments for multi-line comments.
2013-03-02 08:00:34 +00:00
2013-07-04 08:00:56 +00:00
Use C99-style `// ...` comments for single-line comments.
2013-03-02 08:00:34 +00:00
2013-07-04 08:00:56 +00:00
# USER INTERFACE
2013-03-02 08:00:34 +00:00
2013-06-23 08:00:54 +00:00
All button labels shall use sentence case.
2013-03-02 08:00:34 +00:00
All table headings shall use sentence case.
2013-07-04 08:00:56 +00:00
All text-based buttons shall use the `brackets` CSS class.
2013-06-23 08:00:54 +00:00
Use common sense for design-related code. Microsoft's UI design guidelines
explain when certain form input controls should be used over others to
provide a familiar and intuitive interface. Refer to the following links
for the most likely issues to encounter in web design:
2013-03-02 08:00:34 +00:00
2013-07-04 08:00:56 +00:00
* http://msdn.microsoft.com/en-us/library/windows/desktop/aa511452.aspx
* http://msdn.microsoft.com/en-us/library/windows/desktop/aa511453.aspx
* http://msdn.microsoft.com/en-us/library/windows/desktop/aa511488.aspx
* http://msdn.microsoft.com/en-us/library/windows/desktop/aa511494.aspx
2013-03-02 08:00:34 +00:00
2013-07-04 08:00:56 +00:00
# EXAMPLES
2013-03-02 08:00:34 +00:00
2013-07-04 08:00:56 +00:00
## PHP examples
2013-03-02 08:00:34 +00:00
2013-07-04 08:00:56 +00:00
if ($Foo >= 0) {
$SomeString = "this is a string $DiffString with more text";
} elseif ($Foo == 0) {
// other things happen
} else {
// more magic
}
2013-03-02 08:00:34 +00:00
2013-07-04 08:00:56 +00:00
function works_magic($Foo, $Bar) {
return $Foo;
}
2013-03-02 08:00:34 +00:00
2013-07-04 08:00:56 +00:00
echo ($Foo == true ? 'foo is true' : 'foo is false');
2013-03-02 08:00:34 +00:00
2013-07-04 08:00:56 +00:00
if ($Foo == true) {
// magic happens
}
2013-03-02 08:00:34 +00:00
2013-07-04 08:00:56 +00:00
/*
* This is a good, multi-line comment.
*
* Please comment your code!
*/
2013-03-02 08:00:34 +00:00
2013-07-04 08:00:56 +00:00
// This is a good, single-line comment.
2013-03-02 08:00:34 +00:00
2013-07-04 08:00:56 +00:00
## CSS examples
2013-03-02 08:00:34 +00:00
2013-07-04 08:00:56 +00:00
<a href="foobar.php" style="font-weight: bold;">link text</a>
2013-03-02 08:00:34 +00:00
2013-07-04 08:00:56 +00:00
.spellcheck {
margin: 10px 0;
font-size: 1.25em;
font-weight: bold;
}
2013-03-02 08:00:34 +00:00
2013-07-04 08:00:56 +00:00
.linkbox .brackets:before,
.linkbox .brackets:after,
.top10_quantity_links .brackets:before,
.top10_quantity_links .brackets:after {
color: #757575;
}
2013-03-02 08:00:34 +00:00
2013-07-04 08:00:56 +00:00
# SQL examples
2013-03-02 08:00:34 +00:00
2013-07-04 08:00:56 +00:00
SELECT
r.ID, e.EditionID, r.Title, r.Year, r.CatalogueNumber,
l.Name AS Label, r.LabelID, r.Image, r.MusicBrainzID
FROM releases AS r
JOIN editions AS e ON e.ReleaseID = r.ID
LEFT JOIN labels AS l ON l.ID = r.LabelID
WHERE r.ID IN ($ReleaseIDsSQL)
ORDER BY e.EditionID, r.Year ASC
2013-03-02 08:00:34 +00:00
2013-07-04 08:00:56 +00:00
SELECT SQL_CALC_FOUND_ROWS c.ID, c.Subject, cu.Unread, cu.Sticky,
cu.ForwardedTo, cu2.UserID, cu.ReceivedDate AS Date
FROM pm_conversations AS c
LEFT JOIN pm_conversations_users AS cu ON cu.ConvID = c.ID
AND cu.UserID = '1'
LEFT JOIN pm_conversations_users AS cu2 ON cu2.ConvID = c.ID
AND cu2.UserID != '1'
AND cu2.ForwardedTo = 0
LEFT JOIN users_main AS um ON um.ID = cu2.UserID
WHERE um.Username LIKE 'test'
AND cu.InInbox = '1'
GROUP BY c.ID
ORDER BY cu.Sticky, Date DESC
LIMIT 25
2013-03-02 08:00:34 +00:00
2013-07-04 08:00:56 +00:00
SELECT RequestID AS ID, UserID FROM bookmarks_requests