Programming and cultivation (7)

zhaozj2021-02-08  279

28, || and &&'s statement execution order ------------------------ "and" or "operators must be careful, their performance may be like you Different, some behaviors in the conditional statements need to talk:

Express1 || Express2 First Perform Expression Express1 If "true", Express2 will not be executed, Express2 is only executed when Express1 is "false". Because the first expression is true, the entire expression is true, so it is not necessary to perform the second expression.

Express1 && Express2

First execute expression express1 If "false", Express2 will not be executed, and Express2 is only executed when Express1 is "true". Because the first expression is false, the entire expression is fake, so there is no need to perform the second expression.

So, he is not all the expressions you imagine will be executed, this must be understood, otherwise your program will have some Ming's runtime error.

For example, the following program:

IF (SUM> 100 && ((fp = fopen (filename, "a"))))! = null) {fPrintf (fp, "warring: it beyond one hundred / n"); .....} fprintf (FP , "SUM IS% ID / N", SUM); Fclose (FP);

Original intentions, if SUM> 100, write an error message in the file, for convenience, write the two conditions to be judged together, so if SUM <= 100, the operation of opening the file will not be done, and finally, FPRINTF and FCLOSE will find unknown results.

For example, if I want to judge whether a character is content, I have to judge that the string pointer is empty (NULL) and its content cannot be empty (EMPTY), one is an empty pointer, one is empty content. I may write this:

IF ((p! = null) && (Strlen (P)! = 0))))

Thus, if p is null, Strlen (P) will not be executed, so Strlen does not "illegally operate" or "core dump" because of an empty pointer.

Remember, in a conditional statement, not all statements will be executed, as your conditional statement is very much, this should be careful.

29, try to make a loop with for instead of the While --------------- Basically, for can complete the While function, I recommend trying to use the for statement, not to use the While statement, Especially when the cycle is large, the advantage is reflected in it.

Because in For, the initial, end condition, and looping of the loop are together, and it looks at what kind of loop. The procedure for the school is generally like this to you:

P = PHEAD; while (p) {... p = p-> next;}

When the whiling statement blocks, your program will be difficult to read, use for more: for (p = phead; p; p = p-> next) {..

At a glance, you know the beginning of this cycle, end conditions, and progress of the loop. Can you understand what this loop does? Moreover, program maintenance comes easy, don't have to be like While, the tricks up and down in an editor.

30, please sizeof type instead of variable -------------

Many programmers are using SizeOf, likes a SIZEOF variable name, for example:

Int score [100]; char filename [20]; struct usrinfo usr [100];

When the three variable names of SizeOf, the correct result is returned, so many programmers start the SIZEOF variable name. This habit is very good, but I still recommend a SIZEOF type.

I have seen this program:

Pscore = (int *) malloc (pscore, 0, sizeof (pscore)); ... At this point, SizeOf (PSCORE) is 4 (length of the pointer), which will not be the entire array, so MemSet cannot initialize this memory. For the easy-to-read and easy maintenance of the program, I strongly recommend the type instead of variables, such as:

For Score: Sizeof (int) * 100 / * 100 int * / for filename: sizeof (char) * 20 / * 20 char * / for usr: sizeof (struct userinfo) * 100 / * 100 USERINFO * /

Is this code easy to read? I don't think it's mean at a glance.

Another point, SIZEOF is generally used to distribute memory, and this feature can reflect its advantages in a multi-dimensional array. For example, assign memory to a string array.

/ * * Assign a string with 20 strings, * Memory of each string length 100 * /

Char * * p;

/ * * Error allocation method * / p = (char **) Calloc (20 * 100, sizeof (char));

/ * * Correct allocation method * / p = (char **) Calloc (20, sizeof (char *)); for (i = 0; i <20; i ) {/ * p = (char *) Calloc 100, sizeof (char)); * / p [i] = (char *) Calloc (100, sizeof (char));}

(Note: The above statement is commented by the original, wrong, by dasherest friends, thank you)

For the easy reading of the code, save some judgments, please pay attention to the methods of these two allocations, there is an essential difference.

31, don't ignore WARNING ---------- For some warning information, please don't ignore them. Although these Warning does not hinder the generation of target code, this does not mean that your program is good. By, it is not the process of compiling success. It is correct. The success of the compilation is just the first step in the Long March, and there is a big wind and big waves waiting for you. Starting from the compiler, not only correct each Error, and also correct each Warning. This is a cultivated programmer. Generally, some warning information is common:

1) Declare unused variables. (Although the compiler does not compile this variable, it is still an annotated or deleted it from the source program) 2) Use the firing declaration. (Perhaps this function is in other C files, this warning occurs when compiling, you should use the extern keyword to declare this function) 3) No to convert a pointer. For example, the pointer returned by Malloc is Void. You didn't turn it into your actual type and alarm, or manually in the previously converted it) 4) Type down. (For example: float f = 2.0; this statement is the warned, compile will tell you that you are trying to turn a double to float, you are casting a variable, do you really do this? Or add one after 2.0 f, otherwise, 2.0 is a double, not FLOAT), no matter what to say, the compiler's Warning should not underestimate, it is best not to ignore, a program is doing, let alone a few small Warning?

32. Writing the debug version and the release version of the program ---------------- The program must have many programmers plus debugging information during the development process. I have seen many project groups. When the program is ended, the commissioning information in the program is launched, why? Why not build two versions of target code like VC ? One is a debug version, one is release. Those debugging information is so precious, and it is also very valuable during the future maintenance process. How can I say deletion?

Use the pre-compilation technology, as shown below, the debug function is declared:

#ifdef debug void trace (char * fmt, ...) {... #else #define trace (char * fmt, ...) #ENDIF

So, let all the programs use trace to output debugging information, only need to add a parameter "-ddebug" when compiling, such as:

cc -ddebug -o target target.c

Thus, the Precirator finds the Debug variable is defined, and the Trace function will be used. And if you want to release it to the user, just need to cancel the "-ddebug" parameters, so all TRACE macros, this macro does not, so all Trace languages ​​in the source program are replaced by empty. Two fell swoop, one arrow double carved, why not?

By the way, two very useful system macros, one is "__file__", one is "__line__", indicate that the source file and line number, when you debug information or output error, you can use these two Macro, let you see your mistake, which file appears in the first few lines. This is very tube for large works made with C / C . In summary of 32, it is for three major purposes -

1, the readability of the program code. 2. Maintainability of program code, 3. Stability and reliability of program code. If there is a cultivated programmer, you should learn to write this code! This is any kind of problem that wants to face the programming master, the programming master is not only strong, the foundation is good, and the most important thing is to have "cultivation"!

Good software products are not just technologies, but more is the easier maintenance and reliability of the entire software.

Software maintenance has a lot of workload flowers on the maintenance of code, the software's Upgrade, there is a lot of work spending on the organization of the code, so good code, clear, easy-to-read code, will give great reduction software Maintenance and upgrade costs.

<- Previous Page

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

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

New Post(0)