August 17, 2024
Coding Standards
1. Documentation: Developers need to document their code for better understanding and readability. For class, need to specify class description and author. For variable/attribute, need to specify its description and type of value it store. For method, need to write description, input parameter details, and method’s return value. Below is sample class:
Code
/**
* Description: This class is written to manipulate student details
* @author: author name
*/
Class StudentDetails {
/**
* Variable contains student name
*@var string
*/
Protected $student_name;
/**
* Description: get student details basis on student name
(student name parameter is optional), if student name doesn’t
pass then method will return all student records
* @author: author name
* @param: $name (string), contains student name (optional)
* @return: $student_record (json object)
*/
Public function getStudentDetails($name=’’) {
// get student details
return $student_record;
}
/**
* Description: This method save student details
* @author: author name
* @param: $name (string), contains student name
* @param: $class (string), contains student class
* @param: $enrollment_no (integer), contains student enrollment number
* @return: bool value (1/0)
*/
Public function setStudentDetails($name, $class, $enrollment_no) {
// code to save student details
return status(1/0); // bool value
}
}
2. Create common configuration files: All system related configuration need to put in a common configuration directory. For instance, database connection(s) details (hostname, username, password, database name), ftp connection(s) details, global IPs, application version etc. Further, these configurations will be referred in entire application/project.
Example: We can manage system configuration in common ‘config’ directory
Config (Directory)
• Database.php
• Ftp.php
• Config.php
• Constants.php
3. Create common database class: All database related code should keep in model directory and need to write model classes for database interactions.
Models (Directory)
• MasterModel.php (contains code to connect/close database)
• ASEModel.php (contains database related code/queries)
4. Code Reusability/ Remove duplicate code: Need to reduce/remove duplicate code. If duplicate code present in multiple files, so we need to remove duplicate code and put the code in a separate common class/method.
Example with duplicate code: In below mentioned example we have two classes ‘ASE’ (present in file ASE.php) and ‘UKSE’ (present in file UKSE.php) in both classes we have method called ‘parseData’ with duplicate code.
Code
File: ASE.php
/**
* Description: This class is written to manipulate data
* @author:
*/
Class ASE {
/**
* Description: Method gets raw data and return final processed data
* @author:
* @return: array (processed data)
*/
Public function getASEData(){
// get ase data
$final_data = $this->parseData($rawData);
return $final_data;
}
/**
* Description: This method is written to parse raw data.
* @author:
* @param: $raw_data (array), contains raw data
* @return: array (processed data)
*/
Public function parseData($raw_data){
// parse data
return $final_output;
}
}
File: UKSE.php
/**
* Description: This class is written to manipulate data
* @author:
*/
Class UKSE {
/**
* Description: Method gets raw data and return final processed data
* @author:
* @return: array (processed data)
*/
Public function getUKSEData(){
// get ukse data
$final_data = $this->parseData($raw_data);
return $final_data;
}
/**
* Description: This method is written to parse raw data.
* @author:
* @param: $raw_data (array), contains raw data
* @return: array (processed data)
*/
Public function parseData($raw_data){
// parse data
return $final_output;
}
}
Example to remove duplicate code: In below mentioned example we have three classes parseDetails (present in file parseDetails.php), ASE (present in filee ASE.php) and UKS (present in file UKS.php). In below example, a new abstract class created called ‘parseDetails’ which contains logic for parse raw data and we removed ‘parseData’ method from ‘ASE’ and ‘UKS’ classes.
Code
File: parseDetails.php
/**
* Description: This class is written to parse raw data
* @author:
*/
Abstract class parseDetails{
/**
* Description: This method is written to parse raw data.
* @author:
* @param: $data_object (array), contains raw data
* @return: array (processed data)
*/
Public abstract function data($data_object){
// parse data
return $final_output;
}
}
/**
* Description: This class is written to manipulate data
* @author:
*/
require_once ‘parseDetails.php’;
Class ASE {
/**
* Description: Method gets raw data and return final processed data for UKSE
* @author:
* @return: array (processed data)
*/
Public function getASEData(){
// get ase data
parseDetails::data($data);
}
}
/**
* Description: This class is written to manipulate data
* @author:
*/
require_once ‘parseDetails.php’;
Class UKSE {
/**
* Description: Method gets raw data and return final processed data for UKSE
* @author:
* @return: array (processed data)
*/
Public function getUKSEData(){
// get data
parseDetails::data($data);
}
}
5. Naming Conventions: Need to follow naming convention, so we can avoid ambiguity in code.
• If we are creating a class file, then class name and file name should be same. And class name should be in Pascal case.
Example
Filename: ASE.php
class ASE {}
• If current class file is model class then attach word ‘Model’ with class name.
Example
For ‘ASE’, file name should be ASEModel.php and class name ASEModel.
• Variable name should start with lowercase character and if multiple words present then words should be separated with underscore character.
Example
$sts;
$a_s_e;
• Method name should be camel case.
Example
Public function getASED(){}