Documentation Team/API Documentation

Definition
API (Application Programming Interface) documentation is documentation that applies to a program and its interfaces. It is documentation that explains to a developer who is going to work with the program, or a developer who is going to work on a program, how things work and what methods are there to call.

API documentation is sometimes called an API reference manual. It needn't just be a reference manual, though, there can be extensive additional material, like tutorials, histories, etc. In this page, we will refer to all the material that documents and explains the API of a piece of code as "apidox."

Good apidox take some effort to write -- but less effort than good user documentation, since you as a developer are writing for another developer, and explaining how code works and what it is supposed to do. So your audience is clear to start with. The benefits of apidox come when someone else (or maybe even you several months later) needs to (re)use the code, or extend it. Good apidox means that someone new can show up, understand your code, and produce meaningful patches (that is, help) all the more quickly. Especially for libraries, good apidox make the library usable as a black box (and that's how they should be) because all the needed documentation is available in the apidox and not hidden in the (perhaps inaccessible) code.

Preamble
APIDOX are what make a program accessible to other contributors. They are not essential, but they certainly help a great deal for new people who want to work on your code, or better yet, re-use it elsewhere with a minimum of modification.

Look at the KDE documentation to get a feeling of what good apidox look like. There is a consistency of style, a thoroughness which permeates all the documentation. It is possible to learn a lot about KDE just from reading the documentation. You do not necessarily need to run the tutorial programs or read the source code of the library to find out what a parameter flags does in some method of the library. It is all spelled out for you.

Writing apidox is a two-part process. One part is technical: you need to understand the code you are documenting -- or at least know what it is supposed to do and how it is meant to be used. The other part is plain discipline: apidox are most useful when they are exhaustive.

This document is going to try to prevent the apidox process from becoming exhausting, by giving straightforward tips about how to write apidox.

APIDOX Basics
APIDOX are generated by the Doxygen documentation tool. This tool reads the source code for your application (or library) and produces nicely formatted documentation from it. There is a good reference manual available -- but let's hope to make it unnecessary to read, for basic use anyway.

You don't even need to have Doxygen installed to work on apidox for your application. Every few hours, api.sugarlabs.org compiles apidox for all of the Sugar modules we know about. Logfiles are kept and you can see your apidox on the site, or read the error messages and fix those. ...it might not be the fastest way to write and fix apidox, but it gets the job done, and if you spend just an hour at the end of the day writing some apidox, it's sufficiently useful.

Basic apidox are fun and simple: you write comments in your code explaining what things are supposed to be for. These comments are nearly indistinguishable from stuff you would be writing in the headers of your code anyway, so that's not hard to do.

APIDOX consist of two types of comments python docstrings and doxygen directives. These docstrings are surrounded by """ (quote, quote, quote) -- that's what makes them special. The rest of the content of the comment is just plain text describing a part of your program. The plain text is interpreted by the Doxygen processor, so that you can write accurate descriptions of parameters, return types, and do some basic text markup.

Doxygen directives are one line comments preceded by a #. They are short statments that help Doxygen determine the organization of your package.

But documentation can be very straightforward: just write down what a method does, surrounded by """ and """, like this:

Note: This process is slightly different from the instruction on the Doxygen site. Standard Doxygen comments are surrounded by /** and */. In order to maintain compatibility with standard python documentation, api.sugarlabs.org runs a preprocessor that converts docstrings into doxygen comments.

For proper apidox, you need to document every "thing" in your program. "Things" here are: sugar modules, python packages, python modules, classes, functions, and variables. Complete apidox looks something like this:

You can see here that each nesting level of "things" is documented with a comment -- the module, the class and the method. Things that are private do not need apidox.

It is important to document each level of nesting, because if you leave one level out, Doxygen will ignore the documentation in inner nesting levels, and you will be left wondering where it has gone. For instance, if we leave out the class documentation, then the method documentation will vanish as well. This is one of the common pitfalls to writing apidox.

If you just do this -- write an explanation for every part of your program -- then you're already a long way on the road to having complete apidox. Writing all those explanations makes your program more accessible to other developers -- and often shows you where the design or choice of names is sub-optimal. So it's a win for you both ways. You can consult the list of supported tags for examples of more fancy apidox -- explaining parameters, for instance, and annotating the apidox with credits and examples. It's also worthwhile to take a look at the section on enabling apidox, but it's also fine to divide the work: you write the apidox themselves, and ask me (mailto:dfarning@sugarlabs.org) to enable apidox generation for your module. And I will.

Writing APIDOX in New Code
The following little checklist should get you through the worst of writing apidox.

1. Write apidox as you code.

The discipline it takes to write down the apidox for function foo now as you are thinking of foo and what it needs to do more than compensates the effort later where you have to remember what foo was supposed to do, anyway.

This isn't to say you have to do it this way, but it is convenient. The apidox also document design decisions. They also document what you want a particular piece of code to do, regardless of what it actually does. That makes it possible to independently check that the code does what it's supposed to: because it's written down.

2. Document your packages completely

Packages are what's most visible to users (in this context, users are developers who are re-using) of your code, and they should be complete. Document each structural bit of the package as you go along. This means:

In the root directory of your packages create a file named Mainpage.dox. The first line of the comment must be the @mainpage doxygen directive followed by your package name. Next is the package comment wrapped in a docstring. Last are some apidox variables which help define the menu structure.
 * Every package should have a package comment.

A given module only needs a comment once in your source tree (or within one bunch of files that generate apidox together). Near the top of each module __init__.py file, create a @mainpage  directive followed by a description of what the module is for and what it defines. Wrap this up in docstring.
 * Every module should have a module comment.

The same caveats apply as with namespace apidox: make sure the class follows its apidox immediately.
 * Every class should have a comment. Classes are the important building blocks of your application or library, so this is one place where writing lots helps. Write down why the class exists. Write down what it is supposed to do. Give an example of how to use it. Explain how not to use it, or what prerequisites it has (for instance, lots of classes need a KInstance object, but don't document that explicitly).
 * Every method should have a comment explaining what it does and what the parameters are for. Method parameters should be documented using @param. Don't rely on the name of the method or the parameters to be fully self-documenting. Besides, writing these things down properly will trigger Doxygen errors if you change them in an incompatible way later -- and that is going to save you lots of time in finding source and binary incompatibilities and explaining to users why their code suddenly doesn't do what they expect (assuming it compiles at all). So good method apidox is an additional insurance against making bad changes. Same caveats apply.

3. Watch this space!

Watch api.sugarlabs.org for the results of your apidox work. Check the log files for errors -- Doxygen can complain quite loudly.

4. Write a main page for your application.

This is usually done in a separate file in the top-level of a library or application. The file's content is just a single apidox comment that starts with /** @mainpage title ; the rest of the file is just a long comment about what the library or application is for.

Fixing APIDOX in Old Code
<!-- Writing apidox in old code is a lot like writing the same apidox in new code, except that there is more cruft in the way. The number 1 tip to follow is: watch the logs on English Breakfast Network. Those will show you what errors there are in the apidox. However, Doxygen can't catch everything that is wrong with the documentation on its own, so you will have to do some reading yourself. The other tips for new apidox apply equally here: you want to document everything, in a consistent style. If methods show up on the generated apidox pages with no documentation, you know that you have more apidox to write. (Doxygen may provide an error message, but doesn't do that everywhere in the current setup because there would just be too many.)

In old apidox, you are more likely to suffer from the following symptoms:


 * Missing parameter documentation (because parameters were renamed, or added, or removed, or something).
 * Missing method documentation.
 * Missing class documentation.
 * Documentation that has wandered off on its own and is attached to the wrong thing now.

The first of these can be fixed by correcting the parameter documentation. See the examples section. The next two -- missing documentation that you can see is there in the source files but that does not show up in the generated HTML pages, is usually a matter of missing documentation on surrounding blocks. See the common pitfalls section, and make sure that the surrounding classes, namespaces and files all have documentation.

The last problem can best be fixed by moving the offending documentation back to where it belongs (really, it's not the documentation that is at fault, it's whatever has squeezed in -- the home-breaker -- between the documentation and the thing it was originally attached to). You could use some Doxygen special tags to avoid moving stuff around like that, but it does not help the understandability of the source much.

Example APIDOX
So what does documentation look like in the headers? How do you write a method documenation that describes the parameters as well? This section contains boilerplate for most common situations. Doxygen does not require a strict style -- it will ignore whitespace and asterisks at the beginning of a line, so you can make the documentation ASCII-pretty.

Documentation for a file: The newline after @file is significant! The text after @author is listed in a special Authors section of the apidox; you can list multiple authors.

Documentation for a namespace:

Documentation for a class: Some Doxygen special commands are used here to provide additional information. @author (as with files) identifies authors of the code; these are collected in a special Authors: section of the apidox. You can list more than one author. The @since tag tells users since when the class has existed. It is usual to put a KDE release number here.

Method documentation: We can use @author and @since just like we do for classes. In addition, there are the parameters of the method that can be described. @p is used to refer to them in running text, and @param is used to construct a list of parameter descriptions that is specially formatted. Finally, @return entries describe the values that may be returned by the method.

Enum documentation is described in the section on writing new apidox. The same kind of documentation as for classes applies, with the addition of the documentation for each enumerated value which belongs inline.

Common Pitfalls
This section lists common pitfalls in writing apidox. Typically, they are easily overlooked mistakes that produce weird error messages, but I will also include some stylistic pitfalls that should be avoided.

Missing APIDOX: You know you wrote dox for class Moose, but after generation they are not visible. You know that the file-scope function int foo is documented, but it's not there either! What is going on?

The most common pitfall, the one that leads to "missing" apidox, is forgetting to document surrounding structure. The "structure" in the source comes from files, namespaces, classes and methods. In order to document a class you must document the namespace it is in (if there is one) and the file that it is in. In order to document a method, you must document the class it is in (and thus the namespace and file). So the easiest rule of thumb is to document everything.

Broken parameter documentation: Documenting parameters to methods can be done two ways: you can document none of them, or you can document all of them for a given method. There is no middle ground (that doesn't generate gobs of errors that you should fix).

If you document all of your parameters (which is a good thing to do, and generates things in a nicely formatted fashion), then your method apidox consists first of a general description of the method and then a bunch of @param tags which describe each individual parameter. The @param tag is followed by the name of the parameter -- watch out for spelling and case-sensitivity! -- and then the description. The description can span multiple lines.

When you document all the parameters like this, Doxygen will complain if you misspell parameter names, or forget some, or mention parameters that are not there. This will force you to update the documentation if you change the method signature. And that's a good thing.

Additional pitfalls are putting the type of the parameter in the list, like @param int x, which makes "int" the name of the parameter. Another pitfall is that @param should come after the general description. Once the @param list starts, nothing can stop it except for other list-style Doxygen tags like @since, @author or @return. So write @param after the story and before, say, @return.

If you document none of the parameters, you do not use the @param tag at all. You can talk about your parameters by writing @p before the name of a parameter (or anything else, really, but it only makes sense in front of a name of a parameter). Like this:

Misuse of tags in running text: This boils down to the warning above about where to put @param: not every Doxygen tag can go anywhere. Some start lists or basically end the general description part of a description, so you need to avoid using them in running text.

Chosing between Doxygen errors and compiler warnings: If you are going to document your parameters, you need to name them. If you are defining a stub method, this can lead to compiler warnings.

Accidental tags: It can be easy to accidentally use Doxygen tags in running text -- email addresses, backslash escapes, those are the easy ones. Watch the Doxygen logs and escape that at sign with a backslash when needed (i.e. write \@ to get an at sign). It's probably a good idea to avoid the backslash style of apidox entirely; at the same time, if you happen to write \moose, Doxygen will complain that that is an invalid tag.

Accidental HTML: If you use &lt; and &gt; in your apidox, these may confuse Doxygen -- especially if you write things like &lt;service&gt; which look like HTML tags. This is a common way of writing some kind of element that may be replaced in a method call or string or something, so it crops up all the time. You know, you write some thing like this:

This bit of dox is terribly broken, because the whole sample connection string will be interpreted as (bad) HTML and ignored. There are two solutions:  
 * Write \&lt; instead of just &lt; to escape the &lt; and make Doxygen output it normally. Doxygen is smart enough to turn this into &amp;lt; in HTML output. This has only a minimal impact on readability of the dox in the header files themselves.
 * Use the HTML formatting that Doxygen makes available, and write &lt;i&gt;foo&lt;/i&gt; instead of &lt;foo&gt;. This produces nicer output with italics instead of plain text, so it is easier to spot what is the replaceable part of the text. The downside is that it has a larger visible impact on the apidox in the headers.


 * Superfluous @ref
 * It can be tempting -- certainly if you've written dox for other projects -- to use #method or @ref method to force a reference to another method somewhere. Relax, it's not needed and usually causes Doxygen warnings to boot. Just name the method normally, make apidox and watch the references appear naturally.

KDE Specific Tags
@bc: This tag indicated binary compatibility, much like @since does. The argument after @bc is the scope of binary compatibility (for instance, KDE4). Classes that are not marked with @bc may, in some modules, be flagged as incompatible so that they can be avoided.

Example:

@libs: Use this tag to indicate what libraries should be linked in for the given class. Although this is usually the name of the directory the class lives in, this is not always the case.

Example:

Code Examples in APIDOX
It can be useful to put example code in the APIDOX for a class. Very useful, in fact, for the reader who is wondering how exactly to use the class in a straightforward way. Most classes have example code.

One way to write example code is to use @code and @endcode around blocks of example code in the Doxygen comments themselves, like this:

This is how most of the examples are done, actually. It works reasonably well, you can pare the example down to something really minimal and leave out all the boilerplate.

An important drawback of this approach to writing example code is that the code is never checked to see if it actually works. The code is also so terse, usually, that it's hard to expand into a complete example that actually compiles and runs.

To solve this problem, you can write real code that actually compiles (as part of the test suite for the code that you have anyway) and that illustrates exactly how to use the class under consideration in an actual program. Better yet, you do not need to include all the code in the APIDOX, just the interesting bits. The way to do this is to put the code in a file in the tests/ subdirectory of your code. Then use Doxygen's @include tag to include the code from that file into the API documentation.

Links
List of supported doxygen tags
 * This is a list of all available documentation tags of doxygen, from the official site. Note that the list uses a backslash in front of a tag, while this page always used the at sign. Both are allowed, but for KDE you should use the at sign to stay consistent within KDE.

-->