Programming and cultivation (5)

zhaozj2021-02-08  204

16, form the same or nearly the same code to form a function and macro -------------------

Some people say that the best programmers are the most like "lazy" procedures, which are not reasonable.

If you have some program's code snippet, or just the same, put them in a function. And if this code is not much, and it will be used frequently, you still want to avoid the overhead of the function call, then write him into a macro.

Don't let the same code or functional code exist in multiple places, or if the function changes, you will modify several places, this will bring huge trouble to maintenance, so " Change hundreds of changes, or form a function or macro.

17. Brand in the expression ---------

If you are a more complex expression, you are not very clear, even if you are very clear, please add parentheses, otherwise, others or yourself next time, no Be careful, look at it, understand, in order to avoid this "misunderstanding", and make your own procedure more clear, or plus parentheses.

For example, take the address for a member of a structure:

GetUserage (& (UserInfo-> AGE);

Although, & userinfo-> age, -> The priority of the operator is the highest, add a bracket, it will make people understand what your code means.

For example, a long condition judgment:

IF ((CH [0]> = '0' || CH [0] <= '9') && (CH [1]> = 'a' || CH [1] <= 'Z') && (ch [2]> = 'a' || CH [2] <= 'Z')) Braces, plus spaces and wraps, your code is not easy to read?

18. Const in the function parameters -----------

For some pointer parameters in some functions, if read-only in the function, use const to modify it, so that others will know when you read your function interface, you will know that your intent is [in], if not When constant, the parameter represents [IN / OUT], pay attention to the Const use in the function interface, which is conducive to the maintenance of the program and avoids some errors.

Although const char * p, const char * p, there is no, because your statement is not constant, the content of the pointer can be changed, because the compiler will force the conversion, but plus such a Description, facilitating the reading and compilation of the program. Because in C, a Warning will be reported when modifying the memory pointed to by a const pointer. This will cause the programmer's attention.

C is very strict, so much more use of C , using const, constant member functions, constant variables, which will make your code and your program more complete and easy to read. (I will not say more about C constant)

19. The number of parameters (more use structure) -----------------

The number of functional parameters is best not too much. Generally, there are about 6 left and right, and many function parameters will make the people who read the code look very dizziness, and it is not conducive to maintenance. If there are many parameters, please use the structure to deliver parameters. This makes it easy to clean the data package and the simplicity of the program. It is also beneficial to use the function, because if your function is a lot, such as 12, the caller is easy to make the order and number of parameters, while using the structural struct to transfer parameters, you can regardless of the parameters.

Moreover, the function is easily modified. If you need to add parameters to the function, you don't need to change the function interface, just change the internal processing of the structure and function, and this action is transparent to the program of the call function.

20, the return type of the function, do not omit --------------

When I see a lot of program write functions, I don't pay attention to the return type of the function. If a function does not return a value, please add Void's modification in front of the function. And some programmers are lazy, and what is not modified in the return of Int (because if not modified, the default returns int), this habit is very bad, or for the eEl of the original code, plus int.

Therefore, the return value type of the function is not omitted.

In addition, for Void's functions, we will often forget return, because some C / C compilers are more sensitive, will report some warnings, so even void's function, we should best add Return's statement internally. This contributes to the compilation of the code.

21. Use of GOTO statement ---------

N years ago, a generation of software developed - Dijkstra said: "Goto Statment is Harmful !!" and it is recommended to cancel the goto statement. Because the goto statement is not conducive to the maintenance of the program code.

Here I also strongly recommend not using the goto statement unless the following situation:

#define free (p) i (p) {/ free (p); / p = null; /}

Main () {char * fname = null, * lname = null, * mname = null

FNAME = (char *) Calloc (20, sizeof (char)); if (fname == null) {goto errhandle;}

LName = (char *) Calloc (20, sizeof (char)); if (lname == null) {goto errhandle;}

MNAME = (char *) Calloc (20, sizeof (char)); if (mname == null) {goto errhandle;} ... errhandle: free (FREE); Free (MNAME) ; Reporterror (err_no_memoey);}

Only in this case, the GOTO statement will make your program easier to read and easier to maintain. (When you set a cursor operation with the in-CER C, you will also encounter this structure when the database is established, or the use of the macro --------

Many programmers don't know what it means to "macro" in C? Especially when the macro has parameters, it often confuses the macros and functions. I want to talk about "macro" here, the macro is only a definition. He defines a statement block. When the program is compiled, the compiler first wants to perform a "replace" source of the source and put the macro. Replace the macro defined statement, just like text files. This action term is called "macro"

Using macro is a "danger" because you don't know what the macro will look like. For example, the macro below:

#define max (a, b) a> b? A: B

When we use macros like this, there is no problem: max (num1, num 2); because the macro is turned into Num1> Num2? Num1: Num2; However, if it is called, MAX (17 32, 25 21); , Wow, what is this?

Therefore, when the macro is in use, the parameters must be added with parentheses, and the example described above is changed to solve the problem as follows.

#define max ((a), (b)) (a)> (b)? (a) :( b) Even so, don't this macro still have bug, because if I call MAX (i , J ) After this macro, I and J were accumulated twice, this is never what we want. So, in the use of macro, you should also be careful, because the result of the macro show is difficult to make people expect. And though, the macro's execution is quickly (because there is no overhead of the function call), but the macro will rise to the source code, so that the target file size is large, (such as: a 50-row macro, 1000 places in the program) After the macro show will be very good after the opening), the reverse cannot make the program to perform faster (because the execution file is getting bigger, the runtime system is frequently changed).

Therefore, it is necessary to use a function when deciding is a function.

<- Previous Next->

(All rights reserved, please indicate the source and author information when reproduced)

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

New Post(0)