Here are some of the coding standards tht e need to follow .
Names
Make Names Fit
Names are the heart of programming. In the past people believed knowing someone’s true name gave them magical power over that person. If you can think up the true name for something, you give yourself and the people coming after power over the code. Don’t laugh!
A name is the result of a long deep thought process about the ecology it lives in. Only a programmer who understands the system as a whole can create a name that “fits” with the system. If the name is appropriate everything fits together naturally, relationships are clear, meaning is derivable, and reasoning from common human expectations works as expected.
If you find all your names could be Thing and Dolt then you should probably revisit your design.
Class Names
- Name the class after what it is. If you can’t think of what it is that is a clue you have not thought through the design well enough.
- Compound names of over three words are a clue your design may be confusing various entities in your system. Revisit your design. Try a CRC card session to see if your objects have more responsibilities than they should.
- Avoid the temptation of bringing the name of the class a class derives from into the derived class’s name. A class should stand on its own. It doesn’t matter what it derives from.
- Suffixes are sometimes helpful. For example, if your system uses agents then naming something Download Agent conveys real information.
Method and Function Names
- Usually every method and function performs an action, so the name should make clear what it does: CheckForErrors() instead of ErrorCheck(), DumpDataToFile() instead of DataFile(). This will also make functions and data objects more distinguishable.
- Suffixes are sometimes useful:
- Max – to mean the maximum value something can have.
- Cnt – the current count of a running count variable.
- Key – key value.
For example: RetryMax to mean the maximum number of retries, RetryCnt to mean the current retry count.
- Prefixes are sometimes useful:
- Is – to ask a question about something. Whenever someone sees Is they will know it’s a question.
- Get – get a value.
- Set – set a value.
For example: IsHitRetryLimit.
No All Upper Case Abbreviations
- When confronted with a situation where you could use an all upper case abbreviation instead use an initial upper case letter followed by all lower case letters. No matter what.Do use: GetHtmlStatistic.
Do not use: GetHTMLStatistic.
Justification
- People seem to have very different intuitions when making names containing abbreviations. It’s best to settle on one strategy so the names are absolutely predictable.
Take for example NetworkABCKey. Notice how the C from ABC and K from key are confused. Some people don’t mind this and others just hate it so you’ll find different policies in different code so you never know what to call something.
Example
class FluidOz // NOT FluidOZ
class GetHtmlStatistic // NOT GetHTMLStatistic
Class Names
- Use upper case letters as word separators, lower case for the rest of a word
- First character in a name is upper case
- No underbars (‘_’)
Justification
- Of all the different naming strategies many people found this one the best compromise.
Example
class NameOneTwo
class Name
Class Library Names
- Now that name spaces are becoming more widely implemented, name spaces should be used to prevent class name conflicts among libraries from different vendors and groups.
- When not using name spaces, it’s common to prevent class name clashes by prefixing class names with a unique string. Two characters is sufficient, but a longer length is fine.
Example
John Johnson’s complete data structure library could use JJ as a prefix, so classes would be:
class JjLinkList
{
}
Method Names
- Use the same rule as for class names.
Justification
- Of all the different naming strategies many people found this one the best compromise.
Example
class NameOneTwo
{
function DoIt() {};
function HandleError() {};
}
Class Attribute Names
- Class member attribute names should be prepended with the character ‘m’.
- After the ‘m’ use the same rules as for class names.
- ‘m’ always precedes other name modifiers like ‘r’ for reference.
Justification
- Prepending ‘m’ prevents any conflict with method names. Often your methods and attribute names will be similar, especially for accessors.
Example
class NameOneTwo
{
function VarAbc() {};
function ErrorNumber() {};
var $mVarAbc;
var $mErrorNumber;
var $mrName;
}
Method Argument Names
- The first character should be lower case.
- All word beginnings after the first letter should be upper case as with class names.
Justification
- You can always tell which variables are passed in variables.
Example
class NameOneTwo
{
function StartYourEngines(&$someEngine, &$anotherEngine) {
$this->mSomeEngine = $someEngine;
$this->mAnotherEngine = $anotherEngine;
}
var $mSomeEngine;
var $mAnotherEngine;
}
Variable Names
- use all lower case letters
- use ‘_’ as the word separator.
Justification
- With this approach the scope of the variable is clear in the code.
- Now all variables look different and are identifiable in the code.
Example
function HandleError($errorNumber)
{
$error = new OsError;
$time_of_error = $error->GetTimeOfError();
$error_processor = $error->GetErrorProcessor();
}
Array Element
Array element names follow the same rules as a variable.
- use ‘_’ as the word separator.
- don’t use ‘-’ as the word separator
Justification
- if ‘-’ is used as a word separator it will generate warnings used with magic quotes.
Example
$myarr['foo_bar'] = ‘Hello’;
print “$myarr[foo_bar] world”; // will output: Hello world
$myarr['foo-bar'] = ‘Hello’;
print “$myarr[foo-bar] world”; // warning message
Single or Double Quotes
- Access an array’s elements with single or double quotes.
- Don’t use quotes within magic quotes
Justification
- Some PHP configurations will output warnings if arrays are used without quotes except when used within magic quotes
Example
$myarr['foo_bar'] = ‘Hello’;
$element_name = ‘foo_bar’;
print “$myarr[foo_bar] world”; // will output: Hello world
print “$myarr[$element_name] world”; // will output: Hello world
print “$myarr['$element_name'] world”; // parse error
print “$myarr["$element_name"] world”; // parse error
Leave a comment