Thursday, July 29, 2010

High-quality C + + / C Programming Guide - Chapter 3 naming



Chapter 3 naming
More well-known naming convention when push Microsoft's "Hungarian" method, the main idea of naming is "variable and function names to add a prefix to increase people's understanding of the program." For example, all of the character variable ch are prefixed, if the pointer variable is an additional prefix p. If a variable from the ppch beginning, it indicates that it is pointing to character pointer.

"Hungary," the biggest drawback is cumbersome methods, such as

int i, j, k;

float x, y, z;

If a "Hungarian" naming convention, it should be written as

int iI, iJ, ik; / / prefix i said int type

float fX, fY, fZ; / / prefix that float type f

Such a cumbersome process makes most programmers can not stand.

According to investigation, without a naming convention can be agreed with all of the programmers, programming textbooks generally do not specify the naming rules. Naming rules for software products, not "as vital to the success of" things, we should not of too much energy trying to invent the world's best naming rules, most of the projects should be developed that makes the satisfaction of naming the members and project implementation.

3.1 Common rules
This section discusses the common rule is adopted by most programmers, we should follow these common rules in the premise, and then expand the specific rules, such as 3.2.

Rule 3-1-1銆?銆恑dentifier should be intuitive and can pronounce, is expected to know Italian culture, not a "decoding."

Identifier or combination of the best words in English, easy to remember and read. Never use Hanyu Pinyin to name. Program in the English words are generally not too complex words should be accurate. For example, not to CurrentValue written NowValue.

3-1-2銆?銆恟ule identifier length shall conform to the "min-length & & max-information" principle.

ANSI C provides a few decades ago the old name are not allowed more than six characters, today's C + + / C no longer have this limitation. In general, the long name to better express the meaning, so the function name, variable name, class name, up to a dozen characters is not surprising. Then the name is more about good? Not necessarily! Such as variable names maxval useful than the maxValueUntilOverflow. One character's name is also useful, common, such as i, j, k, m, n, x, y, z, etc., they are usually used as local variables within the function.

Rule 3-1-3銆?銆恘aming rules as far as possible with the development tools used by the operating system or the style consistent.

For example Windows application identifier is usually a "case" mixed up approaches, such as AddChild. The Unix application identifier is usually a "lower case underlined" approach, such as add_child. Do not mix these two styles to use.

Rule 3-1-4銆?銆恜rocedures alone do not appear similar to the identifier case sensitive.

For example:

int x, X; / / variable x and X confused

void foo (int x); / / function foo and FOO confused

void FOO (float x);

Rule 3-1-5銆?銆恜rocess identifiers do not appear exactly the same local variables and global variables, although the scope of different grammatical error does not occur, but misleading.

Rule 3-1-6銆?銆恦ariable's name should use the "term" or "adjective + noun."

For example:

float value;

float oldValue;

float newValue;

Rule 3-1-7銆?銆恎lobal function's name should use the "verb" or "verb + noun" (verb phrase). Class member functions should only use the "verb", was omitted term is the object itself.

For example:

DrawBox (); / / global function

box-> Draw (); / / class member function

Rule 3-1-8銆?銆恛pposite of group names with the correct meaning of the variables with mutually exclusive or opposite action it creates.

For example:

int minValue;

int maxValue;

int SetValue (...);

int GetValue (...);


2 3-1-1銆?銆恜roposed name to avoid appearing with figures such as Value1, Value2, etc., unless the logic does need number. This is to prevent programmers lazy, unwilling to use their brains and lead to meaningless naming names (because least resistance with the figures).

3.2 a simple Windows application naming
Of the "Hungarian" naming convention to do a reasonable simplification, the following naming rules are simple to use, more suitable for the development of Windows application software.

Rule 3-2-1銆?銆恈lass name and function name in capital letters at the beginning of the word combination.

For example:

class Node; / / class name

class LeafNode; / / class name

void Draw (void); / / function name

void SetValue (int value); / / function name

Rule 3-2-2銆?銆恦ariables and parameters of words beginning with a lowercase letter combination.

For example:

BOOflag;

int drawMode;

Rule 3-2-3銆?銆恈onstant use all capital letters, with underscores to separate words.

For example:

const int MAX = 100;

const int MAX_LENGTH = 100;


Rule 3-2-4銆?銆恠tatic variables prefixed s_ (that static).

For example:

void Init (...)

(

static int s_initValue; / / static variables

...

)

Rule 3-2-5銆?銆恘eed a global variable if no alternative, then the so global variables prefixed g_ (that global).

For example:

int g_howManyPeople; / / global variable

int g_howMuchMoney; / / global variable


Rule 3-2-6銆?銆恈lass data members plus the prefix m_ (that member), so avoid data members and member functions of the parameters of the same name.

For example:

void Object:: SetValue (int width, int height)

(

m_width = width;

m_height = height;

)

Rule 3-2-7銆?銆恆 software library in order to prevent some of the identifiers and other software libraries in the conflict, for a variety of identifiers with the prefix to reflect the nature of the software. For example, all three-dimensional graphics standard OpenGL library functions are gl at the beginning, all the constants (or macro definition) are GL at the beginning.

3.3 Unix applications simple naming rules







相关链接:



Five-stroke input method is not just pole



Converter mp4 to avi



what is mov



Flac to mp3 converter free



How the statistics of the time difference between the two time Points



brief E-Mail Tools



"Torch Light," the game features seven new



How to check the regional oligarchy Dealers



One after another anti-piracy noise compensation 780 000 Shenzhen Haojia Reconciliation



Zha Yufeng: build "long flight" back pillar



Teach you a trick: how to beautify MM eyebrows, eyelashes and eyes



Articles about Strategy And War GAMES



Format Ts



Easy to use Screen Capture



Flash video playback controls reversed



No comments:

Post a Comment