Some people will think: As long as the program is running, there is no matter what the original program is compiled. But absolutely not the case. The software is not finished in one time, it is necessary to make changes, expansion and other management. Therefore, the original procedure should be as easy as possible to understand, and the management is convenient.
In doing so, the first is for software developers, followed by the software performance. Managing inconvenient programs will not make good software.
I hope to learn good program habits through this article. To understand the content of this article, you should know at least 1 development tool language. It is a C language for example, but you don't worry about the C language. The principle described here is not a specific language.
1. The statement is to end thoroughly --- (colon;)
One of the frequent challenges of programmers is to forget to add a colon after the statement is completed. Such problems are not easy to discover, and the programmer is not known when it is. When programming, pay attention to whether each statement ends with a colon, although not all languages ends with a colon. The following is an example of forgetting a colon.
INT main (void) {/ * does not have a colon, resulting in problems * / printf ("Hello World! / N") Return (0);}
Many people make such a mistake. Less than a few procedures are not difficult to find such problems, but more than 1,000 procedures? Find that the statement that I forgot to write a colon will not be very easy. Remember, end a statement, must write a colon, just like the end of the general article.
There is also a wrong mist of colon not to write a colon. Experienced programmers see that the following example will feel funny, but the author does have seen a lot of such mistakes.
/ * Main () should not write a colon * / int main (int Argc, char * argv []); {Printf ("Hello World"); Return (0);}
The function or Method is not written in the colon.
2. To properly use spaces and tab keys
The C language is not distinguished, so the program can also be written without a space, but such a program will not understand the "very difficult" program, please see the following example:
IF (x == 0) {a = b = c = D = max; x ;}
Write this, may save space, but not only others, it will be difficult to understand. The program is easy to understand!
IF (x == 0) {a = b = c = D = max; x ;}
Write like this, does it look clear? The program has to be exact spaces to understand.
3. Uniform use of braces and cutting methods
Each programmer has its own habits using braces ({}) and modified ways, so that when handing over to others, it will appear. For example, like the following example:
INT main () {int x = 1; int y = 10; while (x Some programmers will write braces like this: INT main () {int x = 1; int y = 10; while (x The author is a second way. Because the beginning and end of a statement is obvious. We cannot ask each programmer to program in a way, but a program must be unified. Also, when you see the procedures for others, you can think of others' programming. 4. Do not use the IF statement Some people like to use the "if" statement, as follows: if (a == 0) {a ; return (a);} IF (a == 1) {a = 5; return (a);} IF (a == 2) {a = 10; return (a);} IF (a == 3) {a = 20; return (a);} IF (a == 4) EXIT (1); Is there a better way than this? ELSE IF statement? Not. A good way is to write a simple program with the "Switch-Case" statement: Switch (a) {case 0: A ; Return (A); Case 1: A = 5; Return (A); Case 2: a = 10; Return (A); Case 3: A = 20; RETURN (A); DEFAULT: EXIT (1); If no value consistent with A, the job defined in default is executed, and the above example is to perform the end. 5. Discharge (Block) Many people often cut off. It is a program that is more difficult to understand using more than three cut off. Please see the example below: INT A = 10; int b = 20; int C = 30; int D = 40; IF (a == 10) {a = A D; if (b == 20) {b = b a; if (c! = b) {c = C 1; if (D> (A B) )) PRINTF ("Made It All the Way To The Bottom! / N");}}} This may be exaggerated, but there are still many people really do. How do you write better? One way is to use a function to write: Void next (int A, int b, int C, int d) {if (c! = b) {c = c 1; if (D> (a b)) Printf ("Made it all the way to the the Bottom! / n ");}} INT main () {Int a = 10; int b = 20; int C = 30; int D = 40; IF (a == 10) {a = A D; if (b == 20) {b = b a; Next (A, B, C, D);}} return (0);} To write this, you may increase the workload, but the program is structured, it is easy to understand, and if the function is better, you can use it elsewhere. 6. Write a note To develop a habit of writing a comment. Especially the procedures or variables that others are difficult to understand must be annotated. After a month, maybe you also need to see the comment. INT x = 100; int y = 1000; IF (x What do you know what the above program means? If you don't know what the variables x, y, and a refer to what is, it is difficult to understand. Let us give it a marked annotation, then look: / * * Program for detection of profit or loss * / INT x = 100; / * x is the total amount of the book * / int y = 1000 / * Y is the cost of doing books * / int A; / * Confirm that there is a profit * / / * Comparison x and y: * / if (x This note, people who don't understand C language will also understand what the statements mean, what each variable refers to. Write a comment is a good habit, but don't comment. The comment is for a so-understanding, not for writing a long-editing article. INT profit = 1; / * Profit equal to 1 ?? * / int LOSS = 0; / * Loss equal to 0 ?? * / / * If the profit is equal to 1 * / if (Profit == 1) / * Print "Print" to win "?? * / printf (" We Made a Profit! / N "); / * If not * / else / * Print "We Loss" * / Printf ("We Made A Loss! / N"); This comment is a waste of time. Generally, a quarry is required, and the comment is required, and the purpose of the program, the use of a function, Procedule, etc. also needs. 7. Being reasonable Programs, variables, procedure, and Structure, etc. must be linked to its content, and the variables do not name "x", "y", "z" Maybe you will say that the author uses X, Y, etc., the reason why the pen is doing this is that when he does not involve other programs, the author will certainly use meaningful variable names. Please see the example below: Void x (int A, int b) {int z; z = a b; Printf ("Z IS% D / N", Z);} Here, we can know what X does, but I don't know what it means. A, B, Z is also the same. Let us change it again and then look: Void SUM_OF_AGES (INT JACKS_AGE, INT JILLS_AGE) {INT TOTAL_AGE; TOTAL_AGE = Jacks_age Jills_age; Print ("Total_age IS% D / N", TOTAL_AGE);} Although there is no comment, it is easy to see what to do. Good name, no comments can be. 8. Confirm the buffer To confirm the size of the set alignment or variable to avoid data chaos or cause the system. Please see the example of the user input data: char city [10]; / * is the arrangement of the city name * / Printf ("Enter a city name:"); scanf ("% s", city); Printf ("City IS% S / N", City; Here, the urban name is set to 10 words (English characters). What if the user enters the urban name of 10 words or more? It can be said that the program fails or covers the data in the buffer. Anyway, don't do an adventure, you have to check the length of the text: Char city [10]; / * is the arrangement of urban name * / Printf ("Enter a city name:"); FGETS (City, SizeOf (City), Stdin; Printf ("City IS% S / N", City; The above has been modified is: If the user enters the urban name of 10 words, only the length of the buffer can accept, and it will not be accepted. 9. Never believe users This is very important. Don't believe that people who will use your software, don't think that users will operate according to your hopes, but they want them to discover programs. For example, the above program is to confirm the length of the text for those users who enter the urban name. As the C language requires a clear data form, it is necessary to remember to confirm whether the input data form and the program data form are consistent, or there will be problems. Whele is incorrectly chopping The above program habits are for your own convenience. Before actually programming, prepare more (analysis, design). Although it is slightly trouble, but in order to compose a neat program, don't give it time to prepare, otherwise you will need more time to modify, expand your program.