469,589 Members | 2,222 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,589 developers. It's quick & easy.

How to comment code?

Hi all,

I've been toying with python for about two years now. Not every day,
just when I encounter something in my job (sysadmin) repetitively dull.
The amazing thing is that like any other language (natural or not)
learning it more gives you power to express your thoughts better and
create something from nothing, for me this is something I can only
compare to freedom.

However since I'm learning more of python I've struggled with
commenting, how should I've comment my code? I'm not talking about the
style but more on the concept itself, things that where a couple of
month ago a bunch of monkey poop is now as easy as reading a comic.

I always give a brief description on what the code is supposed to do and
I suppose that any serious coder knows way more then me about
programming so I don't bother to comment that much since it mostly (in
my eyes) just clutters up the code and makes it actually harder to read.

Though this makes me uncomfortably, commenting so little, I was thinking
that perhaps something like doctest (I'm not so much into unit testing
or the equivalents at this moment) has the side affect to make my code
more understandable and readable. Any practical experience you'd like to
share with me, any other comments are welcome too of course :-)

Thanks.

--
mph
Jan 19 '07 #1
3 2233
Martin P. Hellwig wrote:
Hi all,

I've been toying with python for about two years now. Not every day,
just when I encounter something in my job (sysadmin) repetitively dull.
The amazing thing is that like any other language (natural or not)
learning it more gives you power to express your thoughts better and
create something from nothing, for me this is something I can only
compare to freedom.

However since I'm learning more of python I've struggled with
commenting, how should I've comment my code? I'm not talking about the
style but more on the concept itself, things that where a couple of
month ago a bunch of monkey poop is now as easy as reading a comic.

I always give a brief description on what the code is supposed to do and
I suppose that any serious coder knows way more then me about
programming so I don't bother to comment that much since it mostly (in
my eyes) just clutters up the code and makes it actually harder to read.

Though this makes me uncomfortably, commenting so little, I was thinking
that perhaps something like doctest (I'm not so much into unit testing
or the equivalents at this moment) has the side affect to make my code
more understandable and readable. Any practical experience you'd like to
share with me, any other comments are welcome too of course :-)
I have found it useful to familiarize myself some tool such as epydoc
and write comments in the function doc-string. For example,

def doit(stuff, beta, cycles):
"""
Does nothing with I{stuff} in an infinite loop by applying a null
operation. The I{cycles} parameter is multiplied by infinity.

@param beta: null operator not to be applied to stuff
@type beta: callable
@type cycles: int
"""

This explains paramaters, etc., for you and other users of your API and
paves the way for automatically generated documentation. Note that it
would be excessive to comment each and every parameter, etc. Once the
code is working correctly, a lot of in-line comments can be removed.
Usually, in python, correctly working code is self explanatory at the
implementation level, especially if you attempt to code "pythonically"
(which can roughly be defined as the best practices according to the
python community or the community subset who post to comp.lang.python).

If you find that your code gets very complicated and needs excessive
commenting to understand, try to factor it into hierarchically related
simpler functions and classes and comment each individually using
doc-strings. In essence, think about commenting the interface rather
than the implementation.

Use module level doc-strings (comments) to gather usage and explanation
notes for the most useful functions. This allows users (for example,
you) to use your libraries without burrowing into the automatically
generated documentation for each and every function.

Other tools may be as useful as epydoc, but this is the general strategy
I have converged upon.

James
Jan 19 '07 #2
I think that doc strings are the most important way in which you should
be commenting on your code. Once the code works, you can elimainate
most inline comments, leaving only doc string for everything and a few
comments on some particularly confusing parts. Other than that,
comments usually only clutter Python code. But remember that you are
the person who understands your code best, so don't be too fanatical
about deleting inline comments.

Jan 19 '07 #3
If it's hard to write, it should be hard to read! :)

On 1/19/07, Martin P. Hellwig <mh******@xs4all.nlwrote:
Hi all,
(snip)
However since I'm learning more of python I've struggled with
commenting, how should I've comment my code
(snip)
Jan 20 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Rob | last post: by
15 posts views Thread by Riko Wichmann | last post: by
3 posts views Thread by Xah Lee | last post: by
1 post views Thread by Chuck | last post: by
39 posts views Thread by polas | last post: by
reply views Thread by suresh191 | last post: by
4 posts views Thread by guiromero | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.