Documentation??Wait a minute, wait a minute - what in the world is this documentation stuff? We don’t document, we comment. What is this? To me, commenting and documenting are two different things, so allow me to explain. CommentationCommenting code is the simple part. A comment resides near the line of code that you wish to explain. For example:
Of course, the previous code would assume x had a value of zero, but that’s irrelevant for this example. Simply put, you comment to explain a line or two of code, nothing more. Something like this does not help at all:
Whoa. While the first three lines do not need any extra comments thanks to descriptive variable names (covered later), what’s that last line do? Obviously, thanks to the comment, it has to do with filling the structure. But fill it with what? That line, although related to the last comment, still deserves a comment of its own:
That’s much better! Could we have guessed that? Probably. But why bother take the time to guess when you can be told outright what it does? Commenting, as shown above, is a term best used when describing single or multiple lines of related code. Again, not too many lines of code should be described by one single comment. DocumentationWhen you document code, you take on a wider view. I refer mainly to documenting in this article because it is a term that assimilates commenting into it. A commentation can be referred to as a documentation, but not vice-versa. Why? Because while the scope of a comment is only a few lines, documentation encompasses a whole function, or list of functions, or list of variables, or even a whole program. Here is an example of documentation and commenting put together:
This is what a documented function should look like. It has a header explaining the purpose of the function as well as what each variable is used for. Inside the function body there are comments telling you what everything does. Now, look at the function and pick from below what statements describe it correctly:
First, scroll up and reread the definition I gave you on documentation. Then decide. If you chose A and C, then you’re correct. Give yourself a pat on the back. Could you get away with B? Maybe, but then you get the picture of a few measly lines of comments, not a whole header. Throughout the article I’ll change between commenting and documenting. Just remember that anything I say about commenting can be applied to documenting, but don’t try to switch that around. Things could get ugly. Now that we cleared that up, let’s move on so I can start to show you how to comment and then expand upon that and document. |
|||||||||||