Programming Style
In this section, I'd like to briefly highlight some points that will help you write clean programs that can easily be read, understood, and maintained.
First, make sure the variable or function names in your program describe the meanings of the variables or tasks of the functions precisely and concisely.
Put comments into your code so that you or the other readers can have clues as to what your code is doing, or at least what the code intends to do, but might do incorrectly.
Whenever possible, keep using local variables, not global variables. Try to avoid sharing global data among functions; instead, pass the shared data as arguments to functions.
You should be careful when using C operators, especially the assignment operator (=) and the conditional operator (==), because misuse of these two operators can lead to an unexpected result and make the debugging very difficult.
Avoid using the goto statement; instead, use other control flow statements whenever needed.
Use named constants in your program, instead of numeric constants, because named constants can make your program more readable, and you will have to go to only one place to update the values of constants.
You should put parentheses around each constant expression or argument defined by a preprocessor directive to avoid side effects.
Also, you should set up a reasonable rule for spacing and indentation so that you can follow the rule consistently in all the programs you write. The rule should help make your programs easy to read.
Programming Style
Related Items
The continue Statement
The continue Statement
The break Statement
The break Statement
You can add a break statement at the end of the statement list following every case label, if you want to exit the switch construct after the statements within a selected case are executed.
The switch Statement
The switch Statement
Nested if Statements
Nested if Statements
As you saw in the previous sections, one if statement enables a program to make one decision. In many cases, a program has to make a series of decisions. To enable it to do so, you can use nested if statements.
The if-else Statement
The if-else Statement