473,799 Members | 3,390 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2411
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 "pythonical ly"
(which can roughly be defined as the best practices according to the
python community or the community subset who post to comp.lang.pytho n).

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******@xs4al l.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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
18800
by: Rob | last post by:
Hello all, If I have the following code fragment: /* comment bla bla */ ....code... With a regular expression, how do I get/extract the comment inside this multi line comment block. With and without the comment characters?
15
3536
by: Riko Wichmann | last post by:
Dear all, is there a way in Python to comment out blocks of code without putting a # in front of each line? Somethings like C's /* block of code here is commented out */ Thanks,
3
19504
by: Xah Lee | last post by:
is there a syntax to comment out a block of code? i.e. like html's <!-- comment --> or perhaps put a marker so that all lines from there on are ignored? thanks. Xah xah@xahlee.org http://xahlee.org/PageTwo_dir/more.html
1
1668
by: Chuck | last post by:
I am working with C# on Visual Studio 2003 SP1 on an XP professional SP2 system. All of the *.htm files generated by "Build Comment Web Pages" are flagged as restricted sites when I try to view them on IE 6.0. This problem occurred after installing the recent service packs. The only work around I have found is either to reduce the protection enforced on a restricted site or to manually replace the header line in every generated htm...
6
2426
by: cdrsir | last post by:
we can use 1) // my comments a 2) /* my comments b */ when we want to add some comments. I know some compilers just support the 2nd syntax, but normally both of these 2 syntaxs are supported by most of the compilers. But what are the difference for those two syntaxs in the sense for the compiler, if it support both. Is one of them will be processed faster?
0
3561
by: karen987 | last post by:
Could someone please tell me what code to add here? I have a weblog, with daily news which readers can add comments to, which are stored in a database. The form to add the comments is on an ASP page, (see code below). There is a small box, where readers can type a comment. What i want to do is to add a simple text editor, nothing too elaborate. A toolbar above the text area, should suffice, and it should be the same size as the box obviously. ...
5
16574
helimeef
by: helimeef | last post by:
I designed a little PHP/MySQL forum (just for fun), and I have commenting set up and top threads on the homepage and whatnot. I'm relatively new to PHP, and this is the biggest project I've done, but the only problem is that I can't think up a way to let people reply to an already-posted comment... Anyway, I have a MySQL database called "forum", with three tables; "comment", "thread", and "user". I had the idea of creating a new field in the...
14
3075
by: Siv | last post by:
Hi, Just busily coding away and removed a procedure from my code and all of a sudden an error came up miles away from the location of the piece of code I removed and it relates to the XML comment inserted at the top of the procedure. I had this happen once before and I couldn't fathom why it was complaining, the XML comment is like this:
39
2888
by: polas | last post by:
Afternoon all. I was just wondering about this point - I have (generally) used // for commenting a single line in C, but from looking at code other people have written it seems many use /* */ (which I only use if my comment will be over multiple lines) - does one way have any advantages over the other, or is the style exactly that, a question of style? Cheers, Nick
0
9687
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9543
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10488
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10029
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6808
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5467
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5588
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4144
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3761
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.