functional documentation
Sunday, November 16th, 2008
Software documentation and comments are highly variable in quality and usefulness. At the useless end of the spectrum are the detailed source comments which tell you what the next line of code is going to do. As the next line of code tells us exactly the same thing, this is at best pointless; at worst, when the comment does not accurately describe what the next line of code is going to do, it can be seriously misleading; here’s an example of what I’m describing:
#increase each object's value by 10
for (sort keys %objects){
$_ += 10;
}
Perl programmers will notice that the code does not increase the object values, but rather the keys of the %objects hash, so the comment is not only redundant but also plain wrong.
On the other hand, very high-level, abstract documentation which tells us what a program or function library does conceptually is only useful in a very high-level, abstract sort of way.
One type of documentation which is much more successful is the “Javadocs” style of functional documentation, which describes each function (or subroutine or procedure) principally in terms of its inputs and outputs. “Javadocs” is a documentation methodology maintained by Sun Microsystems, originally intended for documenting Java functions, but easily adapted to almost any other language. Here’s an example of the use of Javadocs on a PHP function:
/** * Returns the capability for a given capability name.
*
* @author Modulus Pty. Ltd. - prh
* @version 2008 1.0
* @param $id unique string id of the device
* @param $name string name of the capability
* @param $fallback boolean for considering fallback
* @param $fallbackChain array of strings, where known, provide this to avoid unnecessary repetitive lookups
* @return string capability
*/
function lib_getCapability($id, $name, $fallback, $fallbackChain) {
...
}
This documentation is immediately useful in the source code for developers maintaining or altering the source code. Furthermore, the effort required to create and maintain the documentation is quite limited in relation to the benefit derived. However, you may wish to publish an API to your functional library without publishing the source code. One way to do this is to use our javadoc.module,
which, for a modest $19.95, creates elegant, valid and conformant XHTML documentation from Perl, PHP, Javascript or Java source code. Here’s an example of the output generated:
function lib_getCapability($id, $name, $fallback, $fallbackChain)
Returns the capability for a given capability name.
author
Modulus Pty. Ltd. – prh
version
2008 1.0
param
$id unique string id of the device
$name string name of the capability
$fallback boolean for considering fallback
$fallbackChain array of strings, where known, provide this to avoid unnecessary repetitive lookups
return
string capability