Do not write code to complete the code

Source: Internet
Author: User

From my work to now, I have participated in many projectsCodeThe Design and writing work has also brought a lot of people to design and write code. Generally, people who work in my team just now experience a very painful period, during that period, he will be severely criticized by me for "writing code to complete the code.

In fact, all these problems occur. Next I will talk about some common mistakes that I have seen by code writers over the years.

(The code in this article is pseudo code)

1. Do not follow the "first-out domain" Principle

Original code

Public void save (string ID)
{
If (string. isnullorempty (ID ))
{
// Complex business code
}
Else
{
Return;
}
}

Improved code

Public void save (string ID)
{
If (! String. isnullorempty (ID) return;
// Complex business code
}

Do you understand? Is it much more readable after the change to the following ?! Whether in the method body or in the process statement, we should try to lean the return statement forward, which will increase readability;

2. The method is too long and complicated.

Original code:

Public void save (string ID)
{
For ()
{
If ()
{
}
Else
{
}
For ()
{
}
}
Switch ()
{
Case:
{
}
Case:
{
}
Default:
{
}
}
}

Improved code:

This method can only be split. Comrades, this split should follow the principle that the length of the Business Code in a method should not exceed five lines (except for the value assignment statement ). This is difficult, but it can be done with a try.

3. Write the User Control ID to death

Original code:

<Script language = "JavaScript" type = "javascript/text">
Function Test (string txtid)
{
Document. getelementbyid ("usercontrols1 _" + txtid)
}
</SCRIPT>

Improved code:

<Script language = "JavaScript" type = "javascript/text">
Function Test (string txtclientid)
{
Document. getelementbyid (txtclientid)
}
</SCRIPT>

Do you understand? In JavaScript code, you can pass a client ID instead of a server ID. Although this change is very small, it will definitely cause endless troubles for you, if you write this code on the user control, and the user control is used twice on a page, can you write the first statement to complete the functions of the second user control?

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.