A little discussion on the program style.

xiaoxiao2021-03-06  123

So far, we have understood the basic elements of the C program. Before entering the process of procedural design, I personally think that the coding habit is a good start. Regarding the problem of programming style, it is a discussion of no answer. As the coding experience has increased, we will have different awareness, different starting points, this article is just two years. Code Experience, I do need a courage (after all, I don't have this eligibility in the eyes of many people), just the purpose of discussion, there is no mistake of my child. The code written by a good programmer not only wants the machine to run and achieve the result, but let anyone understand the C language, what does this code mean, not a lot of annotations and documents Come to help him understand what you mean (of course, as auxiliary or must be). The following is some of the individual experience: first: reasonable use of blank characters, make the code style is simple and clear. Like the following code, it is really cost-effective; #include "stdio.h" #include "stdlib.h" int main () {Int a [2] [1], i, j; for (i = 0 i <2; i) for (j = 0; j <1; J) scanf ("% d", & a [i] [j]); for (i = 0; i <2; i) for (j = 0; j <1; j) Printf ("% d", a [i] [j]); system ("pause"); return 0;} Of course, habit is under Ide Friends who write code can be automatically indented by the environment (often not so comfortable in non-Windows Environments, have to manually indent), let the things above are a little better: #include "stdio.h" #include "stdlib .h "int main () {Int a [2] [1], i, j; for (i = 0; i <2; i) for (j = 0; j <1; J) scanf ("% d", & a [i] [j]); for (i = 0; i <2; i) for) j = 0; j <1; J) Printf ("% d", A [i] [j]); system ("pause"); return 0;}, but it seems to be a bit chaotic, especially the expression of the expression, if you are the BBS in the Telnet environment to see this code for your eyes It is not possible to say that it is a nightmare. So we add a space in the operator and parentheses. And separate variables of different meanings, even if they belong to the same type. #include "stdio.h" #include "stdlib.h" int main () {Int a [2] [1]; INT I, J; for (i = 0; i <2; i) for (J = 0; J <1; J) scanf ("% d", & a [i] [j]); for (i = 0; i <2; i) for (j = 0; J <1 J) Printf ("% D", A [i] [j]); system ("pause"); return 0;} Oh. Don't look only for a few spaces.

When your code is long, it can help you have patient reading, if you look at the symbol letter, who is interested in seeing? Second: Try to decompose the problem, multiflorse function, simplify the main function. For example, the code above can be written as the following, you will feel more easier to understand the function of the code, # include "stdio.h" #include "stdlib.h" const INT * scan (int * p) {Int i, j; for, (i = 0; i <2; i) for (j = 0; j <1; J) scanf ("% d", (p j) i * 2); return p;} void Print (const INT * p) {INT I, J; For (i = 0; i <2; i) for (j = 0; j <1; J) Printf ("% d", * ( (P J) I * 2);} int main () {Int a [2] [1]; Print (SCAN (& A [0])); system ("pause"); return 0 ;}of course. For implementation details, it is more complicated. But let's see what the main function knows what this is doing .. For our users, you need to know that only the interface, the details of the function are usually hidden. Therefore, the above code is written in the same file. This problem is related to design patterns. There is no longer more here. Friends can take a look at "Design Mode". Third: Try to use meaningful symbol names, less useless symbols and magic numbers. In the above example, the name of the function makes us a look at it, but this code is still a bit of trouble, everyone knows that the array of crosses is a mistake, so we use it to measure it. But 2 this number means very unclear, time is long.

转载请注明原文地址:https://www.9cbs.com/read-126214.html

New Post(0)