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.