table-driven approach

1: What is the table-driven method.
     The table-driven method is a programming mode (Scheme), which finds information from the table without using logical statements (if and case). Its advantage is to eliminate if, else, and swith that appear everywhere in the code. Statements, make messy code concise and clear. For simple cases, a table-driven approach may simply make logic statements easier and more straightforward, but as logic becomes more complex, table-driven approaches become more attractive.
2: Example demonstration of table-driven method
    Suppose there is a program to calculate the number of days in a certain month in a certain year. The
    usual practice is as follows:

 

private void btnCalculate_Click(object sender, EventArgs e)
        {
            //Check if 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 that there will be a lot of switch and case statements here. In fact, this is just a simple logic. If the business logic is complex,
these if, else, switch, and case statements in the code will definitely appear overwhelming. Well, let's take a look at a 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, 30, 31};
            int[] ldayPerMonth = new int[12] { 31, 29, 31, 30, 31, 30, 31, 31, 30, 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();
          
         
        }

Comparing these two pieces of code, you will find that if you use the table-driven method, your code will be more concise and clear.
3: Table-driven method of querying data

    * Direct Access (Direct Access)
    * Index Access (Index Access)
    * Stair-Step Access (Stair-Step Access)
    Sometimes indexed queries are required

4: The advantages
     of the table-driven method The advantages of the table-driven method have been emphasized before. Let's summarize:

    * In the right environment, using it can make the code simple and clear.
    * It is easy to modify (easy to maintain) and more efficient.
    * One advantage of the table-driven method is that it can eliminate a lot of if else and swith judgments in the code.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326861089&siteId=291194637