How To Get Developers To Document Code
snydeq writes "Poorly documented code? Chances are the problem lies not in your programmers, but in your process, writes Fatal Exception's Neil McAllister. 'Unfortunately, too few developers seem to do a good job of documenting their code. Encouraging them to start can be a difficult challenge — but not an impossible one,' McAllister writes, adding that to establish a culture of documentation managers should favor the carrot before the stick. 'Like most people, programmers respond better to incentives than to mandates. Simple praise can go a long way, but managers may find other ways to reward developers. Are your developers occasionally on-call for weekend support duties or late-night update deployments? Consider giving them a break if they volunteer to pick up some extra documentation burden. Of course, financial incentives work, too.'"
It's called doing your job.
When the foot seeks the place of the head, the line is crossed. Know your place. Keep your place. Be a shoe.
But money costs money!
Finally had enough. Come see us over at https://soylentnews.org/
Nothing else seems to work...
In space no-one can hear your vuvuzela.
In this case, mandate works best because this is something that the developer SHOULD be doing anyways. Not documenting your code is inexcusable.
I prefer the theory that well developed code is it's own documentation. (believe this comes from reading a lot from Uncle Bob)
Crud loads of javadoc/msdn like documents aren't as effective as readable code and a few real world examples.
Once the boss learns about financial advantages of outsourcing.
Management says "do X" as fast as you can. Programmer does X and report back. Documentation, like proper testing or elegant design, takes time and thus cost money. It often saves cost over the long term but if the management doesn't care why should the programmer ?
If what you need documenting is at the level of comments within a file your problem is not that your programmers aren't writing comments it's that they are not sriting good software. If meaningful class, method and variable names and sensible expression constructs are used there is no benefit to be had from comments.
I'll make an exception for comments to explain why a given piece of code is not actually batshit insane but required to work with a third-party library you have to use.
Producing documentation that spans classes and discusses how things are designed to work at run-time is, however, part of the job.
I guess today is a passable day to die.
I took a class at University many moons ago in software documentation. I got an 'A', and it was a valuable class in that I learned I sucked at it. (But I was better than most of the students)
The teacher, with a PhD in English, was a master. She probably couldn't code worth much, but she could take unclear concepts and make them clear enough for a newbie.
As long as you hire great programmers you are going to get great programs. If you want great documentation, you need a great documentor.
All ideas^H^H^H^H^Hprocesses in this post are Patent Pending. (as well as the process of patenting all postings)
Or simply don't accept code that isn't up to snuff. That includes documentation and testing. Peer review will help make this happen automatically. Here's how it goes:
"I can't tell what this code is supposed to do."
"It toggles the widget's status."
"Is that documented somewhere?"
"No."
"Let's get that done."
You don't have to have this happen too many times before you just do it to avoid that. Same as all the other good code practices.
If you aren't doing peer review, you aren't getting the best code you could. It seems painful at first, but good developers actually like getting feedback on their code, and improving it. It isn't long before any good developer comes to desire it, instead of dread it.
"If you make people think they're thinking, they'll love you; But if you really make them think, they'll hate you." - DM
As I used to say "it was damn difficult to write, it should be bloody difficult to understand".
My UID is prime!
I'm growing increasingly convinced that in many situations, code only requires a minimal amount of documentation.
Code under development changes rapidly, so most of that documentation would never get used. And lots of code is best explained by the code itself plus small, local comments, rather than by separate, copious documentation. And then there's the fact that software often gets discarded before anyone would need to make the kinds of modifications that required extensive documentation.
It seems like sometimes, people who call for extensive documentation do so from the intuition that it's a diligent, long-term-smart strategy. I think the picture is muddier than that.
If you warn a developer up front that documentation of code is part of the job and you let them know there will be random code reviews they can't complain when they see no merit pay increases or eventually are laid off for now performing as expected. If a company does not allow for adequate time to have developers document code during a project it's the companies own fault, period.
Developers have been told time and time again through-out the last 20-30 years that documenting your code is good coding practice, but a vast majority don't. There are several reasons, and the one I hate worst of all is the "job security" response. Be honest, you don't want to so you don't.
Life takes interesting turns, but the most interest is when you're off the beaten path.
I've never had a problem of incentive or motivation to document my code; my problem is and always has been time.
Documenting anything other than conceptual stuff from the beginning is a bit of a waste (though far from a complete one) because the final product (due to bug fixes/spec changes/etc) never matches what you set out to do.
So when you do get to the point of post-testing where the code has stabilized and it's time to really sit down and document everything fully you're being given new stuff to work on. Sure I can (and do) raise a bit of a fuss about it and explain that if it doesn't get documented *now* it'll never get documented *well*. That always seems to fall on deaf ears though.
I think a lot of it is that the deserved biting in the ass never comes. Shortened product life cycles and customers that have become (wrongly) more tolerant of buggy software in production environments means that you can stumble through with a vague knowledge of the product and never really get fucked. It's the same basic reason that so many bad programmers have jobs.
// WHY
int WHAT(...) {
return HOW();
}
-Ouija- poke 53280,11:poke 53281,12
I write comments when what I'm doing is "clever", or hardware-required. Otherwise, I use meaningful labels and a readable syntax (C, for example, is K&R, except that all block-opening braces are on the next line for easy "line-up", any code beyond the current line has braces; variables have meaningful, not formulaic, names).
Too many managers and "religious" programmers want forty lines of comments for a twelve-line function. They ain't getting it from me, and, so far, my peers are happy with what I do. I comment shell scripts, PHP, Perl (more than the others), ... when there's a real reason, not just to fill out some silly "comments requirement". I put useful comments in svn checkins, too. Those are probably more meaningful than most of the code comments I have seen.
I've found two camps at my company. Both camps document their code with comments and meaningful commentary. It is more a matter of the "design document" that describes the product or project that is a problem. The document that is perhaps supposed to guide the overall architecture of the product, and thus, the architecture of the classes, methods, interfaces, etc., aka. "the code".
:)
The first camp of developers just wants to get in and start coding. They often say "I need to code to figure it out." The actual web site works well, but new developers have a tough time maintaining that same site. The original developers are pretty much the only ones that can change the critical aspects of the site, and even then, as time goes by, that becomes difficult. But, they got the site up and live in the time allotted. And they wrote a 2 page "design document" when they were done - the doc was useless.
The second camp of developers writes a 50 page design, then starts coding. The actual web site works well, but the overall time to get the site up and live took 5 times longer (that includes the time to document.) The documentation evolved with the changes that were made along the way. Maintaining this same site went well at first, since the documentation was great. But, the docs slowly get neglected, and in a couple years, this site is difficult to maintain.
In the end, perhaps there is a happy medium. A "good enough" design document, and get started coding relatively soon. I used to lean toward the second camp, but now I lean toward the first camp
"What!?! Comment my code? You question my honor! I'll kill you where you stand!"
-- Mukluk, Klingon Programmer
TLR
A man no more knows his destiny than a tea leaf knows the history of the East India Company
Documentation doesn't really have all that much value unless you can trust it, and in a large organization with a large code base there's likely to be code where the documentation is incomplete, inaccurate or plain old outdated and wrong. Once you cease trusting the comments and realize you must read the code to actually be sure what it does it turns into an evil circle. Nobody bothers to look at the documentation because it's useless and because it's useless nobody bothers to fix the documentation. And you fixing a few snippets here and there isn't going to change that perception and break that circle.
Like with structured code it's a lot easier to trash a code base than it is to keep it clean. You can apply a quick fix to the code and it works, but as you get layers or layers of hacks and quick fixes it grinds to a halt. For a time nobody's going to realize the documentation is deteriorating either, it's only as people lose more and more grip on what's going on - usually after key people left - that you're now spending more and more time locating the problem. And since there's no clear system to things, you keep adding more hacks.
Lack of documentation is just another form of technical debt, and like the entire economy we like to push that debt ahead of us. Maybe next week you'll get another job or get downsized or get outsourced or management will decide to replace it with a different tool or you'll be promoted or reorganized so it's no longer your problem. Meeting deadlines or performance goals now is more important than maintenance later. And your manager, well he's probably got no longer perspective than you, make the executives happy, collect a good paycheck, get a good reference.
Actually it seems a miracle that we produce software that keeps working, because it doesn't seem like much of anyone is in it for the long term. Workers care about their performance and keeping their jobs this quarter, CEOs and stock holders care about the stock price this quarter and that goes for most people in between. Documentation is cost and nobody wants to take the cost in this quarter if they can take it in next quarter. I've seen companies work extremely hard to get income booked in the current quarter or FY, not because of the two days but because of bonuses and such.
Live today, because you never know what tomorrow brings
Make time in the schedule to do the documentation.
When you're already working a 50-60 hour week just to get something out the door because some moron cut your estimate in half before promising a delivery date, where are you supposed to find the TIME to document instead of code, test, and ship?
Programmers may hate doing documentation, but it's MANAGEMENT's fault it doesn't get done. Period.
I do not fail; I succeed at finding out what does not work.
As someone who's started a new position and using classes with No comments, I can say I've wasted a good deal of time trying to figure out what certain public methods and certain classes do. They've used good naming conventions, but even so there is some subtly about what is done that could have used some explaining, plus looking through 1000s of lines of code before using a method isn't time effective. Its would be far easier for me to read through method header with inputs/outputs than to slog through code trying to figure out if this is the method I want. I don't want every line commented (I've seen that some placws), just the jist .
I came from an internal api writing group, and those using our code would just ask us questions if they couldn't figure stuff out, and we rsther they didn't so we documented ..
Examples or some test code I agree are super useful.
I hope to $deity you're not a manager. Your morale must be terrible if you do.
I have to echo other commenters: If you want well documented code, you need to allow sufficient time for said documentation to be written. Failure to do so isn't a failure in programming, it's a failure by management to build realistic timelines based on feedback from programmers.
If your programmers are constantly telling you they need more time, on project after project after project, and it's ALL of them, not just a few complainers, then you need to look in the mirror to find the source of the problem. Go to sales/marketing, read them the riot act about promising impossible deadlines, and get THEM fired if they continue to promise unicorns on a wombat budget.
Never underestimate the power of stupid people in large groups.
As an agile developer, I recommend against documenting your code.
You need to document the algorithms, but never the code.
Documenting your code will require you a lot of maintenance. Every time you change your code, you need to change your documentation. It's a never-ending process.
Instead, decide on a meaningful notation for your routines names (so any coder knows what the routine is supposed to do), and write tests, or more exactly functional tests.
Tests have a few goals:
1) check that your code behaves as expected.
Suppose that you have to fix a bug in your code, without tests, you cannot be sure that the fix will not be harmful.
It also allows greater flexibility in your code, and it will not smell like a rotting corpse.
2) show how to call your code, in order to get results. Examples are very useful for other developers.
3) confirm a behaviour. Your code does something, but sometimes, it's not clear. Your test should demonstrate the value of your code.
4) improve the stability of your program, and reduce the QA time.
Once the habit of writing tests is acquired, you'll start writing the tests before the code.
This is called TDD (Test Driven Development), and helps reduce a lot the amount of code you need to write, because you only write useful code.
Who wants to write unused tested code ?
I work for a firm that conducts a periodic release of code to its production environment. Those of us who regularly work the "release night" know what it means to document code well (and no, it's not just comments in the code). What our firm, and others like us, needs to do is rotate everyone through that situation, or others like it, so that they can see the flipside of their effort. Having to troubleshoot poorly documented code is a good way to instill in a developer good documentation habits.
Jesus told him, "I am the way, the truth, and the life. No one can come to the Father except through me. - John 14:6 NLT
If you want documentation and comments, to start with you should stop calling them programmers.
If you want documentation and comments, you call them software engineers and you allocate time for documentation, commenting, optimization, debugging, and other critical non-functionality tasks. Managers typically only allocate time to develop something that sort of works, not something that's polished, maintainable, debuggable, extensible.
I've run several engineering departments. Some have been good at documentation, others not so good. What's the difference? It's quite simple. Time When a company demands quick turnaround, its all the developer can do to get the code out the door. Good documentation falls by the wayside in these instances. In my experience its more about demands from the top than anything else.
The best way to have programmers write quality code is to automatically enforce it. Have an automated tool verify the code for comments and run it on your build server each time new code is committed. I am a .NET developper, so each time I commit code, StyleCop and FxCop are run on the build server. This means my code is always commented, hell even spelling errors are not allowed!
If I am too lazy to comment, the build server sends an email to the whole team informing them of my laziness.
Six months from now, long after I've moved on to something completely different, am I going to remember a function's return values? The inner workings of a particularly complex algorithm? Or the reasoning while a special case has to be handled differently?
If it's not going to be obvious to myself later on, it's worth adding a concise comment to explain.
Ita erat quando hic adveni.
First, what is the goal? The goal is not well documented code, what use to anyone is well documented code by itself. What you are looking for is a product that works and is easily maintained. Good code and any documentation is a huge part of easy to maintain. But where many people go wrong is that they see code documentation and commenting a critical part of a project. To the point where it starts to interfere with the creation of a good product that is easy to maintain.
So when looking at documentation it needs to make sense for the endgame. Who is going to use it? When are they going to use it? How much effort is going into its creation? I have seen projects where thousands of pages of documentation were generated where the only thing ever read again(other than by the document review team) were the few pages that documented the steps for configuring the server. When the next version was created nobody referred once to the old pile of documents that had every detail in the universe.
The next question is: what is documentation? This might seem odd but documentation can be the very directory structure itself. I wonder where the client code, maybe it is in the directory named client_code.
In agile or XP programing you don't tend to look too far into the future. So two critical documents that would be very active are your todo list for this cycle and a wishlist for future cycles that eventually turns into a todo list. The todo list, when done, can be chopped up into a crude set of documents that show the past if anyone were to be interested.
Lastly the code itself is an excellent place for documentation. A quick explanation of what it is about and why it exists can be useful if the file isn't already named verifycreditcardnumber.c.
So I don't think the criteria is so much poor or good documentation; so much as it is about useful documentation. If non programmers are intimately involved then lots of screen shots, text, and flowcharts are needed. If programmers are the only parties then it might almost all hide in the codebase with the exception of the screenshots mockups.
I always document my code. It saves time later. Six months down the road I'm not asking myself "why the hell did I do that?" It's for my own protection. I've had programmers tell me that they don't document their code because it's obvious what the code does, and sometimes that's true. But I think at least some of the resistance later to add features or fix bugs has to do with not wanting to struggle to remember what the code does. And -- this is just me, but -- you probably don't want to have a reputation for being uncooperative and not a team player in a down economy.
In a very early job, a guy with more credentials than I wrote a program we all used in a language I didn't know. There was a longstanding bug which he refused to fix [1], and he kept the source in a protected directory. So one night we broke into his account, I scooped the completely undocumented code, figured out the problem (had to change *one* (1) character!) and distributed the new binary. It took him awhile to figure out what happened. Boy was he pissed.
[1] Whether this was a dominance game or he really did not want to try to figure out the code he had written months earlier was a point lost in history.
Oliver's law of assumed responsibility: If you're seen fixing it, you will be blamed for breaking it.
I will document when I'm given the time to document.
If it's always the tyranny of the urgent, if there's always some fire to put out, I won't have the time to document.
I had a job where the manager was always complaining we didn't document. But he never fought upper management to give us time to document.
If you say "I have to have documentation and it is a deliverable" you will get it. If there is always something more important to do, it won't get done.
Except for ending slavery, the Nazis, communism, & securing American independence, war has never solved anything.
The agile people say you must never comment at all because you might change the code and then your comments will be wrong. This is only true if you're describing what the code is doing. If you are describing how the code supports the business, the comments will probably still be correct.
The scholarly types say you must comment profusely. Certainly comments help in areas where the code might be confusing for a newcomer, but no amount of describing what the code is doing in any given piece of code will shed much insight onto the business processes that code supports. Many programmers out there are just... bad... and they will probably not read the comments and screw up your code, anyway. Not that this is a reason not to comment your code. Having a comment with a warning might constitute useful proof of a given programmer's... badness... in the event that your code is screwed up in the future. If you comment, it is wise to make sure the evidence is something you wouldn't mind having come to light in the future.
Code itself is a specialized language by which two programmers can communicate, often across a gap of many years. An understanding of the business is both required and usually assumed by one programmer or another. If one of the two programmers has a less-than-perfect understanding of the business, his code will reflect his confusion. If the programmer reading the code has a less-than-perfect understanding of the business, he will find the code confusing no matter how good the comments are. If both programmers have a less-than-perfect understanding of the business... well... that's the situation we most often find ourselves in. And that's usually when people start complaining about documentation.
I'm trying to teach myself to set people on fire with my mind... Is it hot in here?
It really can be done. If your company has a culture where it is just how things are done, people will do it because it'll be more bother for them not to (as they'll get bothered when they don't).
I'm not saying it is trivial to do, just that it can be done. You have a corporate culture where that is how it is done, and people on all levels understand it, it'll be done. When the John Wayne programmer who refuses to do it comes in, he'll learn when every time he submits code it gets kicked back and the team lead comes and explains again that you have to follow certain conventions, document, and so on.
As an example of where it does happen would be Microsoft. If you talk to anyone who's seen the Windows source (either at one of the many universities that has a license to have it, or from the leak a few years back) they'll tell you it is very good in that regard. It follows consistent conventions, is well documented, and so on. That isn't coincidence, it is because they have a culture that demands that.
I expect it is also the sort of thing you seem more of the bigger and longer term the item is you are talking about. Something like an entire OS like Windows (where you are talking a ton of functionality, not just a kernel) is going to require a good setup since the code will be around for a long time and worked on by many people, and is exceedingly large and complex. When you are talking little "fix it" programs as many people have to do to make something work as a sysadmin, then it is less likely to happen.
What must be documented are the complex procedures and why it is doing it. Or if a previous simpler approach has been tried and then removed for a different non obvious procedure, that fact must be documented. Example:
"This class maintains a multi map of triangle pointers, sorted by area, to solid body faces. The sort key is a double, the truncation errors differ between Linux and Windows, so the order of visitation is not the same. When we used a simple std::multimap(Triangle *, FaceList, double),[*], the tables had different orders in different platforms. To provide repeatability we use a tolerance and tie breakers to make sure in both platforms the table will have the same order. It is called by render class, in a not very deeply nested loop. So CPU performance is NOT critical. Code is written for ease of maintenance and development, not optimized for CPU or memory." Such comments are useful, if maintained and kept up to date.
But most of the time it is not done. You see beautifully formatted comments created by macros in the editor, that occupies so much of screen real estate, I am not able to see both the code and the comment. Example:
[*] Syntax nazis: Slashdot formatter would not let me use angle brackets there. So I used () for illustration. /***Function Foo**/
it so stupid, I feel like screaming. If that coder is still working for me, I can scream at him or her. But mostly it is people who have left the company ages ago dumping that legacy POS on my lap.
sed -e 's/Chuck Norris/Rajnikant/g' joke > fact
I often hear that the proper approach is to document WHY you did something.
I am not a coding guru or even have formal studies on that field, I am really just a hobbyist, so anyone here has a few good valid examples of a "why"? I'd like to comment stuff properly.
So far I use non-shortened descriptive names for variables, functions and types, but I don't use that many comments. For example when parsing tokens in a string I write stuff like "//The format is ", but even so I feel the code is doing a better job at explaining it than I do.
As I went with the lengthy names, I have less problems getting lost on my own code, but I feel I am misbehaving. I blame my lack of formal education in coding, but would like to have some quality standards nevertheless.
I think the real problem is trying to measure code readability. Policies and coding standards try to address the issue while avoiding it by mandating frills that they think will kind or "imply" readability - function length, number of spaces in parentheses, badly defined Hungarian notation (dead, thankfully), Javadoc or similar commenting standards, and so on. But there's no getting around the fact that the only way to measure code readability is to read it.
This means that you need to put code review at the centre of the process. Not necessarily anything heavyweight, but just require that one other developer reads and understands the code (and points out any obvious flaws) before committing - with the limit that any questions the reviewer has should only be answered by changes in the code, because a question implies a readability problem. The developer can add comments, or rename variables, or restructure the code to make it clearer, but the end result should be readable code with fewer bugs (bugs live in hard-to-understand code, simply adding some intermediate variables to a complex formula can make them go away).
As long as the code review itself doesn't get bogged down with issues of How The World Should Indent and things like that - that's always a risk with developers looking at each others code.
useful rule of thumb is that programmers should produce documentation that's "good enough" -- and no more. If it gets the job done, it's probably good enough. Any additions are just as likely to be redundant. If that sounds vague, that's because it is;
Thanks Mr. McAllister, that clears it up completely. Bribe, threaten, or otherwise coerce programmers to do ... something, but I don't know what that something is.
I worked on a very large project a few years ago where the company did a study to correlate the number of comments with the number of reported bugs in the code. It revealed exactly what I already knew: the more comments in the code, the more bugs were reported against it. There are several reasons for that (complexity of code, skill of programmer, etc). Fact was that while fixing all those bugs we found that the comments around the bad code were usually incorrect: the code didn't do what the documentation said it did. So out of necessity we adapted to the situation, first thing to do when fixing broken code was remove the comments (because you shouldn't trust them), then figure out what the code really did, fix it to do what it was supposed to do, and (possibly) add back a few succinct comments that were correct.