分类: C/C++
2008-07-10 11:15:47
Most library functions provide good models for implementing functions. Here are guidelines to follow.
Make use of return values to communicate information and to make error trapping easy for the calling program.
Do not exit from functions. Instead, return an error value to allow the calling program flexibility in handling the error.
Make functions general but usable. (Sometimes these are conflicting goals.)
Do not make unnecessary assumptions about sizes of buffers. (This is often hard to implement.)
When it is necessary to use limits, use standard system-defined limits rather than arbitrary constants.
Do not reinvent the wheel—use standard library functions when possible.
Do not modify input parameter values unless it makes sense to do so.
Do not use static variables or dynamic memory allocation if automatic allocation will do just as well.
Analyze all the calls to the malloc family to make sure the program frees the memory that was allocated.
Consider whether a function is ever called recursively or from a signal handler or from a thread. Functions with variables of static storage class may not behave in the desired way. (The error number can cause a big problem here.)
Analyze the consequences of interruptions by signals.
Carefully consider how the entire program terminates.