So a few weeks ago I posted a post titled “Documentation: Boring Yet Necessary“. If you haven’t read this post, read it before continuing on with this post so that I don’t have to re-explain what I already have explained. Done? Awesome. Let’s continue!
So what should this code documentation look like? Well, it really all depends upon personal preference, but here are some tips on what you should be doing in your code:
- Have a documentation block at the top of every file – This is a good practice to have. Basically, this documentation should include your name, the file name, the creation date, and a basic description of the file. A sample of this is below:
/** Author: G7470 Filename: build.php Date Created: 02/15/2016 Description: This file allows a user to build a new building, which will allow them to collect some valuables. **/
- Document each function or method – If you are creating a function and/or a class method to handle a particular “function”, that should be documented with what that function does, what is needed to run the function or method, and what is returned from that function or method. A sample of this is below:
/** Name: get_user_level Parameters: $userid - integer - The user's ID Return: $level - integer - The user's level Description: This function given the user's ID will calculate their level and return this value. **/ function get_user_level($userid) { - Document any other ambiguous code – Any other code that is a pretty large block should be documented, including large if statements, for loops, while loops, switch statements, etc. A sample of this is below:
/** For each user, add 50 points to their account **/ while($useri = $db->fetch_row($uq)) {
Get the idea now? Essentially, the main idea of doing such documentation is to provide anybody reading your code with some understanding of how it all comes together. Without that, your code appears to be messier than it really is, and anybody reading it will be clueless as to what they need to do.
What about general documentation though? Well, again, it depends on what you feel is best to provide for the user, but my suggestion is to provide the five basic areas for each “module” or “feature”:
- Functional Overview – The basic idea of what the “module” or “feature” will be doing for your site.
- Updated/New Files – This can provide the reader of where the code for that “module” or “feature” is located.
- Technical Details – Any specific technical details, like for example if the user must have x amount of coins to do something, that’s something to note here.
- Database Information – Any database tables that were updated or new should go here.
- Other – Any other information of importance should go here.
With that, I’ll use your imagination as to what this document should look like. My suggestion would be to make this document as formal as possible. If you create one-off documents that are difficult to read and/or follow, then there’s no point in providing the documentation at all!
That’s all for now, until next time…keep that tech gear ticking.
~Goliath7470
