Chat: Coding and Specification Code in C Language (Part)

xiaoxiao2021-03-06  34

The coding habit and coding specification occupies an important role in programming. Good coding habits are conducive to reducing the error rate of the code, but also facilitating the debugging of the code, the specification coding enhances the readability of the code, and the communication of the code is possible. This article will detail the method of encoding, the specification code is left in the following story.

The basic idea of ​​coding is extremely simple, that is, "pair". The phenomenon is extremely common in C language, any function, its function body is enclosed in paired "{}". From this point we can give the basic method of the encoding, the following to write a "Hello World" program to describe the method as an example.

The first step in encoding: int main (void) {} ​​Take a "{" to play a "}" immediately after entering!

The second step of encoding: int main (void) {Printf ("Hello World!"); Getch (); return 0;} Move the cursor to the last row, then press the Tab button, then you can Insert the code between two parentheses.

It should be noted that we give a pair of brackets in the first step. We have gave a "{" to give a "}" without hesitation, which is pairing coding. In the second step, we move the cursor between two braces and insert the code in the middle. This is extremely important, we thoroughly abandon backward linear encoding (ie, a character of a character is written), but a nonlinear code of the jump. This method is very wonderful, in any step of the code, we can guarantee that the code can be compiled, can run, debugging. At least it can also guarantee that there will be less than one or more "}" compilation errors.

This basic method can also be applied to any statement using "{}", such as IF, for, etc., such as the following code:

Step 1: int Main () {Int i; for (i = 0; i <10; i ) {}}

Step 2: int Main () {Int i; for (i = 0; i <10; i ) {printf ("Pair of encoded is a good method! / N");}}

Step 3: int main () {Int i; for (i = 0; i <10; i ) {printf ("Pair of encoded is a good method! / N");} getCH (); return 0;}

Another promotion of the encoding is that Malloc () and free () can also be seen as pairs. In the code, if we write a malloc (); statement, we should write a free (); statement, the rest is inserting the code between the Lyon sentence, so that it will not be able to appear Memory leakage problem.

The pair of encoding can also be extended to any statement similar to Malloc () and Free (). For example, when using a linked list, I usually write both initList () and design, and then insert the operation of the linked list to two sentences. Everyone can experience the superiority of the encoding, relative to linear encoding, with a pair of code that has little code, and the code can be run at any time, which can be operated. I have used a pair of encoding methods in many programs prepared, and there is almost no compilation error, it is really cool.

Many of the C language materials only pay attention to the contents of the syntax, and the coding method is almost only. When we read the code, it is accustomed to reading from top to bottom, develop linear coding habits. As a highly efficient encoding method, we must study and master it. At the same time, you can use a method of reading and understanding the code and the city.

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

New Post(0)