Coding guidelines

If you wish to participate in the development of XodaGallery, we need to follow some guidelines for the coding. This is because the code should feel consistent troughout the whole application, and therefore be more readable. Please note that this guidelines are formed for further development of XodaGallery, all files might not comply at this moment.

Files

Encoding

Save your files as UTF-8 encoded. 1)

End of line

End of line should be UNIX style. This is possible to set in the preferences of most editors. 2)

PHP

Wich editor to use is up to you, as long as the code follow theese guidelines.

Code layout

Braces

Use starting brace at the same line as the command and closing brace at seperate line.

// braces prefered this way
if ($life == '42') {
	// some code
}

Strings

Use single-quotes whenever you can.

// avoid as much as possible
$variable = "This is of some $variable2 to me.";
// prefered
$variable = 'This is of some ' . $variable2 . ' to me.';

Indention

Use tabs to indent the code. Set tabspaces to 4 in your editor. In first columns it's ok with one space, to make first level comments stand out.

// you can use one space here
 $days = array('monday','tuesday');
// inside loop use tab
 foreach ($days as $d) {
	// this is tabbed indented
	echo $d;
	// if you nest, use several tabs
	if ($d == 'monday') {
		echo 'What a day!';
	}
 }

Naming

No hungarian notation in normal variables...

// wrong:
$int_age = 32;
// wrong:
$iAge = 32;
// right:
$age = 32;

... BUT in globals like session-, form- and cookie-names:

// formdata uses "frm_" as hungarian notation
$input = $_POST['frm_input'];
// sessiondata uses "sess_" as hungarian notation
$user = $_SESSION['sess_user_id'];
// cookies uses "cook" as hungarian notation
$remember = $_COOKIE['cook_memory'];

This is to avoid trouble on installations where the server uses register_globals.

Space replacement

Use underscore, not camelcase.

// wrong:
$MyName = 'Mattias';
// right:
$my_name = 'Mattias';

Comments

Use tags and practice from PHP Documentors manual (JavaDoc-style tags). Many of the tags have ”@” in them (see below).

Fileheader comments

The beginning of a PHP-file should always start with a commentblock stating

  • the name of the package (XodaGallery or some sort of extension)
  • a link to license in use (GPL)
  • if you want you can add copyright to your files
  • if it's appropiate, explain what the files does.

Example

/**
 * @package XodaGallery
 * @license http://opensource.org/licenses/gpl-license.php GNU Public License 
 * @copyright Mattias Wirf 2008 <xodamattias@users.sourceforge.net>
 * Optional explanation of what the file does
 */

Functioncomments

For functions and classes comments, follow suggestions from PHPDoc. Always give input paramaters (arguments) and return-vale.

  • Short explanation of the function
  • Paramaters
  • Return-value
/**
 * Creates a session for user
 * @param string $username	Name given by user in loginform
 * @param string $password	Password given by user in loginform
 * @return bool			True if valid user, false if not.
 */
 function login($username, $password) {
	// some code
 }

Small comments

Simply use the forward slashes

// user the short one hand forward-slash way
 $variable = somefunction();

CSS

Make it compact

Use shorthand CSS when you can. Some examples:

#someid {
	border: 1px solid #ccc;
	color: #000;
	padding: 0 0 10px 3px;
	margin: 0 auto;
}

Divide with comments

Make your code easy to find in css-documents, keep it in one place and a comment above:

/************
 * Commentform
 ***********/
 #commentform {}

Indention

Indent with tabs. First column can indent by a space, to seperate from commentlines. Example:

/************
 * Commentform
 ***********/
 #commentform fieldset {
	border: 1px solid #ccc;
	background: #f9f9f9;
 }
1) This is not so with all files at the moment, I know. I will try and convert them.
2) This is probably also false on some files in current version. Will change.
 
developer/coding_guidelines.txt · Last modified: 2008/05/20 12:58 by mattias
 
Recent changes RSS feed Creative Commons License Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki