+102  Q: 

Self-documenting code

I have a colleague who insists that his code doesn't need comments, it's "self documenting."

I've reviewed his code, and while it's clearer than code which I've seen others produce, I still disagree that self-documenting code is as complete and useful as well commented and documented code.

Help me understand his point of view.

  • What is self documenting code
  • Can it really replace well commented and documented code
  • Are there situations where it's better than well documented and commented code
  • Are there examples where code cannot possibly be self-documenting without comments

Maybe it's just my own limitations, but I don't see how it can be a good practice.

This is not meant to be an argument - please don't bring up reasons why well commented and documented code is high priority - there are many resources showing this, but they aren't convincing to my peer. I believe I need to more fully understand his perspective to convince him otherwise. Start a new question if you must, but don't argue here.

Wow, quick response! Please read all the existing answers and provide comments to answers rather than add new answers, unless your answer really is substantially different from every other answer in here.

Also, those of you who are arguing against self documenting code -this is primarily to help me understand the perspective (ie, positive aspects) of self-documenting code evangelists. I expect others will downvote you if you don't stay on topic.

+109  A: 

The code itself is always going to be the most up-to-date explanation of what your code does, but in my opinion it's very hard for it to explain intent, which is the most vital aspect of comments. If it's written properly, we already know what the code does, we just need to know why on earth it does it!

Agreed. Although occasionally even the best of code can hide what its final effect is, this is handled by answering the why in comments. As in, "Why did you just change those 5 variables in that way?"
Sam Erwin
Great answer. :)
Jeff Yates
hmmm... how is it this post has 57 votes and the writer only 253 reputation? Think I don't yet understand the voting system...
Pablo Marambio
253 rep - he's a noob. (no offense)
@Pablo: You max out at +200 rep per day...
My 2 Cent: isn't the [Unit, Spec, Behaviour, ] Case somewhat a answer to "why on earth" ?Then you could read the test case and should be given the intent to why.
I think those can answer the high-level why's, but they don't explain things like, "I'm padding this structure this many bytes, so that it stays properly aligned when transmitted to some obscure platform." For those, code comments are the best way of preserving institutional knowledge.
And thus you should comment where the intent isn't unambiguous given the code itself.
@tsellon, your automated spec can tell you that to, and the nice thing it's code that checks the implementing code. So if the implementation changes somehow, the spec breaks. How nice is that? Comments that alert you when the implementing code isn't doing what the comment specified anymore?
Pragmatic Agilist
+2  A: 

My view is written in this post:

The one single tip to document your code.


Instead of writing a lot of comments to explain the subtle behaviors of your program, why not restructure your logics so that they are self-evident? Instead of documenting what a method is doing, why not choose a clear name for that method? Instead of tagging your code to indicate unfinished work, why not just throw an NotImplementedException()? Instead of worrying whether your comments sound polite enough to your boss, your colleagues or anyone reading the code, why not just stop worrying by not writing them at all?

The clearer your code is, the easier it is to maintain it, to extend it, to work on it on future editions. The less ordorous is your code, the less need there is to comment it. The more the comments, the higher the maintanence cost.

Ngu Soon Hui
So you advocate never putting comments in the code and keeping them in external documentation? So when I'm looking in the code and trying to understand why a particular algorithm or formula was chosen instead of another I have to leave the code and hunt down external docs to find out the reason...
Scott Dorman
...and then there are probably even less guarantees that the external documentation will be kept up to date when the code changes.
Scott Dorman
The external documentation should be concise and focused on the main concepts, the critical decisions and listing the important areas, which might be covered in separate docs. It should read like an essay and not like a list of bulletpoints.
...and you should read it (the whole 10 pages of it) before you touch any code on the project.
Ngu, please consider summarizing or providing an excerpt here after the link so people can review your basic concept without clicking through.
Adam Davis
Ngu, I edited your post to include some of the post you linked to. I also upvoted this answer since your post actually does answer my question.
Adam Davis
+3  A: 

self-documenting code normally uses variable names that match exactly what the code is doing so that it is easy to understand what is going on

However, such "self-documenting code" will never replace comments. Sometimes code is just too complex and self-documenting code is not enough, especially in the way of maintainability.

I once had a professor who was a firm believer in this theory In fact the best thing I ever remember him saying is "Comments are for sissies"
It took all of us by surprise at first but it makes sense.
However, the situation is that even though you may be able to understand what is going on in the code but someone who is less experienced that you may come behind you and not understand what is going on. This is when comments become important. I know many times that we do not believe they are important but there are very few cases where comments are unnecessary.

Then refactor it until it's clearer. I am a firm believer that there's nothing that can't be said by code pretty clearly.
Except for why a specific implementation, algorithm, or formula was chosen over another equally correct one. You can never describe why a choice was made in code, only what that choice was.
Scott Dorman
+1  A: 

I think what he might be getting at is that if comments explain what the code is doing it should be re-written to be clear what it's intent is. That's what he means by self documenting code. Often this can mean simply breaking up long function into logical smaller pieces with a descriptive function name.

That doesn't mean the code should not be commented. It means that comments should provide a reason why the code is written the way it is.

+3  A: 

I think that self-documenting code is a good replacement for commenting. If you require comments to explain how or why code is the way it is, then you have a function or variable names that should be modified to be more explanatory. It can be down to the coder as to whether he will make up the shortfall with a comment or renaming some variables and functions and refactoring code though.

It can't really replace your documentation though, because documentation is what you give to others to explain how to use your system, rather than how it does things.

Edit: I (and probably everyone else) should probably have the provision that a Digital Signal Processing (DSP) app should be very well commented. That's mainly because DSP apps are essentially 2 for loops fed with arrays of values and adds/multiplies/etc said values... to change the program you change the values in one of the arrays... needs a couple of comments to say what you are doing in that case ;)

So a function or variable name will provide enough trivially clear context to explain why one implementation was chosen over another given two or more equally correct ways to solve a problem?
Scott Dorman
+10  A: 

I forget where I got this from, but:

Every comment in a program is like an apology to the reader. "I'm sorry that my code is so opaque that you can't understand it by looking at it". We just have to accept that we are not perfect but strive to be perfect and go right on apologizing when we need to.

Rubbish. Good comments in code absolutely have their place. Take the example of choosing between two equally correct methods to solve a problem. A comment explaining why one method was chosen over the other is extremely meaningful and you can never get that from the code itself.
Scott Dorman
If there are two EQUALLY CORRECT methods, then does it matter why you chose one over the other?
Yeah, equally correct does not mean exact the same. One method may be faster in some situations than another.
THen if your decision criteria is speed, they are not EQUALLY CORRECT. I'm not saying that comments are bad. Just that they are required because there is currently no programming language that is so clear an unambiguous that anyone can look at the code and instantly know the code's intent.
EBGreen: But what if you want to change the code? Then the other method might make more sense, and this could be explained, in short, in comments
I don't think people uderstand that quote the same way that I do. I take it to mean that you should strive to always write code that is so clear that it doesn't need to be commented, but that you have to accept that it is an ideal that never works in reality.
I don't think there's the need for two equally correct methods in any situation... See java collections for example, would you like to have 5-7 sort() methods, each of them EQUALLY CORRECT?
Pablo Fernandez
correct != (performant || understandable || extensible)
+1  A: 

I believe that you should always strive to achieve self-documenting code because it does make it easier to read code. However, you have also got to be pragmatic about things.

For example, I usually add a comment to every class member (I use documentation comments for this). This describes what the member is supposed to do but not how it does it. I find that when I am reading through code, particularly old code, this helps me remember quickly what the member is for and I also find it easier than reading the code and working it out, particularly if the flow of code jumps around quite a bit.

This is just my opinion. I know of plenty of people who work without comments at all and say that they find this to be no problem. I have, however, asked somebody about a method they wrote six months previous and they have had to think for a few minutes to tell me exactly what it did. This isn't a problem if the method is commented.

Finally, you have to remember that comments are equally part of the system as code. As you refactor and change functionality you must also update your comments. This is one argument against using comments at all as they are worse than useless if they are incorrect.

+24  A: 

The idea behind "self-documenting" code is that the actual program logic in the code is trivially clear enough to explain to anyone reading the code not only what the code is doing but why it is doing it.

In my opinion, the idea of true self-documenting code is a myth. The code can tell you the logic behind what is happening, but it can't explain why it is being done a certain way, particularly if there is more than one way to solve a problem. For that reason alone it can never replace well commented code.

Scott Dorman
+3  A: 

Self documenting code is an easy opt out of the problem, that over time code, comment and documentation diverge. And it is a disciplining factor to write clear code (if you are that strict on yourself).

For me, these are the rules I try to follow:

  • Code should be as easy and clear to read as possible.
  • Comments should give reasons for design decisions I took, like: why do I use this algorithm, or limitations the code has, like: does not work when ... (this should be handled in a contract/assertion in the code) (usually within the function/procedure).
  • Documentation should list usage (calling converntions), side effects, possible return values. It can be extracted from code using tools like jDoc or xmlDoc. It therefore usually is outside the function/procedure, but close to the code it describes.

This means that all three means of documenting code live close together and therefore are more likely to be changed when the code changes, but do not overlap in what they express.

Ralph Rickenbach
+66  A: 

In my opinion, any code should be self-documenting. In good, self-documented code, you don't have to explain every single line because every identifier (variable, method, class) has a clear semantic name. Having more comments than necessary actually makes it harder (!) to read the code, so if your colleague

  • writes documentation comments (Doxygen, JavaDoc, XML comments etc.) for every class, member, type and method AND
  • clearly comments any parts of the code that are not self-documenting AND
  • writes a comment for each block of code that explains the intent, or what the code does on a higher abstraction level (i.e. find all files larger than 10 MB instead of loop through all files in a directory, test if file size is larger than 10 MB, yield return if true)

his code and documentation is fine, in my opinion. Note that self-documented code does not mean that there should be no comments, but only that there should be no unnecessary comments. The thing is, however, that by reading the code (including comments and documentation comments) should yield an immediate understanding of what the code does and why. If the "self-documenting" code takes longer to understand than commented code, it is not really self-documenting.

In line with this answer, I think:
Point nr. 3 should be part of point nr. 1 IMHO, if a method is so complex that it requires high abstraction comments for several blocks of code, each such block of code should be a new method.
+1 for "does not mean that there should be no comments", which seems to be some people's opinion.
+1  A: 

I think its a matter of the right amount of documentation, rather than all or none. If the parameters to a function are well named, you often don't have to say exactly what they are, e.g. char *CustomerName is pretty obvious. If you use assert value ranges for parameters, you don't have to document those ranges as well. IMO, documentation should cover everything which is less than obvious and hence needs some explanation, and most code needs some documentation. Personally, I'd rather see an illustrative example of how a given function works than descriptive documentation, in most cases.

Documentation for documentations sake can be a waste of time, as the documentation needs maintenance to be kept up to date with the code base. If no one will benefit from reading it, don't produce it.

Shane MacLaughlin
+3  A: 

The real problem with the so-called self-documenting code is that it conveys what it actually does. While some comments may help someone understand the code better (e.g., algorithms steps, etc.) it is to a degree redundant and I doubt you would convince your peer.

However, what is really important in documentation is the stuff that is not directly evident from the code: underlying intent, assumptions, impacts, limitations, etc.

Being able to determine that a code does X from a quick glance is way easier than being able to determine that a code does not do Y. He has to document Y...

You could show him an example of a code that looks well, is obvious, but doesn't actually cover all the bases of the input, for example, and see if he finds it.

+1  A: 

I'd turn this around.

Ask yourself what you don't understand in his code and then ask him to document those. And maybe you could also tell us some.

+3  A: 

One thing that you may wish to point out to your colleague is that no matter how self-documenting his code is, if other alternate approaches were considered and discarded that information will get lost unless he comments the code with that information. Sometimes it's just as important to know that an alternative was considered and why it was decided against and code comments are most likely to survive over time.

Onorio Catenacci
You take this as a rule and you'll end up writing textbook in your codebase ;-) I agree for non-obvious decisions though.
@ddimitrov, that's a good observation. But as you say for non-obvious decisions (and those are usually the ones that really require documentation anyway) it's a good practice.
Onorio Catenacci
+3  A: 

For one, consider the following snippet:

 * Sets the value of foobar.
 * @foobar is the new vaue of foobar.
 public void setFoobar(Object foobar) {
     this.foobar = foobar;

In this example you have 5 lines of comments per 3 lines of code. Even worse - the comments do not add anything which you can't see by reading the code. If you have 10 methods like this, you can get 'comment blindness' and not notice the one method that deviates from the pattern.

If course, a better version would have been:

 * The serialization of the foobar object is used to synchronize the qux task.
 * The default value is unique instance, override if needed.
 public void setFoobar(Object foobar) {
     this.foobar = foobar;

Still, for trivial code I prefer not having comments. The intent and the overall organization is better explained in a separate document outside of the code.

It may seem trivial to document getters and setters, but I think there is a happy medium between having a useless comment and not having any comment at all. The point of alot of Javadoc comments is to inform someone who can't or doesn't have the inclination to look at the code in the method.
James McMahon

Self documented code is code that is so clear that a comment would be unnecessary. I'll give a small example:

//iterate from 0 to 100
for(int i=0; i < 100; i++) {
   println i

The comment is pretty useless, because the code is clear. Documentation is a good practice, but extra documentation can add unecessary noise to the code. What your colleague needs to know is that not everyone can read other's code and acknowledge all it's details.

int calc(int a, int b) {
   return sqrt(a*a + b*b); //pythagoras theorem

The last snippet would be extra hard to decipher without the comment. You can imagine other examples that are more contrived.

Miguel Ping
I think you mean "Pythagorean" theorem? Why not just name the function that?
That first comment is not only redundant, it can also be argued that it is wrong because it does not state the side-effect that something is getting printed - even though this particular example is rather contrived.
Christian Vest Hansen
The first comment is also wrong since it iterates from 0 to 99. A fate too many comments experiences.
The second example should be something like "int calcHypotenuseLength(int side1, int side2)" or "int pythagoreanHypotenuseLength(...)". Self-documenting.
calcHypotenuseLength conveys the algorithm. This violates information hiding. Let the implementation keep track of that, maybe a method called vektorSum(int xVektor, int yVektor) if the mathematical properties are important, or something closer to the domain like distance(int pointX, int pointY)
John Nilsson
in hindsight, calcGypotenuseLength and vekrotSum is equal in this regard... i was thinking of the pythagorean part...
John Nilsson
In fact, I think this is an example of someone who thinks they can write self-documenting code but can't!
+194  A: 

Well, since this is about comments and code, let's look at some actual code. Compare this typical code:

float a, b, c; a=9.81; b=5; c= .5*a*(b^2);

To this self-documenting code, which shows what is being done:

const float gravitationalForce = 9.81;
float timeInSeconds = 5;
float displacement = (1 / 2) * gravitationalForce * (timeInSeconds ^ 2)

And then to this documented code, which better explains why it is being done:

/* compute displacement with Newton's equation x = vₒt + ½at² */
const float gravitationalForce = 9.81;
float timeInSeconds = 5;
float displacement = (1 / 2) * gravitationalForce * (timeInSeconds ^ 2)

And this example of a poor commenting style:

const float a = 9.81; //gravitational force
float b = 5; //time in seconds
float c = (1/2)*a*(b^2) //multiply the time and gravity together to get displacement.

In the last example, comments are used when variables should have been descriptively named instead, and the results of an operation are summarized when we can clearly see what the operation is. I would prefer the self-documented second example to this any day, and perhaps that is what your friend is talking about when he says self-documented code.

I would say that it depends on the context of what you are doing. To me, the self-documented code is probably sufficient in this case, but a comment detailing the methodology behind what is behind done (in this example, the equation) is also useful.

That entire chunk should really be in a function with a descriptive name though ;)
Agreed, but still you can't put the math equation into the Method name. So a comment helps in any case
Yeah, the function displacementDueToGravity(int timeInSeconds, float gravitationalAcceleration = 9.81) would be easier for me to read.
Cristián Romo
Also, trying to store a floating point number in a const int is kinda silly - you lose a good deal of precision.
Cristián Romo
Yeah, oops. I fixed it. Good thing I don't work for NASA...
The one comment I miss here is: Why 5 seconds?
John Nilsson
Another vote for the descriptive function name. It doesn't give the equation itself but I don't see that that is needed.
Loren Pechtel
"you can't put the math equation into the Method name"The equation is in the method body, with descriptive variable names instead of x, v, a, and t. (BTW, what happened to v0t?)
Patrick McElhaney
What are the units of gravitational force? There are limits to how much you can add to a variable name. At some point you have to explain *what you are trying to do*. Often this is *not obvious*, which is why you need to comment code. It's absolute rubbish to say code is self documenting, it is just self *descriptive*.
This <em>really</em> needs to be put in a function with a descriptive name. At that point it becomes truly self documenting! In fact every time you find yourself writing a comment to explain a block of code, that is a clear sign that you need to extract that block of code into a method with a telling name. Even if you only ever call that method once.
Yeah, but now look at this perfect example of self-documentation:
@gbjbaanb: yeah, you can go overboard with the idea. I could have called the first variable "gravitationalForceInKilogramMetersOverSecondsSquared" But in general the principal still stands.
@Nick: Units! Please, units on all variable names! timeoutSec or timeoutMs accurately describes the variable, while just timeout is going to be a pager call when you have 1000 threads waiting 2000 seconds, not ms, to timeout.
In some languages, (1 / 2) will use the values as integers and end up being 0 due to truncation errors. You can avoid that by doing (1.0 / 2.0) or just use 0.5
+7  A: 

In order:

  • Self-documenting code is code that clearly expresses its intent to the reader.
  • Not entirely. Comments are always helpful for commentary on why a particular strategy was chosen. However, comments which explain what a section of code is doing are indicative of code that is insufficiently self-documenting and could use some refactoring..
  • Comments lie and become out of date. Code always tells is more likely to tell the truth.
  • I've never seen a case where the what of code couldn't be made sufficiently clear without comments; however, like I said earlier, it is sometimes necessary/helpful to include commentary on the why.

It's important to note, however, that truly self-documenting code takes a lot of self- and team-discipline. You have to learn to program more declaratively, and you have to be very humble and avoid "clever" code in favor of code that is so obvious that it seems like anyone could have written it.

I'll add a nit pick here. Code does not always "tell the truth". Code can be misleading and obfuscate its intentions very easily. For example, a misnamed variable or method can lie just as much as an out of date comment.
Point taken. Edited.
+1  A: 

Here are my best answers to your questions.

Self documenting code is code that's written clearly with class, method, function, and variable names that make it's intent and function easily understood. If done well, it is the documentation.

It can replace code that's well commented and documented, but I've rarely seen it. Too many times, programmers believe that they are good enough to do this, but the best way to knock them down is to start asking questions. If they have to start explaining too much, then their code wasn't clear enough. You shouldn't have to read the code to know what it does.

There may be some situations where it's better. If the code is small and simple, then you may clutter things up by adding documentation.

Code that includes an algorithm should contain comments. Most times, even the original programmers can remember what the heck they were thinking a few months ago when they wrote a long function.

+12  A: 

self-documenting code is a good practice and if done properly can easily convey the meaning of the code without reading too many comments. especially in situations where the domain is well understood by everyone in the team.

Having said that, comments can be very helpful for new comers or for testers or to generate documentation/help files.

self-documenting code + necessary comments will go a long way towards helping people across teams.

+1  A: 

This is an excellent question. It traces back to the first programming language that allowed comments, I'm sure. The code certainly should be as self-documenting as possible. Comments that point out the obvious, should be eliminated. Comments that make it easier to understand the intent, purpose, and use of a given method or section of code can be invaluable to those of us dolts that may be less familiar with the language or code in question. Structured comments that allow for the generation of API documentation are a good example. Just don't comment an IF statement that checks to see if a checkbox is checked and tell me that you're checking to see if the checkbox is checked. Restating the obvious in a comment is the worst waste keystrokes in our universe.

//For example, the above text deals with what is a useful comment
Tyler Jensen
+10  A: 

First of all, it's good to hear that your colleague's code is in fact clearer than other code you have seen. It means that he's probably not using "self-documenting" as an excuse for being too lazy to comment his code.

Self-documenting code is code that does not require free-text comments for an informed reader to understand what it is doing. For example, this piece of code is self-documenting:

print "Hello, World!"

and so is this:

factorial n = product [1..n]

and so is this:

from BeautifulSoup import BeautifulSoup, Tag

def replace_a_href_with_span(soup):
    links = soup.findAll("a")
    for link in links:
        tag = Tag(soup, "span", [("class", "looksLikeLink")])
        tag.contents = link.contents

Now, this idea of an "informed reader" is very subjective and situational. If you or anyone else is having trouble following your colleague's code, then he'd do well to re-evaluate his idea of an informed reader. Some level of familiarity with the language and libraries being used must be assumed in order to call code self-documenting.

The best argument I have seen for writing "self-documenting code" is that it avoids the problem of free-text commentary not agreeing with the code as it is written. The best criticism is that while code can describe what and how it is doing by itself, it cannot explain why something is being done a certain way.

Steven Huwig
+1  A: 

Self documenting code is code that is trivially easy to understand. Variable naming goes a long way to making code self documenting, but i find the best tactic is to break any complicated logic down into tiny little chunks and refactor that information into seperate methods with verbose and informative names. Then your complicated methods become simply a list of steps to be performed. The tiny private helper methods then are documented sufficiently by their own method name and the complicated methods are documented as a sequence of abstract steps to be performed. In practice this strategy cannot always be applied perfectly so comments are still very useful. Plus you should never completely abandon any tool which will help you write code that is easier to understand.

Spencer Stejskal
+45  A: 

Someone once said

1) Only write comments for code that's hard to understand.
2) Try not to write code that's hard to understand.

What seems trivial for you to understand at the time of writing the code may in fact be very hard for someone else to understand later, even if that someone else is in fact yourself in a few months/years.
Anders Sandvig
I often find things I wrote on Friday pretty tough to grok on Monday morning :)
which leads us to "try not write comments"
Mustafa A. Jabbar
+1  A: 

