Advantages of table-driven development

Source: Internet
Author: User
Table Driving Method
Table-Driven Method

1: What is the table-driven method.
The table-driven method is a programming mode (Scheme) that searches for information from a table without using logical statements (if and case) it eliminates the IF, else, And swith statements that appear everywhere in the Code, so that the messy code can be simplified and clear. In simple cases, the table-driven method may make logical statements easier and straightforward, but as the logic becomes more complex, the table-driven method becomes more attractive.
2: Example of the table-Driven Method
Assume that a program is used to calculate the number of days of a month in a certain year.
The common practice is as follows:

Private void btncalculate_click (Object sender, eventargs E)
{
// Check whether the input is correct
If (! Checkinput () return;

Int days = 0;

Int month = convert. toint16 (txbmoth. Text );

Switch (month)
{
Case 1:
Days = 31;
Break;
Case 2:
If (isleapyear (txbyear. Text ))
{
Days = 29;
}
Else
{
Days = 28;
}
Break;
Case 3:
Days = 31;
Break;
Case 4:
Days = 30;
... Case 11:
Days = 30;
Break;
Case 12:
Days = 31;
Break;
Default:
Break;
}
Txboutput. Text = days. tostring ();}

You may see a large number of switch and case statements. In fact, this is just a simple logic. If the business logic is complex
These if, else, switch, and case statements will surely appear in the dark. Let's take a look at the simple application of the table-driven method.
Private void btncalculate_click (Object sender, eventargs E)
{
If (! Checkinput () return;


Int [] daypermonth = new int [12] {31, 28, 31, 30, 31, 30, 31, 31, 30, 31 };
Int [] ldaypermonth = new int [12] {31, 29, 31, 30, 31, 30, 31, 31, 30, 31 };

Int days = 0;
Int Index = convert. toint16 (txbmoth. Text );

If (isleapyear (txbyear. Text ))
{
Days = ldaypermonth [Index];
}
Else
{
Days = daypermonth [Index];
}

Txboutput. Text = days. tostring ();


}

By comparing the two pieces of code, you will find that if you use the table-driven method, your code will be simpler and clearer.
3: Table-Driven Data Query Method

* Direct Access)
* Index access)
* Step access)
Sometimes address change query 4: Advantages of table-Driven Method
We have been emphasizing the advantages of the table-driven method. The following is a summary:

* Use it in an appropriate environment to make the code simple and clear.
* Easy to modify (easy to maintain) and more efficient.
* One of the advantages of the table-driven method is that it can largely eliminate if else and swith judgment in code.

This method is recommended in code Daquan.

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.