Code style and programming habits

Source: Internet
Author: User

Whzeng@126.com

1. reasonable and appropriate comments
Reasonable and appropriate comments are a good way to increase code readability, but comments for comments are a waste of time.
2. simple and efficient naming
There are generally three naming rules: Hungary naming, camel naming, and Pascal naming.
2.1 Hungary naming law
It is widely used in environments like Microsoft Windows. Variables used in Windows Programming (including macros)
The naming rules of the Hungarian naming method, which is developed by a competent Microsoft programmer Charles sideni
(Charles Simonyi) proposed. The Hungarian numerator adds a lowercase letter symbol before the variable name.
The identifier is used as the prefix to identify the scope and type of the variable. These symbols can be used at the same time. The sequence is first M _ (member
Variable), then pointer, then simple data type, and other. For example, m_lpszstr indicates pointing to a string ending with 0 characters.
Long pointer member variable of the string.
The key to the Hungarian naming method is: the identifier name starts with one or more lower-case letters as the prefix;
An uppercase word or a combination of multiple words that specify the purpose of a variable.

2.2 camel naming
A combination of uppercase and lowercase letters is used to form the names of variables and functions. For example, the following uses the camel naming method and
The same function named by the line method:
Foo: historyitem ();
Foo: sethistoryitem (void * item );
Void set_history ();

Each logical breakpoint in the first two function names is marked with an uppercase letter;
The third function name uses the underline method. Each logical breakpoint in the function name is marked with an underscore.
The camel naming method has become increasingly popular in recent years. In many new functions libraries and Microsoft Windows environments, it
When used properly, it is widely used. On the other hand, the underline method became popular after the emergence of C, in many old programs and Unix
Environment, it is widely used.

2.3 Pa SCA naming convention
Similar to the camel naming method. However, the trigger name method is lower-case, while the Pascal method is upper-case, for example:
Foo: historyitem ();
Foo: sethistoryitem ();
Both adopt the Pascal naming method. In C #, Pascal naming and camel naming are mostly used.

3. Comparison of the three
I personally think that the Hungarian naming method is too cumbersome, and variables or types are differentiated by prefixes, which is less efficient in Development (compared with the latter two,
The latter two generally do not need to be prefixed to differentiate types), suitable for the use of C and C ++ mixed programming (due to the C compiler for the Type
Check is weak. Different types, such as signed integer, may cause improper use, but the caller can be reminded after the prefix is added .);
The camel naming method is concise and elegant. It does not need to be named to distinguish different types. Instead, it is determined by the meaning of the types, and the development efficiency is high.
(With the Hungarian naming method), suitable for pure C ++ development.
I personally think that the camel naming method is very simple and elegant, but Chinese people will be too high in ms, and some computer books are either not named
If you are familiar with the Hungarian naming method, it is suitable for the development of C and C ++ mixed languages. For pure C ++ or C
The Hungarian naming convention is a waste of youth!
The following are examples of the camel naming method.

Class Foo {
Public:
Foo ();
~ Foo ();

Enum flag {// The enumeration, struct, and class are all types and do not need to be distinguished by names
Pageflag = 0,
Frameflag,
};

Historyitem & item (); // you do not need to add get to indicate that it is a get. Otherwise, if the caller does not use an interface once
// Add a get to vomit blood!
Void sethistoryitem (); // but the set parameter indicates the set value.

PRIVATE:
Historyitem * m_item; // although M _ is used to represent class members, the prefix of the type is not added before the name.
};

Contact Us

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

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.