Code Documentation
Code documentation is as important as the code itself. It helps readers of your code, including your future self, to understand
- the purpose of a piece of code
- what assumptions are being made, and
- the reasoning behind why certain things are done.
C Syntax for Comments
In C, you can write comments in two ways:
- Either prefix a one-line comment with two slashes
//
, or - Write multiple-line comments between
/*
and*/
For example:
1 |
|
1 2 3 4 5 |
|
The Doxygen Format
In CS1010, we will adopt the Doxygen format for C comments. Doxygen is a tool that automatically generates HTML documents from comments in C code and is widely used in the industry.
We write a Doxygen comment with an additional *
after /*
:
1 2 3 |
|
The comments can be free-form text. However, to help with creating a more structured document, we can add what Doxygen calls special "commands". I view these commands as keys to certain information. Useful commands are:
@author
: The name the author
This is used to identify the author and placed at the top of the .c.
file This is what you have been using since Exercise 0.
@file
: The name of a file
This is used to identify the name of the file and be placed at the top of the .c.
file. This is usually written for you already.
@pre
: The precondition of a function
If your function makes certain assumptions about the inputs, explain it using this command. This is used to document assertions at the beginning of your function (e.g., a string is not empty, a pointer is not NULL, etc)
@post
: The postcondition of a function
This command is used to document assertions that are true just before you return from your function.
@param[<direction>] <name>
: Describe a parameter of a function.
<name>
is the name of the parameter (can be a variable, array, pointers, struct, etc).
<direction>
indicates if you are passing data in or out of the function. From Unit 1 to 16, we only pass data into the function. For such parameters, we document it with @param[in]
. In Unit 17, we will learn about passing by reference. If a parameter is passed by reference to be modified inside the function, we will document it as @param[out]
. For a parameter that is meant to serve both purposes (pass a value into the function, be modified, and passed the new modified value out), we use @param[in,out]
.
@return
: Describe the return value of a function
The comments should be placed before a file, a function, or a variable that you want the comment to apply to.
Example 1: CS1010 I/O Library
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
|
Example 2: Collatz
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
|
Note: a better design of the function above is not to rely on the
caller to set max_num_steps
to 0, but set it at the beginning of
the function ourselves.
Example 3: Taxi
1 2 3 4 5 6 7 8 9 10 11 12 13 |
|