Summary of "clean code"

Source: Internet
Author: User

I recently read some experiences from two books, code cleansing and 45 practices for agile development for efficient programmers.

I hope I can share with my predecessors. I personally think that a good program can be written by following the following points.

Encoding principles

1. Make the code cleaner than when you come, and the code checked in each time is cleaner than when you sign out.

2. The code should clearly express its intent and use the code to explain the code.

3. The code should have a sense of attention.

4. The class should have only one responsibility, one reason for modification, and work with a few other classes to achieve the expected system behavior.

5. Exceptions are also a responsibility.

6. Write and solve the log.

7. Warning is an error. process every warning in the project.

8. Provides useful error messages.

9. Test code is as important as normal code.

10. Do not skip the test.

11. The project manager should keep abreast of the project progress.

12. Do not make a big design that exceeds your needs, and do a simple solution.

Code Specification

1. Fixed variables must also be meaningful.

For example:

if(state==4)

Can be modified

int state_close=4

if(state==state_close)

2. Do not use a single letter as the variable name

For example, change int I = 0 to int length = 0;

In particular, you cannot use letters like o l to complete variables.

3. Necessary and unnecessary prefixes

The name can be changed to jobname, indicating the name of the job rather than the user name.

A prefix such as s_name is unnecessary. Everyone knows that name is a character type.

4. Do not use callback functions.

This is a cup

new OrderInfo().Save().getid().toString();

Change

int rowid=new OrderInfo().Save().getid()
txt_id.txt=rowid.toString();

5. Do not interrupt the normal logic for exception handling. If you do not handle this exception, do not capture it.

The following is an incorrect example.

                int orderid = 0;
try
{
orderid = Convert.ToInt32(Request.Params["orderid"]);
}
catch {

}
if (billingForm.Address == null)
{
billingForm.Address = Profile.AccountInfo;

}

6. Hide your data, variables, and useless methods.

 

        private static Hashtable parmCache = Hashtable.Synchronized(new Hashtable());

Reduce public methods and variables.

7. function expression

     public IList<ItemInfo> GetItemsByProduct(string productId) {

Let other functions know what you want to do.

8. delete dead code. do not comment on the code because we have source code management.

9. Do not add additional comments.

Int I = 0; // auto-Increment

This annotation is unnecessary.

10. Do not input bool as a parameter.

        protected void But_Save(object sender, EventArgs e) {
bool _Save = Request.Params["rowid"].ToString().Equals("-1");
Save(_Save);
}
protected void save()
{
if (_Save)
{
//insert...
}
else
{
//update...
}
}

Change

        protected void But_Save(object sender, EventArgs e) {
bool _Save = Request.Params["rowid"].ToString().Equals("-1");
if (_Save)
{
insert();
}
else
{
update();
}
}
protected void insert()
{
}
protected void update()
{
}

11. Use as few languages as possible for source files

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.