C # Coding specifications summarized by myself-2. Naming and selection

Source: Internet
Author: User

In the previous article, we talked about the naming conventions for Identifiers. Today we will talk about how to select an identifiers.

To put it simply, you must be aware of an identifier.

 

  • The name must be able to express the meaning of the identifier

The identifier name must express the meaning of the identifier. Meaningless V1, V2... Names such as VN.

Public static void clonechars (char [] cl1, char [] Cl2)

{

For (VAR I = 0; I <cl1.count (); I ++)

{

Cl2 [I] = cl1 [I];

}

}

The caller of the Code does not know whether cl1 or Cl2 is the char array to be copied. He must go to this function and read the entire logic before calling the function. In addition, it takes several seconds for me to think about what we are doing.

If it is changed to a meaningful name: source and target, the caller will know how to use the method at the first glance.

Public static void clonechars (char [] source, char [] target)

{

For (VAR I = 0; I <source. Count (); I ++)

{

Target [I] = source [I];

}

}

  • Select a name with a single meaning

Use professional words to avoid empty words.

For example, class binarytree

{

Public int size ()

When you see this line of code, what do you think of the size? The height of the tree, the number of nodes, or the size of the tree in the memory?

We can use a more specific word to tell readers the specific meaning of this method, such as height, nodesnum, and memory_bytes.

  • Use a name that does not produce Ambiguity

When naming an identifier, there must be no ambiguity. Many errors in the Code are caused by the ambiguity in naming. For example:

Public const int cart_too_big_limit = 10;

If (shoppingcart. Count ()> = cart_too_big_limit)

{

Logerror ("too events items in cart .");

}

This piece of code has a classic "one size difference defect ". When determining a shopping cart item, I should use ">" or "> =". I cannot determine from the code, so this place is prone to bugs. if we change to max_items_in_cart, I can immediately determine that ">" is used here ".

 

  • The name must match the user's expectation.

The reason why some names are misleading is that readers are preemptible to them, even if you do not mean so. In this case, you 'd better select a name that matches the user's expectation.

For example, many programmers get used to the get-started method as a "lightweight accesser", which simply returns member variables.

The following code is displayed:

Class binarytree

{

Public int getnodescount ()

The attacker only returns the private int _ nodescount; private variable.

However, if your code is actually a very time-consuming code, the internal implementation is to traverse all Tree nodes in a breadth-first manner. You also need to go to the database to find the relationship between the parent node and the child node, and then accumulate it.

Therefore, this time-consuming method may be called multiple times by the caller due to your name, resulting in a reduction in the overall system performance.

If you change the name to computenodescount, the caller will know that this operation is time-consuming and needs to cache the call results and reduce the number of calls.

  • Add more information for the name

A variable name is like a small comment. Although the space is not large, no matter how much extra information you squeeze in a hit, this information will be seen every time someone sees the name.

 

Example: When you receive a request form from a Web page, there may be insecure code, such as injection statements. In this case, you must name the form to indicate that the data is not secure, you can use unsafeformdata. after calling the security check method, you can change it to safeformdata = handleunsafedata (unsafeformdata ). in this way, the code reader can safely use this variable.

The following table provides more examples for appending additional information to a name.

  • Do not show off

The most commonly used and well-known word. Do not show your knowledge when naming codes. It is a real cool to let your code express your thoughts quickly and accurately.

For example, public static string convertxml2html (string sourcepath)

When some people see this method, what is the purpose of this 2? Is it to convert an XML file into two HTML files?

People familiar with English culture may know that this is a slang expression of. If you cannot ensure that everyone who reads your code knows that 2 is the abbreviation of. Use convertxmltohtml.

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.