Wednesday, July 21, 2010

Best Code Development Practices-III

The objective of this document is to provide common PHP software development guidelines.

1) CODE LAYOUT (REQUIRED)

a) Header: Every file created in a project must have a comment header. This must contain the following
/**
* File Name:

* File objective:

* File Description:

* Created by:

* Created on:

* Last Modified by:

* Last Modified on:

* Included Files: Files included in this file

* Included In: Files where this file is included

*/

b) PHP Code tags: Following the following format

This is correct
&
?>

These are incorrect
&
?>



<% %>

c) Indenting: For code indenting, use 4 spaces instead of tabs.

d) Linefeeds: Make sure that you PHP editor adds \n whenever you press enter to create a
new line. Most Windows based editors add \n\r by default.

e) Inline Comments: Write inline comments where code block objective is not clear. As a
thumb rule, write inline comments when any of the following is true



i) When variable name, class name or function name do not tell anything about their
purpose.

ii) When a decision is being taken. For example, If-else, switch-case, etc.

iii) When a regular expression is being used

iv) When modifying/replacing an existing code.

The format for inline comments should be as below

/*
* MODIFIED BY developer_name ON dd/mm/yy
* describe change here briefly yet clearly
*/
Also, keep the original code as is, however, commented

2) SQL CODE GUIDELINES (REQUIRED):

a) SELECT: Avoid using * in SELECT queries since this delays the results. Instead give names of fields.
For example, SELECT first_name, last_name, email FROM users; is correct,
but SELECT * FROM users; not;

b) JOINING CONVENTIONS: When multiple tables are involved,always create table aliases for clarity. Keep table aliases short and close to.
For example SELECT cmp.company, emp.first_name, emp.last_name_first FROM
tbl_company AS cmp, tbl_employee AS emp
WHERE cmp.company_id = emp.company_id AND cmp.company = '" . $company. "' ORDER BY emp.first_name ASC;
c)QUERY FORMAT OPTIONS:
Follow any of the following query formats for more readable query format.

i) $sql = SELECT first_name, last_name, email FROM users WHERE status=1 ORDER BY first_name ASC;



Notice that main SQL keywords come prior to anything else in any row.
ii) $sql = “SELECT first_name, last_name FROM users WHERE status=1”;

Click Software Outsourcing
iPhone Application Development