OftenCodeThere will always be problems when dealing with them. On the one hand, it is the same as yourself. In the past, there was no problem. I usually think that it is unlikely to make a mistake. On the other hand, the project time is tight and there is not so much time to think about it carefully, more than a dozen functions are waiting for you to implement every day, and most programmer will wait.ProgramThe problem is found again during the test.
Below I will list the mistakes I have made in project development, some of which I have seen and I think they are unreasonable.
1 time processing
For example, if the current time is the datetime. Now attribute, and the current time is the next month, will you use the following code?
Datetime current = datetime. now;
Datetime nextmonth = new datetime (current. Year, current. Month + 1, current. Day );
Also, whether to directly call current. day + 1 and current. Day-1 on the date of yesterday and tomorrow.
2. return values of database fields
If the field value of the database to be returned is null, for example
Select lastname from northwind. DBO. Employees where employeeid = 999
Is the following method reasonable in the program?
Dataset DS = sqlhelper. executedataset (SQL );
If (Ds. Tables [0]. Rows. Count> 0 ){
If (Ds. Tables [0]. Rows [0] ["lastname"]. tostring ()! = "")
{String name = Ds. Tables [0]. Rows [0] ["lastname"]. Trim ();}}
Sometimes, you can use it for convenience.
Int age = int32.parse (Ds. Tables [0]. Rows [0] ["Age"]);
3. SQL statement writing
I often see the following SQL statement writing method. What's the problem?
1) "select lastname from northwind. DBO. Employees
Where employeeid = "+ txtemployeeid. text;
2) int emplyeeid = convert. toint32 (txtemployeeid. Text );
String SQL = "select lastname from northwind. DBO. Employees where employeeid = {0}";
SQL = string. Format (SQL, emplyeeid );
4. Where parameter location of SQL
In either case, You can directly splice the SQL where parameter in the Code on the interface, or splice the SQL parameter in the Dal method.
1) datetime dtfrom = convert. todatetime (txtdatefrom. Text );
Datetime dtto = convert. todatetime (txtdateto. Text );
String where = "where ottime between" + dtfrom + "and" + dtto;
2) datatable TBL = otreport. getmonthreport (dtfrom, dtto );
Then, in the same code as above, concatenate the SQL whrer parameter in getmonthreport.
What are the differences between the two methods?
5. Which of the following methods do you use to compare strings?
String OBJ = txtemployeename. text;
1) int ret = string. Compare (OBJ, "CN \ shli", true );
2) "CN \ shli" = OBJ
3) obj. Trim (). Equals ("CN \ shhli", stringcomparison. invariantcultureignorecase)
6. A number is still in place to indicate the service processing status.
For example, the application, approval, rejection, and approval status are still marked with 1, 2, 3 in the program.
1 indicates that the application is in progress, 2 indicates that the application is being approved, and 3 indicates that the application is rejected.
7. If the numeric type is required, the string type is still used.
For example, if a column value cannot be obtained directly in an SQL statement, it needs to be determined in the program. Therefore, an additional column is added and the type is string.
Datacolumn otsummary = new datacolumn ("otsummary", typeof (string ));
TBL. Columns. Add (otsummary );
8. On an ASP. NET page, you are still using static variables to save temporary variable values. For example, you can add users or modify users on a page at the same time. Therefore, you need a static variable to save whether the current action status is insert or update.
Public partial class otdefault: system. Web. UI. Page {
Static string action = "insert ";
}
9 at the interface layer, you are still using the following method to convert user input data
For example, you need to enter the employee number. In the employee table you designed, the employee number field type is int, And it is self-increasing.
Int employeeid = convert. toint32 (txtemployeename. Text );
Or use int employeeid = int32.parse (txtemployeename. Text );
10 when setting the current value of dropdownlist, selectedvalue is preferred.
For example, you have a dropdownlist control that binds the names of all departments of the company. at this time, a user a logs on to the system. You need to set the current selected project in the dropdownlist as his department. The code is roughly the ddldepartment written in this way. selectedvalue = userdal. getdepartmentname (currentuser)
11 because we need to rounding the numerical result, when the precision requirement is to keep one decimal place after the decimal point, you can use this method.
Math. Round, good. The method is correct. However, you are always skeptical about the following results.
Math. Round (3.44, 1) = math. Round (3.45, 1) = 3.4
12. I like to use query strings. It is usually out-of-the-box and easy to use.
String id = request. querystring ["guid"];
Guid key = new GUID (ID );
13. On the report Presentation page, you can use the Ajax control to select a date range and obtain report data based on the selected time range. Two controls are available: txtdatefrom and txtdateto.
<Asp: textbox id = "txtdatefrom" runat = "server" width = "160"> </ASP: textbox> <asp: imagebutton id = "btndatefrom" runat = "server" width = "16" imageurl = "image/calendar.png" Height = "16"> </ASP: imagebutton>
<Ajax: calendarextender id = "calendarextender1" runat = "server" popupbuttonid = "btndatefrom" targetcontrolid = "txtdatefrom" format = "yyyy-mm-dd"> </ajax: calendarextender>
<Asp: textbox id = "txtdateto" runat = "server" width = "160"> </ASP: textbox> <asp: imagebutton id = "btndateto" runat = "server" width = "16" imageurl = "image/calendar.png" Height = "16"> </ASP: imagebutton>
<Ajax: calendarextender id = "calendarextender1" runat = "server" popupbuttonid = "btndateto" targetcontrolid = "txtdateto" format = "yyyy-mm-dd"> </ajax: calendarextender>
Compile and run without any problems. One day, the user wants to view all services that occurred on the previous day, so he can use the following code to obtain the time of the day selected by the user.
Datetime dtfrom = convert. todatetime (txtdatefrom. Text );
Datetime dtto = convert. todatetime (txtdateto. Text );
The program runs normally, but no data is displayed. It's strange that there were more than 100 businesses on that day, and no data was found.
14 In order to return a set of fields, we still get used to the arraylist.
For example, to read the names of all departments in the department table in the database, the following code
Arraylist list;
While (dataread. Read ())
{List. Add (dataread ["departmentname"]. tostring ());}
Sometimes this is done to save time, and an array is directly returned.
Public static string [] getdepartmentlist (string deparmentid)
15 not used to handling exceptions, but only handling exceptions in the development phase.
In the interface layer, Cath is used to intercept exceptions, resulting in a pile of try {} catch {} in the interface layer {}
At the business layer, you do not know what behavior will cause exceptions, but are unwilling to verify the parameters of the method.
In the data access layer, when a database exception occurs, it is not thrown up, resulting in a problem that has not been found for a long time.
16 For the same project, different customers have different requirements for the same function.
Therefore, the code is directly copied and maintained separately.
For example, for an IPR project, there are two customers, one is Customer A and the other is Customer B. After the IPR project is developed, the IPR project developer of Customer A is responsible for maintaining the copy of the project and the IPR project developer of Customer B, it only maintains his copy.
The same features have different requirements. For example, for a report, the same WIP report, the report format required by Customer A is different from that required by Customer B, and the report must contain their own company logo, customer A needs to present data by production group. Customer B needs to present data by project name. Maybe another potential customer C may ask the report to present WIP data by process.
17 in the database field, you like to use the auto-increment ID as the primary key, and you are creating a table storing configuration information, called iprparam.
ID is the primary key. When ID is 1, it indicates that the maximum sequential number of the currently used business. ID is 2. The current report is presented according to the production group, according to the employee ID, this auto-increment ID keeps increasing, and there has never been a problem in your development stage. The above two rows of data look like this,
The configuration table is finally ready, and the program is ready, so the configuration data table is imported and exported using SQL to the server. If an error occurs, the system prompts that the data cannot be imported.
So I thought that I could use a tool to convert the data in the SQL table into an insert statement, so I would like to use it,
A corresponding SQL statement is generated soon. Open the query analyzer and execute the SQL statement generated above. An error is returned.
Do you have to use the SQL Enterprise Manager to manually input the data directly in the database table on the server?
The problems listed above are not necessarily bugs. In some cases, there will be no problems.
When these problems are hidden in various business code, can you still notice and avoid them?
You are welcome to add frequent errors during development, so that our projects will have fewer bugs, more stability, and more stability.