in reply to Documenting Methods/Subs

All methods must be documented, even if it's simply a
=head2 returnName =cut
how else would know what your API consists of? (it shouldn't be magic).

Comments are not documentation -- or comments are documentation for the developers of the API. The users of the API should refer to the user documentation (the pod).

Comments needn't truly be comments (=for comment ...=begin comment ...)

I heartily disagree Abigail's comment that people who document every sub are wrong. There is no such thing as too much documentation, no matter how redundant it may be.

Lack of documentation is almost always wrong (programmers should not resort to sourcediving to learn function names).


MJD says you can't just make shit up and expect the computer to know what you mean, retardo!
** The Third rule of perl club is a statement of fact: pod is sexy.

Replies are listed 'Best First'.
Re: Re: Documenting Methods/Subs
by Tanalis (Curate) on Jan 10, 2003 at 17:33 UTC
    There is no such thing as too much documentation, no matter how redundant it may be.

    It can get to the extreme, very easily. Consider a simple sub to commify a number, called commify_number. There's no need for this to have a description of what it does - it's obvious. Large blocks of comments (or comments documenting most of the lines of code within the sub) for something like this are going to obscure what should be a relatively simple thing.

    My thoughts: if code is getting lost in a sea of comments, there's too many comments. Redundancy is largely irrelevant if the comments themselves are stopping you seeing what's going on.

    While I agree that a lack of docs is bad, it's very important to get some sort of balance between code usability (in a maintenence sense) and understanding, at least in my mind.

    -- Foxcub

Re: Re: Documenting Methods/Subs
by ihb (Deacon) on Jan 10, 2003 at 21:13 UTC
    I agree, but with some clarifications.

    When speaking of documentation, I mean things that document the particular module, philosophy, code snippet, or whatever the documentation is related to. If someone starts documenting how pack() works in the middle of the code or API then he's just lost track of what he's doing.

    However, there seem to be couple of misunderstandings here between fellow monks. Some speak of documentation, some speak of commentation, and some speak of commentation as documentation. Naturally, disagreements will arise.

    I usually wouldn't refer to comments as documentation. If someone says "look in the documentation" I certainly don't look for comments. I say that there can be too many comments, but too much documentation has never hurt me--as long as it has a sensible structure. If you end up with a long documentation with lots of examples and explanations in it you'd perhaps should start thinking about writing a separate tutorial or extract the examples to an own section. Has anyone ever complained about too many examples?

    For modules this means that you first document the most important things so people quickly can use your module. But towards the end of the documentation you very well can put a more detailed explanation and give examples to illustrate special features, nifty uses, or gotchas. If it's a "funky" module, i.e. it does something technically advanced with Perl itself (constant.pm to take a well-known module) it can be interesting with a little explanation or forward to documents explaining how the module achieves what it does so the reader gets a chance to learn and understand rather than being a simple user. This certainly can help the module author with finding bugs or even receiving patches. It also feeds for further development and other related and interesting ideas. So no, I wouldn't say that there can be too much documentation. (Remember that I don't count a pack() tutorial in the middle of code as documentation.)

    Just my thoughts,
    ihb
Re: Re: Documenting Methods/Subs
by vek (Prior) on Jan 10, 2003 at 17:49 UTC
    how else would you know what your API consists of?(it shouldn't be magic)

    I agree, and that is what POD is for. Remember that a lot of programs/modules have POD at the very end of the code (I've seen my fair share from the CPAN). As a programmer who wishes to learn the API I can do a quick perldoc foo and (hopefully) get everthing I need. I was referring to code where every method is documented by the type of comments I showed in my root node, including those private or debug methods of a class which won't be in the POD.

    -- vek --
Re: Re: Documenting Methods/Subs
by autarch (Hermit) on Jan 11, 2003 at 05:16 UTC

    I heartily disagree Abigail's comment that people who document every sub are wrong. There is no such thing as too much documentation, no matter how redundant it may be.

    Abigail's a pretty smart person, and I think the slip from "comment" to "document" was unintentional. Yes, you should always document your whole public API. Otherwise it's not really public, is it?

    But don't waste my limited screen real estate on crap like this:

      #
      # Class->foo() - returns the value of foo for the object
      #
      sub foo { $_[0]->{foo} }
    
    Another commenting pet peeve:
      # if $x is bigger than 10
      if ( $x > 10 ) {
          # set $x to $y modulus 50
          $x = $y % 50;
      }
      # otherwise, if $y is not equal to 50
      elsif ( $y != 50 ) {
          ...
      }
    
    Argh! I hate that so incredibly much. Comments that simply tell me _what_ the code is doing are absolutely, utterly useless. Always assume that the next person does understand programming logic, assume that they understand Perl, etc.

    Don't assume they know why something is the way it is, however. In the above code, I want to know why it's important to set $x to the $y modulus 50 only when $x is greater than 10.

    There are a few exceptions to the "no walk-through comments" rule. Very complicated algorithms may deserve very careful step by step comments. This can help you as you code, in making sure that you're actually following the algorithm, and it helps future readers. And if you deliberately write less-than-clear code, for example as an optimization (after benchmarking and profiling, please), then it may be worthwhile to comment the what of the code.

    But a good rule to follow is that if you think the what of the code is too hard to follow without comments, then you need to rewrite the code in a clearer manner.

Re: Re: Documenting Methods/Subs
by Anonymous Monk on Jan 10, 2003 at 17:36 UTC
    There is no such thing as too much documentation, no matter how redundant it may be.

    Whew! Obviously you haven't seen the code I have.

    use warnings; # turn warnings on use CGI; # use Stein's CGI.pm for parsing params my $q = new CGI; # create the CGI.pm instance # get the customer's name (in the name parameter) # place in in variable $customer_name my $customer_name = $q->param("name") # if the customer's name is "Bobby" # print "Hi Bobby" if ($customer_name eq "Bobby") { print "Hi Bobby\n"; } else { # if not, print "You're not Bobby" print "You're not Bobby\n"; }

    Get the point? This completely distracts from the code and takes way more time on the part of the programmer.

    As for subs, I say one comment directly above them. Anymore and the sub is too long and should be broken into multiple subs.

Re^2: Documenting Methods/Subs
by adrianh (Chancellor) on Jan 12, 2003 at 11:56 UTC
    There is no such thing as too much documentation, no matter how redundant it may be. Lack of documentation is almost always wrong (programmers should not resort to sourcediving to learn function names).

    Lack of needed documentation is always wrong - however over documentation, in my experience, can cause problems. For example:

    • Excessive documentation/comments can be an excuse for poor code. Rather than spend the effort to produce comprehensible code certain developers will write a long and complicated piece of text to justify their technique. It sounds weird I know, but I've come across it enough times for it to become a personal anti-pattern I keep an eye out for.
    • People change code and do not change the comments/documentation. The more redundent documentation there is the worse this problem gets.
    • Documenting to early. If you're in an environment where the code is changing rapidly, methods and classes are being refactored and moved around on a daily basis, etc. then time spent documenting and commenting is wasted time.

    Personally, I tend to go for development environment with common code ownership and require unit tests for everything. I find this removes most of the need for comments and heavy API documentation during development.

    If the team discovers a need for documentation during development then we do it. If the team is not in-house then we will also have appropriate levels of design & implementation documentation as a deliverable (and we'll do it properly too :-)

    This has worked well for me with smallish projects (about a dozen coders max).

Re: Re: Documenting Methods/Subs
by pdcawley (Hermit) on Jan 13, 2003 at 12:10 UTC
    All methods must be documented
    Round objects!

    And so do I. Whilst I take your point that it's generally a good idea to document the methods that make up your public API, those methods will not be the same as the full set of methods to which an object responds.

    Anyone who works with your code needs to see documentation covering how you expect the code to be used (though I'm as guilty as the next man of failing to do this on one of the major projects I'm working on, partly because the API is still in flux). Anyone who works on your code should be using the source like God intended. And if you're not competent to write clear code then what on earth makes you think you're competent to write clear comments in a far more ambiguous language?

Re: Re: Documenting Methods/Subs
by hardburn (Abbot) on Jan 13, 2003 at 16:45 UTC

    There is no such thing as too much documentation, no matter how redundant it may be.

    Yes there is. I'm working on replacing some programs right now that stop every 5 lines or so with 20 lines of comments. Just document the subs if it's not obvious from their names, then document anything you do that's clever but obscure. Anything more gets in the way of following the code, which is The Ultimate Documentation.