Web Development Code Standards

Directories

The following four locations for storing Web files are required for each project/site:

/planfor pre-web development, such as documentation, graphics and HTML mockups
/devfor code development and debugging
/stagefor staging area; testing and QA
/migratea mirror of the staging; for migrating data from an existing site to a new DB
/live; available to end users

All non-live areas should be password-protected to allow only developers. Clients may have access to the /stage directory.

Naming Conventions

Files

file_name.php, file_name.htm

Files should be all lowercase, with a three-letter file extension. Words should be separated by an underscore ("_") character. The first word should be the section, followed by a sub-section (if necessary) and finally by the action. For example, a file that creates a new record for Events might be named events_add.php. Use only alpha-numeric characters, period and underscore only.

Because of Windows limitations, HTML files should have a .htm suffix, rather than the Mac and UNIX .html.

Included files should be preceded with "inc_" and have a three-letter extension. If they are PHP files, the extension should be .php. For example: inc_top.php.

DB Tables

TableName, PRE_TableName

MySQL database tables should be InterCapitalized, with the first letter of each word capitalized. If a category is necessary, use a short abbreviation, in all caps, proceeded by an underscore and a description of the table. Be careful to avoid using language keywords as table names. Use alpha-numeric characters only.

Table names should always be singular, describing one record of the table. Two examples are Employee and Company.

DB Field Names

FieldName, ID, TableID

MySQL field names should be InterCapitalized, with the first letter of each word capitalized. Be careful to avoid using language keywords as field names. Do not use the name of its table as the field name. Use alpha-numeric characters only.

Primary key fields should be called ID. Foreign key fields should start with the name of the table they relate to, followed by "ID". For example, if the One table is Company and the Many table is Employee, use CompanyID as the foreign key in each record in the Employee table.

Whenever possible, use the field names Name and Description if these fields exist. Then makes it easier to reuse SQL statements.

Variable Names

$MyVariable, $row["DatabaseVariable"]

PHP variable names always start with a dollar sign ("$") and should be InterCapitalized, with the first letter of each word capitalized. Use alpha-numeric characters only.

MySQL variable names follow the same convention, but without the dollar sign ("$").

In general, variable names should be as descriptive as possible, without being unnecessarily long. A variable that is used in very limited scope may have a short name, such as using $i in loops.

CSS Class/ID Names

ClassName

Wherever used (defining or specifying), CSS class and ID names should be InterCapitalized, with the first letter of each word capitalized. For example, <p class='TopLevel'> and div.ErrorMessage {}.

Commenting

Top

There should be a section of comments at the top of each page, above all other code or HTML elements (immediately after <?php), with the following information:

This information should be contained in an ASCII box created using the slash ("/") character and any others that will create a visual box. For example:

/////////////////
//
//  Comments...
//
/////////////////

or

//------------------
//
//  Comments...
//
//------------------

or

//=================//
//                 //
//  Comments...    //
//                 //
//=================//

etc.

The style of the box is left up to each programmer, but it should be consistent across all of a programmer's files.

Section comments

When there is a section of code that is related, or can be logically grouped, it should be labeled at the top with either a small comment box or line. The comments should describe the code below it, and start at the left edge of the screen, with no code on the same line. If a small ASCII box is used, it should be similar in style to the large box at the top. The size of the box should also represent the amount or importance of the code it describes. For example, a large, ornate box shouldn't be used with two lines of code.

For example:

// Comment here

lines of code...

or

//================
// Comment here...
//================

lines of code...

Inline comments

Inline comments describe a single line of code, and are usually placed at the end of the line. For example:

$Variable = "Value";  // Comment goes here

SQL

SQL statements are easier to read if they are broken into multiple lines. Each keyword should be in all-caps on its own line, followed by a new line, then a tab for the parameters. Each parameter should be on its own line, indented from the keyword. For example:

SELECT
   ID,
   FirstName,
   LastName
FROM
   TableName
WHERE
   ID > 5

Curly Braces

Curly braces should be on separate lines, directly under the statement they go with. Anything contained in the braces should be indented one tab in from the braces on separate lines. For example:

if ($i > 5)
{
   echo "\$i is greater than 5";
}
else
{
   echo "\$i is less than 5";
}

or

while ($i < 5)
{
   echo $i;
   $i++;
}

This applies to other similar code, such as defining functions and classes.

Spacing

There should be one space in the following scenarios:

HTML

HTML needs to look good for several reasons: debugging, redesigning, and because anyone can see our HTML source.

Example:

<table class='Dark'>
   <tr>
      <td>
         Table content
      </td>
   </tr>
</table>

CSS

Formatting Cascading Style Sheets correctly and consistently will make it easier to add, edit, and find things in a style sheet document.

Example:

p.Dark
{
   background-color: #030;
   color: #999;
}