Revision [14745]

This is an old revision of DocumentingCodeHowto made by DarTar on 2006-06-30 11:57:09.

 

WikkaHowto Wikka "Howto" Pages

Code Documentation Tutorial


 

Code documentation

Documentation is essential! Even well-written code is not necessarily self-documenting; there is always some decision-making involved, for instance, in why you coded something the way you did, why a particular algorithm is chosen, etc. Such "why" decisions should be documented. Also, pieces of code that need to be used by others, such as classes and functions (and in Wikka this includes actions and handlers) need to have their interface documented so others can make use of the functionality without needing to read all the code first.

In fact, when starting to write something new, it's a good idea to start with the documentation - and then work that out into code.

It's important that such documentation "travels with" the code, instead of having it externally. That's where comments come in, and specific guidelines for how to write comments for multiple purposes. A tool like phpDocumentor can help to make a user-friendly and cross-referenced documentation for others to use by parsing the code looking for structures like classes and functions and for structured comments, and outputting fully-crossreferenced HTML documentation.

phpDoc docblocks

This section uses materials adapted from the phpDoc manual
Self-documenting code can be created by adding phpDoc docblocks. Every file, class, function and - where needed - constant or variable should be introduced by a docblock.

A docblock is an extended C++-style PHP comment that begins with /** and has an * at the beginning of every line. Docblocks always precede the element they are documenting.

/**
 * The most basic Wikka function.
 */

function foo()
{
}

A docblock contains three basic segments in this order:
The Short Description starts on the first line, and can be terminated with a blank line or a period. A period inside a word is ignored. If the Short Description becomes more than three lines long, only the first line is taken. The Long Description continues for as many lines as desired and may contain html markup for display formatting. Here is a sample docblock with a Short and a Long Description:

/**
 * Display information about the system Wikka is running on.
 *
 * Depending on the 'show' parameter, this action displays different types of system information.
 * By default it only displays this information to Wikka Admins, this option can be changed by
 * setting 'public_sysinfo' to '1' in the Wikka configuration file.
 *
 *  Syntax:
 *  {{system [show="OS|machine|host"]}}
 *
 */

You can optionally use a limited set of HTML elements in the Long Description if needed. Long Descriptions can also contain lists like in the following example:

/**
 * Simple DocBlock with simple lists
 * Here's a simple list:
 * - item 1
 * - item 2, this one
 *   is multi-line
 * - item 3
 * end of list.  Next list is ordered
 * 1 ordered item 1
 * 2 ordered item 2
 * end of list. This is also ordered:
 * 1. ordered item 1
 * 2. ordered item 2
 */


The last (and most important) part of a docblock contains tags that add information on the documented item. Tags are single words prefixed by a @ symbol.

Wikka uses a subset of all the possible tags supported by phpDoc (for the complete list of phpDoc tags check this page)

/**
 * A short description
 *
 * As many lines of extendend description as you want {@link element}
 * links to an element {@link http://www.example.com Example} links to
 * a website.
 *
 * @access       public or private
 * @author       Author Name <author@email>
   or
 * @author       {@link http://wikkawiki.org/UserPage author name} (further notes)  
 * @copyright    name date
 * @deprecated   description
 * @example      /path/to/example (include an external example file with syntax highlighting)
 * @global       type $globalvarname
   or
 * @global       type description of global variable usage in a function
 * @ignore
 * @internal     private information for advanced developers only
 * @link         URL
 * @name         Name of the handler/action/formatter, e.g. recentchanges
 * @package        package name, e.g. Handlers
 * @param        type [$varname] description
 * @return       type description
 * @see          name of another element that can be documented (displays an internal link to the documentation for that element)
 * @since        Wikka version number
 * @subpackage    sub package name, e.g. Page (for page handlers)
 * @todo         item for a todo lisy
 * @var        type    a data type for a class variable
 * @version    version (will use SVN $Id$)
 */

function foo()
{
// ...
}


(stub - file/class/function/constant/variable - refer to page with templates)

Single-line comments vs. comment blocks

(stub)

End-of-line comments

(stub)

Special markers

(stub - conventions for things like "todo" and "fixme" remarks)


CategoryHowto
There are 2 comments on this page. [Show comments]
Valid XHTML :: Valid CSS: :: Powered by WikkaWiki