Naming is the core of program planning. The ancients believed that as long as they knew the real name of a person, they would gain incredible power over that person. As long as you give things a correct name, it will give you and people later more powerful than the code. Don't laugh! The name is a long-term and profound result of a thing in its ecological environment. In general, only understanding about system naming is the core of program planning. The ancients believed that as long as they knew the real name of a person, they would gain incredible power over that person.
Quantity. As long as you give things a correct name, it will give you and people later more powerful than the code. Don't laugh!
The name is a long-term and profound result of a thing in its ecological environment. In general, only programmers who know the system can
Obtain the most appropriate name. If all the names are suitable for their own purposes, the relationship is clear and the meaning can be deduced.
You can expect it.
If you find that only a few of your names can match their corresponding items, you 'd better look at your design again.
Class name
Before naming a class, you must first know what it is. If you provide clues through the class name, you still cannot remember whether this class is
Then your design is not good enough.
A hybrid name composed of more than three words is easy to cause confusion between various entities of the system. let's look at your design and try to use (CRC Se-
Ssion card) to see if the object corresponding to the name has so many functions.
Naming a derived class should avoid the temptation to include its parent class name. The name of a class is only related to itself, and its parent class is nothing.
Off.
Sometimes suffix names are useful. for example, if your system uses an agent, you can name a part as "bottom
DownloadAgent is used to transmit real information.
Method and function name
Generally, every method and function executes an action. Therefore, their names should clearly indicate what they do: use
CheckForErrors () replaces ErrorCheck (), and DumpDataToFile () replaces DataFile (). This can also enable functions and
Data becomes a more identifiable object.
Sometimes suffix names are useful:
Max-indicates the maximum value that an object can assign.
Cnt-the current value of a running count variable.
Key-Key value.
For example, RetryMax indicates the maximum number of retries, and RetryCnt indicates the current number of retries.
Sometimes prefix names are useful:
Is-it means to ask a question about something. At any time, people will know that this Is a problem.
Get-indicates obtaining a value.
Set-meaning to Set a value
For example, IsHitRetryLimit.
Do not use uppercase letters for all acronyms
In any of the following situations, you can use uppercase letters and other lowercase letters to replace all uppercase letters for the table.
Show acronyms.
Use: GetHtmlStatistic.
Not used: GetHTMLStatistic.
Reason
People seem to have a very different intuition when naming with acronyms. Unified rules are the best, so that the meaning of the name is complete.
All can be pre-notified.
Here is an example of NetworkABCKey. it is very confusing to note whether C in ABC or C in key. Some
People don't care about this, but others hate it. So you will see different rules in different codes, so that you do not know how
Call it.
For example
Class FluidOz // do not write it as FluidOZ
Class GetHtmlStatistic // do not write it as GetHTMLStatistic
Class name
Use uppercase letters as the separator for words, and use lowercase letters for other letters
Uppercase is used for the first letter of the name.
Do not use underscores ('_')
Reason
According to many naming methods, most people think this is the best way.
For example
Class NameOneTwo
Class Library name
Currently, namespaces are becoming more and more widely used to avoid class name conflicts between different vendors and group class libraries.
Before using a namespace, to avoid class name conflicts, you can add a unique prefix before the class name.
Yes, of course it will be better to use more.
For example
John Johnson's data structure class library can use Jj as the prefix, as shown below:
Class JjLinkList
{
}
Method name
Use the same rule as the class name
Reason
Most people using all different rules find that this is the best compromise.
For example
Class NameOneTwo
{
Function DoIt (){};
Function HandleError (){};
}
Class attribute name
The attribute name should be prefixed with the character'm.
The prefix 'M' follows the same class naming rule.
'M' always acts as a modifier at the beginning of the name, just like a reference starting with 'R.
Reason
The prefix 'M' prevents any conflicts between class attributes and method names. Your method name and attribute name are often very similar, especially for accessing elements.
For example
Class NameOneTwo
{
Function VarAbc (){};
Function ErrorNumber (){};
Var mVarAbc;
Var mErrorNumber;
Var mrName;
}
Parameter name in method
The first character uses lowercase letters.
All words after the first character are capitalized according to the class naming rules.
Reason
You can always know the variable corresponding to it.
You can use a name that is similar to a class name without duplicate name conflicts.
For example
Class NameOneTwo
{
Function StartYourEngines (
& $ RSomeEngine,
& $ RAnotherEngine );
}
Variable name
All letters are in lower case.
Use '_' as the boundary of each word.
Reason
In this way, the scope of variables in the code is clear.
All variables look different in the code and are easy to identify.
For example
Function HandleError ($ errorNumber)
{
$ Error = OsErr ();
$ Time_of_error = OsErr-> getTimeOfError;
$ Error_processor = OsErr-> getErrorProcessor;
}
Reference variables and function return references
The reference must be prefixed with 'R '.
Reason
Makes variables of different types easy to recognize
It can determine which method returns changeable objects and which method returns unchangeable objects.
For example
Class Test
{
Var mrStatus;
Function DoSomething (& $ rStatus ){};
Function & rStatus (){};
}
Global variables
The global variable must have the prefix 'G '.
Reason
It is very important to know the scope of a variable.
For example
Global $ gLog;
Global & $ grLog;
Define naming/global constants
Global constants are separated.
Reason
This is the tradition of naming global constants. Do not conflict with other definitions.
For example
Define ("A_GLOBAL_CONSTANT", "Hello world! ";
Static variables
Static variables should have the prefix's '.
Reason
It is very important to know the scope of a variable.
For example
Function test () {static $ msStatus = 0;
}
Function name
The function name follows the c gnu convention. all letters use lowercase letters and '_' to separate words.
Reason
This makes it easier to distinguish the names of associated classes.
For example
Function some_bloody_function ()
{
}
Error return detection rules
Check all system call errors unless you want to ignore them.
Define the system error text for each system error message to include.
Braces {} rules
Two of the three main braces are acceptable, and the first is the best:
Place braces in the same column below the keywords:
If ($ condition) while ($ condition)
{{
......
}}
The traditional brackets of UNIX are the same as the keywords, and the ending brackets are the same as the keywords:
If ($ condition) {while ($ condition ){
......
}}
Reason
Non-principles that have aroused heated debate can be solved through a compromise. either of the two methods is acceptable.
Most people prefer the first one. The reason is the scope of psychological research.
There are more reasons to prefer the first one. If the character editor you use supports matching brackets (for example, vi ),
The important thing is to have a good style. Why? We say that when you have a large program and want to know where it is
. Move the brackets to the beginning, and press the button editor to find the ending brackets. for example:
If ($ very_long_condition & $ second_very_long_condition)
{
...
}
Else if (...)
{
...
}
To move from one block to another, you only need to use the cursor to match your brackets. you do not need to move back and forth to the end of the line.
Find matching brackets.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service