Program code should maintain good standards (C #)

Source: Internet
Author: User
Well, this is a great old saying. But I still hope that more programmers can understand the influence of some details and habits on program reading. This largely determines the portability of the program.

1. Note that spaces are reserved between variable values. Some programmers often do not pay attention to it.
Bad:

Body.txt Version. Text = ib. Version. ToString ();
Body. oriDate. Value = ib. CreatedDate;
Body. revDate. Value = ib. UpdatedDate;

Okay:

Body.txt Version. Text = ib. Version. ToString ();
Body. oriDate. Value = ib. CreatedDate;
Body. revDate. Value = ib. UpdatedDate;

Comments:
A single sentence does not mean that when dozens of rows are together, it is enough for you to have a headache.

2. For or if statements, keep braces.
Bad:

If (I! = ArrHidBrand. Count-1)
Hidbrand + = ",";

Okay:

If (I! = ArrHidBrand. Count-1)
{
Hidbrand + = ",";
}

Second:

If (I! = ArrHidBrand. Count-1) hidbrand + = ",";

Comments:
When many statements of the same type are nested, the first method is easy to confuse and make mistakes, the second method is clear and understandable, and the third method is relatively easy to understand.

3. Avoid complex sentence structures and syntaxes, such as ternary operators. The following is an example.
Bad:

Body.txt HidBrand. Text = hidbrand = ","? "": Hidbrand;

Okay:

If (hidbrand = ",")
{
Body.txt HidBrand. Text = "";
}
Else
{
Body.txt HidBrand. Text = hidbrand;
}

Comments:
I believe that you can understand the first writing method only when you think about it. Good code should be pleasing to the eye, and many of us do not pay attention to it.

4. Duplicate code extraction to reduce repeated code. This is a high requirement. I hope everyone can review their code and try to streamline their code.

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.