Reduce the judgment in the program

Source: Internet
Author: User

If too many judgments are made, the program may be complicated, and maintenance may be conceivable when conditions are added in the future. Insus. NET has the following code for a netizen post on the Forum:



The problem with this method is the judgment of the Session to determine whether the Session is null. The correct method should be:

Session []! =


Now, we will not discuss the problems and causes here. We will discuss how to design the drop-down menu and how to judge the reduction conditions. From the two pieces of code, the user put the previous page in the drop-down menu, let the user choose, and enter some values in the text box, to switch to the second page, then judge to query the selected conditions.

No matter the first code or the second code, there are many If statements, that is, many conditions are judged. You can add it later. As long as conditional modification is available, the user must modify both the first and second sections of the Code.

From the second code, users want to search for some fields in a table. In the drop-down menu of the first code, some field names are replaced with a popular name for users to see and understand. The netizen thought it was good, but it was not well designed and brought about the complexity of the program. What do we say?

The DropDownList drop-down menu has two attributes: one Value, one Text, and the other Text. The content is displayed to the user, and the Value is used by the programmer. Therefore, when designing the DropDownList data source, it should be the common name corresponding to the field name. [Author] <---> author; [bookName] <---> name of the book, and so on.

In this way, the user can see the selection on the DropDownList on the first page, such as the author, the title of the book. During program processing, select Yes, which is the real field name. What are the advantages of doing so? We can see from the reconstruction program.

The first code can be reconstructed as follows:



The second code can be reconstructed as follows:



After reconstruction, you may not have the opportunity to modify the two sections of code. Only increase or decrease the data source of DropDownList.

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.