Why you really, *really* should document your code properly, inside and out
By Brandon Ching
On June 2nd, 2008
Coders like to code; coders don’t like to write. It’s no secret that thorough and approachable documentation is a rarity in the coding world. Despite its necessity for the adoptability of a given software package, finding good documentation is notoriously difficult. I’ve seen “documentation” consist of a simple phpDocumentor run. To the folks at Magento: this is NOT DOCUMENTATION!!! It’s merely is an incomplete reference guide!
Maybe it’s because I’m not the greatest coder this side of the Mississippi, or perhaps because I actually have an interest in writing English, but I, for one, like to write documentation. In past projects, I have begged bosses and project managers to allocate time for me to document the code that I have written (every time I was denied…by the way). [Not the case here, for the record. We love documentation and Brandon's new. He'll come to see that. :-) —Ed] Good documentation, whether for internal applications or publicly available code bases is nearly as important as the code itself. Here’s why, after the jump.
Why develop good internal documentation?
- Sorry to say this folks, but employees leave. They get better jobs, or get fired, or for some reason or another leave your organization. By having your coders maintain well written documentation you give yourself two advantages:
- You protect yourself against workers leaving with legacy application knowledge, and
- You shorten the ramp-up time of the new employees that take over the application.
- You automatically create a lasting knowledge base for any problems that might arise in the future. A documentation system, if properly designed and maintained, can make finding obscure answers to even more obscure problems take much less time.
And what about good external documentation?
- PEOPLE MIGHT ACTUALLY USE IT!! Yes, this is an obvious one but think about it: there are only a few truly elite coders for whatever system you may developing. They are the people who will be able to (and more importantly, have the time to) manually go through your API and automagically know how to develop within it. Now, the other 90% of programmers that will be using your product (who may be very good programmers but left the Matrix years ago) could perhaps use a natural language interpretation of what you’ve developed. This obviously expands the reach of your product (and hence the adoption rate) while exemplifying the nuances of your coding genius.
- It also shows your dedication and attitude on software development. For instance, what would you say if you bought a new car and it didn’t come with a manual? Sure, you probably already know how to drive it, but what about the viscosity of the oil it uses? Or maybe the recommended service intervals? Without the manual, it would appear that the car manufacturer didn’t really care much about your experience with their product.
So we know that documentation is a good thing, but the problem really seems to be, “what makes good documentation?”
Put simply, good documentation should:
- Be able to take an individual with average programming ability and allow them to understand AND implement about 90% of your product. The more features you document properly, the more useful people will find your product.
- Take a building block approach to developing. Start out with the basics but then progress, using your previous topics, into more robust and complex designs and implementations.
- Provide valid and WORKING code samples in each documentation topic that allow the user to follow along and progress with the documentation. People love to develop into examples. If you provide them with great starting points for features and comprehensive examples that demonstrate the potential of your more impressive features, developers will run with it and make their own magic.
- Provide a reference of all classes, methods and variables (inherited, etc), along with sample usage of each. I suppose a phpDocumentor-like output would work here but I strongly feel that code samples which detail abbreviated usages is very important.
- Be written as if you were writing to a teenage programmer (or intern). I’m sure I will get a lot of negative feedback on this one but there’s a reason USA Today is in such wide circulation…not because they provide great news stories with college-level English (which they don’t), but rather because they assume little and walk their readers through the details of the story. This is the type of approach good documentation should have. It must be accessible by the majority.
Now this is not to say that documentation needs to be completely dumbed down. By its very nature it can’t be. What we are dealing with here are quite often the highly complex interactions of code and design. But what we as coders and developers can do is provide a solid foundation for others, to help them get a handle on our creations as best they can. This is a best practice for not only the success of our code but for the success of other coders as well.
A relatively decent exmaple of this documentation philosophy is the Google Maps API documentation. It includes nice code samples and progression. If you have any other exmaples of good code documentation, please post and let us know! [And please share any other guidelines by which you try to document your code and your projects. Or any rants about projects you've seen ruined by poor documentation.—Ed]
Tagged with: adoption, best practices, code, comments, documentation, knowledge, legacy, phpdocumentor, projects
Posted in: Development, How To, Rants
Related Posts
Trackbacks on this post
Discussion on this post
-
For what it’s worth, WordPress’ Documentation is pretty stellar, despite our problems with the platform. It makes extending through plugins feasible, which is another great reason to document your code and your project’s capabilities in full.
-
Good points, and something I’ve been thinking of writing on for a while too. I do not claim to hold myself up as a paragon of good docs, but, like pornography, I know it when I see it (apologies to Justice Potter Stewart!). Many would say what you’re asking for fall outside of ‘docs’ and in to the realm of ‘tutorials’. If someone wants to make that distinction, fine. I would then say projects need good tutorials in addition to (or even in place of!) inline javadoc-style comments.
-
Great article. I have one thing to add. I think the whole process of documenting an app makes you a better programmer. When you can explain it in simple terms to an average programmer, you know that you’ve got a good handle on the code. And, it’s so nice to be able to refer back to well documented code, especially when you need to change something in a hurry later.
@Chris Cardinal, I also love the WordPress documentation, and I’m also quite impressed with the PHP documentation and the MySQL documentation too.












July 25th, 2008 at 2:17 am
[...] Document your code. Do it. I’m serious. Use JavaDoc syntax (for PHPDocumentor) and document the hell out of it. Use comments to identify what things are, and use them to describe what you’re doing in situations where your intent is not exceedingly obvious from the code. Some say that if your code is too complex to be understood by looking at it, then instead of commenting, you should simplify your code. That’s ridiculous and those people deserve to be ridiculed. [They're probably mostly Ruby coders.—Ed] Sometimes things are just complicated, and well written comments describing your intent and providing context and explanation for actions is not only essential for other developers to understand your code, but also to help you understand what you were doing when you come back to a project to make changes. (Many a time, I’ve stared at code I wrote for what seemed like hours, trying to remember what the hell I was thinking. Documentation cures this problem. Most of the time.) [...]
July 31st, 2008 at 12:57 am
[...] The EAV data model is great. It’s fast and makes sense when you have many, many possible attributes to assign to a specific object. However, trying to make database-level changes to all or a subset of products is quite a pain in the ass. This is not so much a knock on Magento since the model does make sense given the options to add arbitrary attributes and such. I suppose this goes back to Magento’s lack of a well-documented database schema (or decent documentation in general). [...]
October 27th, 2008 at 9:59 pm
89 Ways for You to Become the Coolest Programmer in the World…
Since there are dozens of posts on becoming a better developer, but no single post with all the advice you need, perhaps, you’ll find this short guide useful.
1. Learn the Skills You Need
Learn the programming basics
“The goal of this guide is to b…