8.Code Igniter Coding Conventions

Introduction:
This code igniter coding conventions is based on my own style of coding though there are standard practices included herein. The point that I’m making is that this is solely applicable to all who at some point become a part of my small software development team and or students. Outside than that, your will be done, yet if you need a standard to follow, you may use this as you pleased.:)

A.    File naming
1.     General
-          All in lowercase and use underscore to separate filename which is 2 words or more.
-          Filename should be a noun.
2.     views file
-          It should be a singular noun with view suffix.
Example: resume_view.php
3.     controllers file
-          It should be a plural noun with no suffix
Example: resumes.php
4.     models file
-          It should be a singular noun with model suffix.
Example: resume_model.php

B.    Constant naming
-          Constant variable should be on uppercase
-          Constant value like TRUE,FALSE,NULL should be on uppercase

C.    Views
1.     On reading data arrays
-          Use foreach using $row variable to represent each data row.
Example:
      foreach($resumes as $row)
      {
        echo  $row[‘firstname’];
      }
2.     Use table tag sparingly, use div tag oftenly.
3.     Use internal CSS sparingly, use external CSS(on the <head> tag) to format data.
4.     Use internal javascript sparingly, use external javascript  and place it before the end of the </body> tag
Example:
........
<script src="<?php echo base_url() ?>js/jquery-1.4.3.min.js" type="text/javascript"></script>
</body>
5.     Use php include to display repeated data on different pages like header, menu and footer.
Example:
<?php include 'include/header.php'?>
........
<?php include 'include/menu.php'?>
........
<?php include 'include/footer.php'?>

D.    Controllers
On the starting tag of the php Controller, put code something like this:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/*
*Filename:resumes.php
*projectname:bizjobfinder.com
*Date created:November 18,2011
*Created by:Mario T. Silvano
*/
1.     Class naming
-          should the same name with the file name.
Example:filename-resumes.php, classname-resumes
2.     Function naming
-          should contain with at most 3 words.
-          1st word is verb and the rest should be a noun.
-          if 2 words, 1st letter is lowercase, and the 2nd letter of 2nd word is uppercase.
Example: function insert(), function getList(), function uploadPhoto(), function createAccountView()
-          private function/method should start with an underscore.
example:private function _getURL()
3.     Variable naming
-          should be a noun
-          all in lower case
-          if 2 words, 1st letter of the 1st is lowercase, and the 1st letter of the 2nd word is uppercase.
4.     Object Model instance
-          Should be the name of the model without suffix.
Example: classname model: resume_model objectname: resume
5.     Use $data array variable to store values from the database
Example:
function getList()
{
  $data['resumes']=$this->resume->get_list();           
}
6.     Controller function name should be identical with the model function name except that the latter is separated by an underscore if two words.
Example:
function insert()
{
//resume is an instance and insert() is a function found on a class resume_model
  $this->resume->insert();      
}
function getList()
{
//resume is an instance and get_list() is a function found on a class resume_model
  …. = $this->resume->get_list();       
}

7.     Use comment for the ending php body tag
<?php
class resumes{
//.....
}
/* End of file resumes.php
 * Location: ./application/controllers/resumes.php */

E.    Routes
-          Associative array name should start the name of the object instance of the model and separated with a dash to indicate function name.
Example:$route[‘resume-get-list’]=resumes/getList

F.    Models
On the starting tag of the php Controller, put something like this:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/*
*Filename:resume_model.php
*projectname:bizjobfinder.com
*Date created:November 18,2011
*Created by:Mario T. Silvano
*/
1.     Class naming
-          should the same name with the file name.
Example:filename:resume_model.php classname:resume_model
2.     Function naming
-          all in lower case
-          should contain with at most 3 words.
-          1st word is verb and the rest should be a noun.
-          if 2 words, it would be separated by an underscore
Example: function insert(), function get_list(), function upload_photo()
-          private function/method should start with an underscore.
Example: private function _check_password()
3.     Variable naming
-          should be a noun
-          all in lower case
-          if 2 words, it would be separated by an underscore
4.     Writing SQL statement
-          use the code igniter functions for sql statements
5.     Use comment for the ending php body tag
<?php
class resume_model{
//.....
}
/* End of file resume_model.php
 * Location: ./application/models/resume_model.php */

Please be guided accordingly and please feel free to suggest if along the way you encounter exception to the rule of our coding conventions.

2 comments: