Page 154 - Programming Microcontrollers in C
P. 154
Coding Tips for Microcontrollers 139
files is through the type FILE. FILE in this case is simply a type
that has been made available to the program through a typedef
statement of a structure that contains all of the essential elements
needed for file input/output. It is recommended that types created
by typedef statements involving a structure should have names
with the first letter capitalized. The names of these types should be
brief and should be one word only.
4. The C macro gives us the ability to create functions that will be
compiled in-line. This is very useful, but remember there is no
type checking for macro defined functions and you do not know
what will be returned from a macro function. This does not mean
to avoid macro functions, but it does mean that with no type check
ing we have a part of the program that does not get the normal
scrutiny we expect with most function calls. Be careful when us
ing macros—you, not the compiler, must make certain that the
argument types and the return type are all correct.
5. Use as few global variables as possible. Global variables seem to
be an easy way to avoid using function arguments, but they also
can make debugging extremely difficult. Parameters should be
passed to functions as arguments. This approach will give the
programmer some assurance that compile time checking will catch
any typing errors, and will eliminate the problems of “side ef
fects” found with global variables.
6. Be consistent. When designing function calls, make the order of
the parameters in the argument list logical and be consistent
throughout the whole program.
7. Avoid complicated argument lists. If it becomes necessary to send
many variables to a function, create a structure that contains all of the
arguments and send a pointer to the argument structure as a param
eter rather than the arguments themselves. Since the members of the
structure are all mnemonics with—hopefully!—meaningful names,
the filling of the structure should result in a more accurate creation of
the parameter list than would be found if the parameters are all listed
in the function argument.
8. Be courteous—document your code. There is no need for a com
ment for every line of code, but it is unconscionable that a program
should go page after page with no comments. Every function