You should employ helper tools (such as lint) as much as possible to enhance your code. A further (however not automated) tool can be seen in code reviews by humans.
Lint
lint is a traditional Unix development tool, originally designed for K&R1.
It allows
• to catch some of K&R1 pitfalls (insufficient type checking, missing declarations)
• to check inter-module issues (inconsistent interface declarations, e.g. char p[] vs. extern char* p)
• to find globally unused functions and variables
• to find dead (unreachable) code (which is usually an error)
• to check for false or missing arguments to printf() or scanf()
• to check static array's bounds
• to catch inconsistent function use (e.g. ignoring return values sometimes)
Metrics
Metrics are a means to quantify some code characteristics. These quantifications can be used to achieve a desired level of commenting or modularization, etc.
Simple metrics count e.g. the
• number of source code lines
• number of lines of code (LOC, leaving out empty lines and comments)
• number of comments18
• number of statements
• ratio of comments per statement
One can do statistics on the distribution of these values over modules or projects, or check if the values are in certain ranges.
Advanced metrics can count function points, express code complexity (e.g. statements per function, nesting, number of cross-references, etc.).
Even more advanced metrics may try to track changes in micro architecture19, changes in interfaces and the like, as the project development goes on. This may be accomplished together with configuration management tools.
Assertions
An alternative to runtime checker applications are Assertions. They allow to check preconditions and postconditions of functions (and other constructs) at runtime.
Assertions can be used to check the internal program logic and a correct program flow.
Drawbacks are:
• They're expensive to code.
• Their use might not be systematic (because manually created).
• The code may get harder to read.
Release versions typically do not contain the assertions. Beta versions may or may not contain the assertions.
Runtime checkers
Applications are available that instrumentate code with runtime checks (for array boundaries, dynamic data boundaries, missing memory deallocation, function/system calls with bad arguments, etc.).
The use of such tools may be time consuming since the program may run factors slower. The tool may not be able to distinguish between e.g. memory leaks (bad) and reusable buffers that won't be freed (a valid design decision).
Samples are: purify, electric fence (dynamic memory checks only).
These applications may be used in automated testing suite.
Friday, January 9, 2009
Helper tools
Posted by abhilash at 8:53 AM
Subscribe to:
Post Comments (Atom)
0 Comments:
Post a Comment