Who is responsible for this problem in the program?

Source: Internet
Author: User

Some time ago, I encountered such a problem in the project.

I provide some functions, such as a (), B (), C ()...... Then others call different functions according to the actual situation. These functions have

In common, the call method is very similar.

The user may use these functions in this way.

If (condition = 1) ();

Else if (condition = 2) B ();

Else if (condition = 3) C ();

Else...

I cannot see any problems. However, the problem is that calling a function is not that simple. A complex example is provided.

The user may use

If (condition = 1)
{
Do {
If (condition2) break;
A ();
If (condition3 & condition4)
{
// Do some code...
}
} While (condition1)
}
Else
If (condition = 2)
{
Do {
If (condition2) break;
B ();
If (condition3 & condition4)
{
// Do some code...
}
} While (condition1)
}
Else...

The above is just to illustrate that the processing methods for each case are similar, but they are complicated, with so many conditions and operations.

Now that something goes wrong, the caller will always use these conditions. For example, if condition2 is set up, it should jump out of the loop but forget break.

Or there are other errors. In short, even if the call to a is correct, sometimes I forget to call B's section.Code.

The problems are constantly occurring.

Of course, some people have said that you obviously have repeated code. You just need to change it to parameterization.

You can change it to the following:

Void func (INT condition)
{
If (condition = 1) ();
Else
If (condition = 2) B ();
Else
...
}

Then the above loili code can be modified in this way.

Do {
If (condition2) break;
Func (condition );
If (condition3 & condition4)
{
// Do some code...
}
} While (condition1 ).

But who should provide the key func function? At the beginning, I only provided a, B, c...

You can write a func function by yourself, but I am wrong. the user does not do this, but simply follows the dumbest method. When I

It was late at the time of discovery,ProgramThis code is everywhere, so that I am miserable. Although a, B, c... can be provided separately

More flexible (in case the user has different use cases for the function), but so far there have been no special cases of use.

I also regret that I did not provide a parameterized method at the beginning.

 

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.