The way to code: It's important to take a good name

Source: Internet
Author: User

Code is the child of the programmer, it is important to give "child" a nice name!

In project development, the variables, functions, and classes that we touch are defined by the project itself, often to solve some specific domain problems, introduce a variety of concepts, the name of the code corresponds to the problem domain or the concept of the area of the program. So, for a system that is well-named, code-compliant, and simple to design, the most straightforward way to understand a system very quickly is RTFC (Read the fucking code). For a constantly evolving system, the readability of the code is of paramount importance, the first problem to be solved is the name, variable name, function name, class name, etc. need to be carefully considered, seriously, a concise, can clearly express the concept and intention of the name is particularly important.

Read the "Code Clean Road" This book found that the content in our own projects abound, casually come up with a piece of code can be used as a negative word for everyone to talk about half a day. Accumulation for a long time, causing the code moldy deterioration, the name is also no discipline, handy. Read this code and hit the south wall with the heart. The following is a discussion of the principles of naming related to the questions in your project and the "Code Clean Way".

1. Principle: a veritable
    • Choosing a name is a serious matter and choosing a good name is important.
    • If the name needs a comment to add, it's not a good name.
    • The most important thing is to be true, and the name can express the concept and intention.

Bad:

int t = Currenttime.elapse (e);// The elapsed time, In milliseconds ...< Span style= "margin:0px; padding:0px; Color: #0000ff; font-family: ' Courier New '; font-size:12px; " >if (T >  Timeout_ Value) {Zebra::logger->debug ( " "       
Good:
int Elapsed_ms =Currenttime.elapse (e); .... if (Elapsed_ms > timeout_value) {zebra::logger->debug ("-----One cycle with%u milliseconds---", Elapsed_ ms);}      
2. Principle: Avoid misleading
    • Must avoid leaving behind the wrong clue that hides the code's meaning
    • Avoid using words that contradict your intentions
    • Beware of the use of smaller names in different places
    • Inconsistent spelling is misleading

Bad:

std::vector<Int> account_list;// _list is a misleading, accounts will be better bool Sendtozoneserver (); // and the following functions differ very little // Sendtoallzoneservers will be better.        

  3. Principle: Make a meaningful distinction

    • Code is written to people, just to meet the requirements of the compiler, it will cause confusion.
    • The naming of digital series (A1,A2,...) is purely misleading.
    • Meaningless nonsense: A, an, the, Info, Data

Bad:

void Copy (Char a1[],Chara2[]) {for (size_t i = 0; a1[i]! = ' + ' ; i++) a2[i] = a1[i];}       

Good:

void Copy (Char source[],Char dest[]) {for ( size_t i =  0; Source[i]! = \0) Dest[i] = source[ I];}        

  4. Principle: Use a readable name

    • Avoid excessive use of abbreviations
    • Easy-to-read name Exchange

Guess what the following classes are for? And what do others say about these classes?

According to these simply perverted abbreviations, if there is no comment is basically difficult to know what to do, when you communicate with others, you have to read a letter "X-l-q-y", "L-t-q Manager", the ghost know what you say? PS. Xlqy-xianlvqiyuan (Immortal), Ltq-liaotianqun (chat group), have such a name is also drunk.

Bad:

Class xlqy;  Class Fcnv;  Class Ltqmanager;   

  5. Principle: Use searchable names

    • Avoid using magic number
    • Avoid the use of single-letter, or the occurrence of very high frequency of the short letter combination (the grasp of the degree of attention).

Bad:

if (obj->Base->id = =4661)// 4661 what is it?  Cmd:: XXXXXXX;} int e; Span style= "margin:0px; padding:0px; Color: #008000; font-family: ' Courier New '; font-size:12px; " >// How to find? Xxxx:iterator it; // when the range of variables is larger, it's not necessarily a good name.        

Good:

#define Ojbect_feedback_card 4661if (Ojbect_feedback_card = = obj->base->id) {   usetype = CMD: : XXXXXXX;}   

  6. Principle: Name as far as possible from the solution area or problem area

    • Using the Solution realm name

Most of the students who write the code are from CS, the terminology, the algorithm name, the pattern name, the mathematical terminology, although used. such as the account class implemented by the Accountvisitor:visitor pattern.

    • Use the name of the problem domain

Most of our code is these names, do not understand to find planning to ask, basically is the name of the function-related.

7. Principle: Appropriate use of meaningful contexts
    • Well-named classes, functions, namespaces to place names, providing context to the reader.
    • There are only two or three variables, prefixed to the name.
    • Charm, more than three variables are considered encapsulated as concepts, adding a struct or class.

Bad:

//Looking neat? Easy to use? DWORD Love_ensure_type_;//Current Love insurance type DWORD love_ensure_ret_;// buy Love Insurance response flag DWORD Love_ensure_total_; // now has the number of stamps DWORD Love_ensure_..._; //... DWORD love_ensure_..._; //...

  Finally: our C + + naming conventions

    • Filename:

      • Capitalize the first letter and combine multiple words together
      • Such as:SceneUser.h, Sept.h
    • Class name/namespace name:

      • Capitalize the first letter and combine multiple words together
      • Use a noun or noun phrase
      • Avoid using c prefixes, such as:CSept
      • Such as:SceneUser, SeptWar
    • Name of function:

      • First Letter Lowercase
      • Use a verb or verb phrase
      • Avoid using orphaned global functions, which can be encapsulated within a class or namespace
      • Get, set, is prefix use
      • Such as:fuckYou(), levelup()
    • Variable name:

      • All letters are lowercase, and multiple words are separated by an underscore.
      • Private member variable plus suffix _, public variable not used.
      • Avoid using orphaned global variables, which can be encapsulated within a class or namespace.
      • Such as:quest_id, questid_

Naming is a serious matter, we need to take seriously, the name represents a concept, the name represents the intention you want to express, good name is the first condition of readable code:

    • When you write down any line of code, mind your own code is for others to see.
    • Take a good name for a function, variable, class, and follow the rules and principles.
    • See a name that does not conform to the rules and principles, and ruthlessly kill it, especially the functional code.

http://kb.cnblogs.com/page/517741/

The way to code: It's important to take a good name (turn)

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.