A point of discussion on program design style

xiaoxiao2021-03-06  149

A little discussion on the program style.

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: Reasonably utilize blank characters, making the code style simple and clear. Like the following code, it is really costful;

#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, friends who are used to writing code under IDE 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;

}

However, it seems to be a bit chaotic, especially the expression of the expression, if you are a Telnet environment, I can't say that it is a night dream for the eyes. 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;

}

Ha ha. 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 easily understood the function,

#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] [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. The program is complicated, we will not understand this 2 is what, so we have to use macro to replace it. After amended, write these code into a single file as shown below (again, single file is not a good choice, here is just for Display convenience):

#include "stdio.h"

#include "stdlib.h"

#define size_1 2

#DEFINE SIZE_2 1

Const Int * Fun1 (INT * P)

{

INT I, J;

For (i = 0; i

For (j = 0; j

Scanf ("% d", (p j) i * 2);

Return P;

}

Void Fun2 (const INT * P)

{

INT I, J;

For (i = 0; i

For (j = 0; j

Printf ("% d", * ((p j) i * 2));

}

int main ()

{

INT Array [SIZE_1] [SIZE_2];

FUN2 (Fun1 (& Array [0]));

System ("pause");

Return 0;

}

The length of each dimension is indicated by SIZE_N, so that we no longer explain 2, 1 in the loop. Fourth: Comment. For some statements that are difficult to understand, we can use annotation to assist in understanding, but individuals think that the comments can't be written, on the one hand, you have to trust your user's ability, they are better than the author. As long as we feel slightly implied in variables and functions, they will pay attention to what you mean, but it is not clear about some of themselves, it is estimated that you may cause trouble in the future, you must add your mean.

For example, for this code. I don't dare to affirm that the traversal of the pointer is correct, then I have to note.

Printf ("% D", * ((p j) i * 2)); // traverse the 2D array with a one-dimensional pointer.

Finally, I still have to say that something written here is only some of the experiences in the past two years, not suitable, don't adopt, you can read a masterpiece "program design practice." It is better than I say that these must be better.

This article is the second bookstore

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

New Post(0)