The point of view that code is self documenting drives me crazy. A particular line of code or a sub algorithm may be indeed self documenting but it's purpose in the greater picutre simply is not.

I got so frustrated with this a month or two ago I wrote an entire blog post describing my point of view. Post here.

+6  A: 

Self-documenting code is a good example of "DRY" (Don't Repeat Yourself). Don't duplicate information in comments which is, or can be, in the code itself.

Rather than explain what a variable is used for, rename the variable.

Rather than explain what a short snippet of code does, extract it into a method and give it a descriptive name (perhaps a shortened version of your comment text).

Rather than explain what a complicated test does, extract that into a method too and give it a good name.


After this you end up with code that doesn't require as much explanation, it explains itself, so you should delete the comments which merely repeat information in the code.

This doesn't mean you have no comments at all, there is some information you can't put into the code such as information about intent (the "why"). In the ideal case the code and the comments complement each other, each adding unique explanatory value without duplicating the information in the other.

One exception: bad programmers. I've seen comments say code is doing something that it's not. Then I ask myself: should I fix the code or the comment?
+1  A: 

When writing mathematical code, I have sometimes found it useful to write long, essay-like comments, explaining the math, the notational conventions the code uses, and how it all fits together. We're talking hundreds of lines of documentation, here.

I try to make my code as self-documenting as possible, but when I come back to work on it after a few months, I really do need to read the explanation to keep from making a hash out of it.

Now, of course this kind of extreme measure isn't necessary for most cases. I think the moral of the story is: different code requires different amounts of documentation. Some code can be written so clearly that it doesn't need comments -- so write it that clearly and don't use comments there!

But lots of code does need comments to make sense, so write it as clearly as possible and then use as many comments as it needs...

+2  A: 

Have you heard of Donald Knuth's "WEB" project to implement his Literate Programming concept? It's more than self-documenting code; it's more like documentation that can be compiled and executed as code. I don't know how much it is used today though.

This has certainly influenced the development of test-driven development and of behaviour-driven development (especially technologies like FIT and Fitnesse). So in a highly specialized way it's standard practice for a lot of people.
+1  A: 

A couple of reasons why extra comments in addition to the code might be clearer:

  • The code you're looking at was generated automatically, and hence any edits to the code might be clobbered the next time the project is compiled
  • A less-than-straightforward implementation was traded off for a performance gain (unrolling a loop, creating a lookup table for an expensive calculation, etc.)
John at CashCommons
+1  A: 

Its going to be all in what the team values in its documentation. I would suggest that documenting why/intent instead of how is important and this isn't always captured in self documenting code. get/set no these are obvious - but calculation, retrieval etc something of the why should be expressed.

Also be aware of difference in your team if you are comming from different nationalities. Differences in diction can creap into the naming of methods:




These three methods contributed from developers trained on 3 different continents do the same thing. Only by reading the comments that described the algorithm were we able to identify the duplication in our library.

+1  A: 

If your code isn't completely clear without comments, then there's room to improve your code.

I'm not saying "don't comment unclear code". I'm saying "make your code clear".

If you end up leaving your code unclear in some way, then use comments to compensate.

Jay Bazuzi

Self documenting code is silliness. Anyone who's had to re-visit their code after weeks, months or gasp years knows that (or days, in my case). (Perhaps the guy who is promoting this idea is still wet behind the ears!?!?!)

Use meaningful, descriptive data names, factor your code intelligently, and leave yourself hints as to why the heck you did what you did and you will live a richer, more fullfilling life.

Although...I did read a quote once attributed to Bill Gates: "The code IS the documentation."

Go figure.

I disagree on the "Self documenting code is silliness thing". "Use meaningful, descriptive data names, factor your code intelligently" - Is part of self documenting code.
True, but that wasn't my point, which is wrapped up in the "and leave yourself hints as to why the heck you did what you did" comment (no pun intended).
+4  A: 

I think it's relevant to question whether a particular line of code is self-documenting, but in the end if you do not understand the structure and function of a slice of code then most of the time comments are not going to help. Take, for example, amdfan's slice of "correctly-commented" code:

/* compute displacement with Newton's equation x = v0t + ½at^2 */
const float gravitationalForce = 9.81;
float timeInSeconds = 5;
float displacement = (1 / 2) * gravitationalForce * (timeInSeconds ^ 2);

This code is fine, but the following is equally informative in most modern software systems, and explicitly recognizes that using a Newtonian calculation is a choice which may be altered should some other physical paradigm be more appropriate:

const float accelerationDueToGravity = 9.81;
float timeInSeconds = 5;
float displacement = NewtonianPhysics.CalculateDisplacement(accelerationDueToGravity, timeInSeconds);

In my own personal experience, there are very few "normal" coding situations where you absolutely need comments. How often do you end up rolling your own algorithm, for example? Basically everything else is a matter of structuring your system so that a coder can comprehend the structures in use and the choices which drove the system to use those particular structures.

Mike Burton
+2  A: 

I'm surprised that nobody has brought about "Literate Programming", a technique developed in 1981 by Donald E. Knuth of TeX and "The Art of Computer Programming" fame.

The premise is simple: since the code has to be understood by a human and comments are simply thrown away by the compiler, why not give everyone the thing they need - a full textual description of the intent of the code, unfettered by programming language requirements, for the human reader and pure code for the compiler.

Literate Programming tools do this by giving you special markup for a document that tells the tools what part should be source and what is text. The program later rips the source code parts out of the document and assembles a code file.

I found an example on the web of it: or the HTML version

If you can find Knuth's book on it in a library (Donald E. Knuth, Literate Programming, Stanford, California: Center for the Study of Language and Information, 1992, CSLI Lecture Notes, no. 27.) you should read it.

That's self-documenting code, complete with reasoning and all. Even makes a nice document, Everything else is just well written comments :-)

That's actually the opposite of self-documenting code. Text for the human, code for the machine. And what language should the code be? Assembly of course. Humans don't need to read it, right? They only need to write it!
+1  A: 

Some perspectives from the non-commenting camp.

"well commented" (verbose) code is harder to read and understand. For one thing, there is simply more text to scan. It increases the cognitive effort in understanding a CodeBase - the nonfunctional text takes up screen space that could be used to show code.

Another big problem with comments is that they are unreliable - especially on older code bases, comment rot sets in faster than bit rot.

And then of course there is the effort involved in writing comments. With the exception of the occasional one line clarifier, every time I start commenting code I get one of two guilty feelings

  1. this info needs to go in overall supporting documentation
  2. I need to clean up my code
Scott Weinstein

I always use System.out to explain what code does. That way, you can print it off and read it at home :p

+1  A: 

For me reading code that needs comments is like reading text in the language I do not know. I see statement and I do not understand what it does or why - and I have to look at comments. I read a phrase and I need to look in dictionary to understand what it means.

It is usually easy to write code that self-documents what it does. To tell you why it does so comments are more suitable, but even here code can be better. If you understand your system on every level of abstraction, you should try organizing you code like

public Result whatYouWantToDo(){
  return resultOfWhatYouHavDone;

Where method name reflects your intent and method body explains how you achieve your goal. You anyway can not tell entire book in its title, so main abstractions of your system still have to be documented, as well as complex algorithms, non-trivial method contracts and artifacts.

If the code that your colleague produc is really self-documented - lucky you and him. If you think that your colleagues code needs comments - it needs. Just open the most non-trivial place in it, read it once and see if you understood everything or not. If the code is self-documented - then you should. If not - ask your colleague a question about it, after he gives you an answer ask why that answer was not documented in comments or code beforehand. He can claim that code is self-document for such smart person as him, but he anyway has to respect other team members - if your tasks require understanding of his code and his code does not explain to you everything you need to understand - it needs comments.

Pavel Feldman
+2  A: 

I would argue - as many of you do - that to be truly self documenting, code needs to show some form of intent. But I'm surprised nobody mentioned BDD yet - Behavior Driven Development. Part of the idea is that you have automated tests (code) explaining the intent of your code, which is so difficult to make obvious otherwise.

Good domain modeling 
+ good names (variabes, methods, classes) 
+ code examples (unit tests from use cases) 
= self documenting software 

Regardless of purely self-documenting code is achievable, there are some things that come to mind one should do anyway:

  • Never have code that is "surprising". Ie. don't use silly macro's to redefine things etc. Don't misuse operator overloading, don't try to be smart on this.
  • Split away code at the right point. Use proper abstractions. Instead of inlining a rolling buffer (a buffer with fixed length, with two pointers that gets items added at one end and removed at the other), use an abstraction with a proper name.
  • Keep function complexity low. If it gets too long or complex, try to split it out into other other functions.

When implementing specific complex algorithms, add documentation (or a link to) describing the algorithm. But in this case try to be extra diligent in removing unneeded complexity and increasing legibility, as it is too easy to make mistakes.

Paul de Vrieze

Very mixed inputs here it seems :)

I use the Pseudo code Programming Process for new developments, which virtually makes my code self documenting. I start to write Pseudo code only when writing new code then extend on it. Im not saying this is best practice or anything like that, i'm just highlighting one technique I find useful if you know you want a lot of documentation for your code if its going to a third party, reviewer, etc... it also occasionally highlights problems for me before ive even written a line of code.

' check database is available
  ' if it is then allow the procedure
  ' if it isnt roll back and tidy up 
' move onto something else


' check database is available
  if checkDBStateResult(currentDB) = Open then 
     ' if it is then allow the procedure
          proc.Ok = True 
     ' if it isnt roll back
          proc.Ok = False
  end if

Most documentation / comments serve towards assisting future code enhancers / developers hence making the code maintainable. More often than not we would end up coming back to our module at a later time to add new features or optimize. At that time it would be easier to understand the code by simply reading the comments than step through numerous breakpoints. Besides i would rather spend time thinking for new logic than deciphering the existing.

+2  A: 

When you read a "self-documenting code", you see what it is doing, but you cannot always guess why it is doing in that particular way.

There are tons of non-programming constraints like business logic, security, user demands etc.

When you do maintenance, those backgorund information become very important.

Just my pinch of salt...

+1  A: 

//if it's no biggie, don't fret about it.

//If you write comments all over, the important ones will not be seen

  • Method Parameter comments: Must die. This is code duplication.

//Parameters should be self-explanatory.

  • WTF-factor imagination: Will someone, including myself, say WTF to this?
Methods take in many times business related data, so that data should be documented somewhere.

I once worked with a guy who was about to sell a financial suite to a large company. They insisted he document the source code, to which he produced a 30+ page assembler routine and said 'it is documented, look' - then he flipped to page 13 and there was a comment 'bump counter by one'. Great product, great implementor, but...

Anyway to my mind the inportant comments above are to set the context. This snippet was stated as self-documented:

> from BeautifulSoup import
> BeautifulSoup, Tag def
> replace_a_href_with_span(soup):
>     links = soup.findAll("a")
>     for link in links:
>         tag = Tag(soup, "span", [("class", "looksLikeLink")])
>         tag.contents = link.contents
>         link.replaceWith(tag)

But, I for one, need a context to understand it fully.


The point has been made that comments should capture intent, but I would go a little further.

I think for any class of problems, there is an ideal (or nearly so) vocabulary and syntax to describe it, and you can see it if you just ask the person who has such problems to describe them (assuming that person can think clearly).

If that vocabulary and syntax maps easily (by defining classes, methods, etc.) onto code in a computer language, then that code can be self-documenting. Also, IMO, a domain-specific language has been created. (And that is my rough-hewn definition of "declarative".)

Failing this ideal, if the problem does not map so directly onto the computer code, then you need something to link the two together. IMO, that is the purpose of comments.

That way, when the problem changes, you can find the corresponding parts of the code to change.

EDIT: I am not, by the way, arguing in favor of the OOP methodology where every noun becomes a class and every verb a method. I've seen enough bloatware built that way.

Mike Dunlavey
+1  A: 

I would like to offer one more perspective to the many valid answers:

What is source code? What is a programming language?

The machines don't need source code. They're happy running assembly. Programming languages are for our benefit. We don't want to write assembly. We need to understand what we are writing. Programming is about writing code.

Should you be able to read what you write?

Source code is not written in human language. It has been tried (for example FORTRAN) but it isn't completely successful.

Source code can't have ambiguity. That's why we have to put more structure in it than we do with text. Text only works with context, which we take for granted when we use text. Context in source code is always explisit. Think "using" in C#.

Most programming languages have redundancy so that the compiler can catch us when we aren't coherent. Other languages use more inference and try to eliminate that redundancy.

Type names, method names and variable names are not needed by the computers. They are used by us for referencing. The compiler doesn't understand semantics, that's for us to use.

Programming languages are a linguistic bridge between man and machine. It has to be writable for us and readable for them. Secondary demands are that it should be readable to us. If we are good at semantics where allowed and good at structuring the code, source code should be easy to read even for us. The best code doesn't need comments.

But complexity lurks in every project, you always have to decide where to put the complexity, and which camels to swallow. Those are the places to use comments.


Good design structure helps to point out that some functions are for general use and some for random business logic even though you don't have a comment saying "this function is general".

We should not forget about design and specification documentation though. Those have or at least should have much of the texts that are not necessarily needed in comments. Software often have also user manuals and other description documents, and those should be in sync with what the program does. The situation is not great if the user has to find out what the software does from the source code instead of a manual. So self documenting code still doesn't mean that the actual software has been documented.

Think about traceability of features, too. When you have your manual, then you should be able to trace the features to source code and back for better maintainability. Manuals and specifications are not that much about programming, but they are about software engineering. The bigger the software, the more engineering is needed.

+2  A: 

The difference is between "what" and "how".

  • You should document "what" a routine does.
  • You should not document "how" it does it, unless special cases (e.g. refer to a specific algorithm paper). That should be self-documented.
Stefano Borini

In a company where I worked one of the programmers had the following stuck to the top of her monitor.

"Document your code like the person who maintains it is a homocidal maniac who knows where you live."