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?