Good habits and good habits of coding

Source: Internet
Author: User

Good habits and good habits of coding

This is what I did not mean to visit other people's blogs. It is a good reference for me who just came out and was somewhat confused.

 

1. Do not hard-compile string/numeric. You can use constants instead. (Improve readability)

Int Count;
Count = 100;
Private static const int ZERO = 0;
If (Count = ZERO)
{
// Perform some operations
}

2. For String comparison, use String. Empty instead "".

3. Do not declare the member variables as public or proteted. Try to use the private member variables and public/protected attributes. (Modify)

4. When we want to operate strings in a loop, use StringBuilder instead of strings, as shown in the following example.

Bad habits:

String temp = String. Empty;
For (int I = 0; I <= 100; I ++)
{
Temp + = I. ToString ();
}

Good habits:

StringBuilder sb = new StringBuilder ();
For (int I = 0; I <= 100; I ++)
{
Sb. Append (I. ToString ());
}

5. Simple operations prefer to use Array than collections. (Recommended here, as appropriate)

6. Compared with ArrayList, Generic Collection is preferred. (Recommended here, as appropriate)

7. Generic Dictionary is more preferred than HashTable. (Recommended here, as appropriate)

8. For string operations and storage, StringCollection and StringDictionary are preferred. (Recommended here, as appropriate)

9. Use suitable data types.

For example, if you want to judge the status, it is better to use bool than int.

Bad habits:

Int Check = 0;
If (Check = 0)
{
// Perform some operations

}

Good habits:

Bool Check = false;
If (! Check)
{
// Perform some operations
}

10. When using as for type conversion, determine the null value of the converted value.

Class
{

}
Class B:
{

}
B objB = new B ();
A objA1 = (A) objB;
A objA2 = objB as;
If (objA2! = Null)
{
// Perform the required operations
}

11. Create a wcf proxy and use the using expression. (This can be used in many places)

Using (Cerate the proxy)
{
// Perform the required operations
}

12. For expensive resources (such as Connection and File), follow the 'acquire late and release early '(obtain as late as possible and release as early as possible) principle.

Example: If you want to use the SqlConnection object during data operations, create an instance at the method level rather than the class level.

Code

If you want to create a class-level SqlConnection instance, make sure that your class implements the IDisposable interface and clears the SqlConnection instance in Dispose.

Code

13. If you don't want others to extend your class functions, use 'secaled '.

14. Avoid declaring 'delimiter' for each class because it will increase the lifecycle of classes that do not require resident memory.

15. Compared with manual threading, Thread Pool is preferred.

16. Do not call other methods in a loop. (Call function performance loss)

For example:

Bad habits:

For (int I = 0; I <= 100; I ++)
{
Calculate (I );
}

Good habits:

For (int I = 0; I <= 100; I ++)
{
// Write the Calculate logic directly.
}

17. Do not handle exceptions in a loop, but put the loop processing logic in try/catch.

Bad habits:

For (int I = 0; I <= 100; I ++)
{
Try
{
}
Catch (Exception ex)
{
Throw ex;
}
}

Good habits:

Try
{
For (int I = 0; I <= 100; I ++)
{
}
}
Catch (Exception ex)
{
Throw ex;
}

18. You do not need to handle application logic through exceptions

For example:

Bad habits:

Try
{
Int x, y, z;
X = 0;
Y = 10;
Z = y/x;
}
Catch (DevideByZeroException ex)
{
Throw ex;
}

Good habits:

Try
{
Int x, y, z;
X = 0;
Y = 10;
If (x! = 0)
{
Z = y/x;
}
}
Catch (Exception ex)
{
}

19. Compared with for/while, foreach loops are preferred. [Correction]

20. In a multi-layer architecture system, the interaction between layers is more inclined to use objects to transmit data than DataSet/DataTables.

Author: Zhu Yulin Source: http://zhuqil.cnblogs.com this article copyright belong to the author and blog park a total, welcome to reprint, but without the author's consent must retain this paragraph of the statement, and in the Article Page clearly given the original connection, otherwise, you are entitled to pursue legal liability.

Related Article

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.