Dry: Don't repeat yourselfIs an important principle for writing high-quality code. The meaning is not to write the same code around. In short, it is a dry ).
Some beginners like to paste code with the same or similar functions around, which is a typical self-repeat behavior. A major danger of writing code in this way is that once you need to modify the corresponding code, you have to go around and modify every code.
Even so, you may still feel uneasy, because you are likely to miss it. Even if you are sure that you have made all the modifications without any omissions, the most important thing you have done is that everything has been correctly modified. According to the probability analysis, if you are sure to make a correct modification, the possibility of making ten correct modifications without errors will be reduced to 99%.
To put it back, you can ensure that all the modifications are correct, and the effort required for the modifications is amazing (this is not obvious when the program is very small, therefore, it is difficult for beginners to pay attention to it. The so-called "don't take a cut, don't grow wise"), you are almost unlikely to have the energy to focus on more important things in the programming process-such as improving algorithms.
Even if you do not have the above drawbacks, your code will eventually be bloated and ugly.
Therefore, any qualified programmer knows how to abstract code segments with the same function into a function. Make a simple call only once where these functions are required. This avoids the drawbacks mentioned above.
Next we will talk about another method that is not "dry", that is, "wet". This method is clumsy when the same statement is repeated multiple times in the function.
Int main (void)
{
Int N;
Printf ("Please input the value of N :");
Scanf ("% d", & N );
While (n <= 0)
{
Printf ("error! ");
Printf ("Please input the value of N :");
Scanf ("% d", & N );
}
/* Other statements */
}
This Code clearly uses the standard input device to input N values. If the input is not a decimal positive integer, the system prompts the user and asks the user to re-enter the value.
However, it is obvious that in this code segment
Printf ("Please input the value of N :");
Scanf ("% d", & N );
The two statements are repeated.
For some people, they don't have any feelings about this. For others, they will feel awkward and unnatural just as they see repeated long-winded sentences in their compositions. This is a matter of realm. Some of the people in the latter do know how to modify the changes, and some do not know how to modify the changes. This article is for those who have feelings but do not know how to modify it. I don't have a prescription for treating numbness and feeling lost.
The preceding code can be rewritten:
while( printf("please input the value of n: ") , scanf("%d",&n) , n<=0 ) printf("error!");
This writing method is much simpler than the previous one. The following describes the meaning of this writing method.
We know that the general form of the while statement in C language is
While (expression)
Statement
The "expression" here can be a scalar type. In the second write method, the expression after the while keyword () is a comma expression, and its value type is the type of the subexpression "n <= 0, n <= 0 "is undoubtedly a scalar type, so the second method meets the syntax requirements of C language.
The evaluation process of this comma expression is: (1) evaluate the value of printf ("Please input the value of N, however, we do not need or use this value here. In the process of evaluation, side effect is involved. The "Please input the valueof N:" Character Sequence is output on the standard output device, this is what we need. (2) evaluate the value of the function call expression scanf ("% d", & N). We do not need this value. What we need is the sub-effect of the function call expression scanf ("% d", & n). The most important sub-effect is that variable n gets a new value. (3) then, because Variable N has a meaningful value through the input, the value of the subexpression "n <= 0" can be obtained, this value is required for executing the while statement. This value is also one of the necessary conditions for the second write method and the first write method to be equivalent. Another necessary condition is printf ("Please input the value of N:"), scanf ("% d", & N), and printf ("error! ") The order of the sub-effects of these function calls is the same. This order is also guaranteed due to the comma expression calculation rules for the order of value.
Other languages with poor expressiveness (such as basic) may have to use the first method to write code. It is because of the limitations of the language itself that there is no way to do it. However, for the C language, it is more elegant and elegant to complete the same function. This is mainly because of the powerful expressions in C language.
This method also works for higher functional requirements due to the powerful c expression. For example, if you are prompted to re-enter a positive number or other illegal input (such as a non-decimal character), the code can be written as follows:
While (printf ("Please input the value of N:"), scanf ("% d", & n) = 0 | n <= 0) {// some necessary processing}
When the input does not meet the % d format requirements, scanf () ends the call and the return value is 0. Here, "," and "&" jointly ensure the necessary order.
As for which of the first and second statements is better, leave this question to yourself. Any value judgment may inevitably have some subjective colors. What's more, according to my experience, once you propose that you should use your rice bowl for dinner, there will certainly be a lot of people who use the sputum to eat, and it is also acceptable to argue with you that using the sputum for dinner.
If someone is fond of and insists on eating with a slogan, it's off my ass!