User:Prodego/URS/Conventions

These conventions are basically the MediaWiki conventions, with a few modifications.

Basic structure
Lines should be indented with a single tab character per indenting level (NOT SPACES).

All text files should be checked in to Subversion with svn:eol-style set to "native". This is necessary to prevent corruption by certain Windows-based text editors.

All text files are encoded with UTF-8. Be sure that your editor supports this.

Do not use MS Notepad to edit files. Notepad inserts unicode byte order marks (BOM) which stop PHP files from working.

Indenting and alignment
MediaWiki's indenting style is similar to the so-called "One True Brace Style". Braces are placed on the same line as the start of the function, conditional, loop, etc.

Multi-line statements are written with the second and subsequent lines being indented by one extra level:

Use indenting and line breaks to clarify the logical structure of your code. Expressions which nest multiple levels of parentheses or similar structures may begin a new indenting level with each nesting level:

Mid-line vertical alignment should be achieved with spaces. For instance this:

Is achieved as follows with spaces rendered as dots:

$namespaceNames·=·array(  →    R_USER············=>·1,   →    R_ADMIN·········.·=>·2,   →    R_DEV·············=>·3,

Line continuation
Lines should be broken at between 80 and 100 columns. There are some rare exceptions to this. Functions which take lots of parameters are not exceptions.

The operator separating the two lines may be placed on either the following line or the preceding line. An operator placed on the following line is more visible and so is more often used when the author wants to draw attention to it:

An operator placed on the preceding line is less visible, and is used for more common types of continuation such as concatenation and comma:

When continuing "if" statements, a switch to Allman-style braces makes the separation between the condition and the body clear:

Opinions differ on the amount of indentation that should be used for the conditional part. Using an amount of indentation different to that used by the body makes it more clear that the conditional part is not the body, but this is not universally observed.

Continuation of conditionals and very long expressions tend to be ugly whichever way you do them. So it's sometimes best to break them up by means of temporary variables.

Assignment expressions
Using assignment as an expression is surprising to the reader and looks like an error. Do not write code like this:

Space is cheap, and you're a fast typist, so please...

Ternary operator
The ternary operator can be used profitably if the expressions are very short and obvious:

But if you're considering a multi-line expression with a ternary operator, please consider using an "if" block instead. Remember, disk space is cheap, code readability is everything, "if" is English and ?: is not.

Classes

 * In general, member variables are named mXxx to distinguish them, which helps spot missing instances of $this, which will cause odd breakage due to PHP's silent initialisation of the variable
 * We prefix the names of global variables with wg in order to make it easier to distinguish them, which thus makes it easier to spot missing global declarations

Naming conventions
There is a preference for lowerCamelCase when naming functions or variables. For example:

There are also some prefixes used in different places:

Functions

 * uf (urs functions) - Top-level functions, e.g.

Variables

 * wg - global variables, e.g. ,
 * m - object member variables:

Bot Functions

 * bf - bot functions : functions that are used to make !commands do something

HTTP and session stuff

 * ws - Session variables, e.g.
 * wc - Cookie variables, e.g.
 * wp - Post variables (submitted via form fields), e.g.

Database

 * Column names are given a prefix derived from the table name: the name itself if it's short, or an abbreviation:
 * page -> page_id, page_namespace, page_title...
 * categorylinks -> cl_from, cl_namespace...

Inline documentation

 * The PHPDoc documentation style is used. For example: giving a description of a function or method, the parameters it takes (using ), and what the function returns (using  ), or the   or   tags.
 * General format for parameters is such:

Messages

 * HTML class and ID names should be prefixed with "urs-". It seems most common to hyphenate them after that, like "urs-some-new-class" instead of "urs-somenewclass" or "urs-some_new_class", but there doesn't appear to be a clear convention at present.