Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Commenting One's Code In A GNU/GPL/OpenSource World

by Revelation (Deacon)
on Mar 31, 2002 at 15:38 UTC ( [id://155611]=perlmeditation: print w/replies, xml ) Need Help??

First of all, I'd like to start by saying that this in no way pertains to perl scripts which are written for one's job, where maintainability is key, and where it is necessary to have people changing your code. This argument does not pertain to coding for oneself, where documentation can help the coder visualize how a script will work, or let the coder add code where code is needed, but instead chooses to focus on code that's out to the public. I will make an argument to myself, and we'll see how it comes out to everyone else ;)

I'm going to argue that it's unnecessary to comment one's code, and if anything bad for the reader. It's obvious that commenting code makes the comprehension of code easier, but is it better to facilitate the reader's comprehension of the code, and in doing so facilitate the skipping over pieces of the code, which are well commented, and he/she has no reason to edit? In my opinion, the purpose of distributing perl code is not only to save the new coder time, but also to educate perl scripters. Commenting code sacrifices the educational purpose of code for maintainability and laziness. This is where an argument sprouts in my mind.

Is it better for the new "bearer of the code" to be able to make small hacks that don't really fit into the grand scheme of the code, or would it be better to force this new bearer to read through the code, and make sure he understands what is really happening? One reason I love code obfuscation is that it shows me whether I really *know* a function, if I don't I'll be forced to go to perldoc, and in some cases even ask for help. Another example of this is the bugtrack, which was created by Adam Siegel. Although I have been writing basic perl scripts for over two years, my understanding of perl had always been rather basic, and I began my coding career in PHP; however, the bugtrack package forced me to learn a lot about perl, and many functions in perl. The package was not commented at all, and when I say at all I mean *at all*. This forced me to re-examine my knowledge of perl, after all, if I couldn't understand what was going on in this package, I wouldn't be able to modify it. My re-examination of that knowledge has resulted in a great affinity for perl, and its' inherant flexibility and robustness.

It seems strange that packages which are not well documented make me work harder, and perl doc more, in order to understand the inner workings, than packages which are well documented, and should be easy to "hack up"; however the packages which aren't well documented have made me learn much more than any well-documented package could ever do, as perl coders have a tendency to oversimplify their explanations, and not include the flexibility of functions, or how else a function could be used. This forces me to question whether it's necessary or right, for that matter, to document our code in a version that is open for the world. This facilitates people hacking up the code, or making crude hacks to the code, which would not happen if they actually examined the purpose for the code.
Gyan Kapur
gyan.kapur@rhhllp.com
  • Comment on Commenting One's Code In A GNU/GPL/OpenSource World

Replies are listed 'Best First'.
(jeffa) Re: Commenting One's Code In A GNU/GPL/OpenSource World
by jeffa (Bishop) on Mar 31, 2002 at 17:46 UTC
    Nice writeup. ++ for that and for bringing this topic up.

    But i have to disagree. ;)

    Commenting is an art form - too much or too little can both be bad. IMHO, the following snippet uses unecessary comments: (update - except the ones marked with *, again IMHO)

    # open the file and quit if we can't * open(FH,'foo.txt') or die $!; # store the contents into an array * my @line = <FH>; # close the file close FH; # for each element in the array, print it * for my $line (@line) { # start for loop # print it print $line; } # end of for loop # exit exit;
    But, for complex, obfuscated lines of code - you have to make comments for them!
    if (exists $self->{'pk'}) { $self->{'pk_index'} = delete $self->{'fields_hash'}->{$self->{'pk'}} +; splice(@{$self->{'fields_arry'}},$self->{'pk_index'},1) if defined $ +self->{'pk_index'}; }
    Say you wrote that piece of code five months ago. At the time, it made perfect sense - but now, you can't remember why you did that. That is why comments are good - to quickly jog your own memory so you don't waste time trying to remember.

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)
    
      Commenting is an art form - too much or too little. IMHO, the following snippet uses unecessary comments:

      Well said. Excessive use of comments can be just as annoying as a lack of comments. I'm sure almost everyone will agree that the snippet you posted was overcommented, however someone just starting to learn Perl might find it useful (many tutorials also use this format). The tricky part is striking a good balance so your code remains useful to the most people.

Re: Commenting One's Code In A GNU/GPL/OpenSource World
by cjf (Parson) on Mar 31, 2002 at 16:58 UTC
    Commenting code sacrifices the educational purpose of code for maintainability and laziness.

    I'd have to disagree. Commenting code makes it easier to understand, and in doing so, makes it easier to learn from.

    First of all, if someone new to Perl who is looking for some code to learn from comes across your code and sees no comments, do you think they'll even bother trying to figure out what it does? If for some reason they do, they might end up making wrong guesses and coming to false conclusions. Including comments will make reading your code less intimidating and give people a basic understanding of your code which they can build upon.

    The other side of it is that maintainability is important in publically released code as well. If you want your code to be truly useful to others and want them to reuse it, it needs to be well documented. This is especially important for larger projects.

    Taking out the comments from your code will lead to code that is harder to maintain and is less useful to others. Even if comments did have some magical effect making all code with them uneducational, why not let people decide for themselves whether or not they want to take the comments out?

Re: Commenting One's Code In A GNU/GPL/OpenSource World
by ilcylic (Scribe) on Mar 31, 2002 at 20:13 UTC
    I'm afraid I also have to disagree. I can concur with the idea of what you are trying to say, and once again, this is a "perfect world" scenario, but unfortunately, we don't live in one.

    My main source fro disagreement is that time is a real world resource. From a purely utilitarian point of view, the less time people have to spend deciphering complex code, the more time they can spend writing improvements to that code. A broad and overarching way to look at Open Source is that we're all working towards the goal of having better computers to work with, and the sooner we get the gigatons of code that need to be written written, so much the better.

    If everyone had the same coding style, (and realistically, if everyone was a great perl coder), there wouldn't be a need for comments, because not only would people be able to read the code like it was a natural language, but it would have been written by someone who wasn't a grammatic moron. A lot of the code I have ended up having to work on felt like I was trying to read a book which was written by someone who didn't really speak the language. A "Translation", so to speak, into a language which both the original author, and myself, were more fluent in, would have sped up both the process of my understanding what had originally be written, and likewise, my changes and additions to the code.

    This would, of course, have left me with more time to work on other things. It's a pretty Randian viewpoint on things, But I think it makes at least some sense. I agree that providing the less experienced with material to study for improvement of their skills is important, but there are reasons that programming language textbooks have paragraphs in english (or whatever) and not just code.


    -Il Cylic

Re: Commenting One's Code In A GNU/GPL/OpenSource World
by chromatic (Archbishop) on Mar 31, 2002 at 21:35 UTC
    What if you guess the purpose of the code incorrectly?

    If you don't want code comments to explain *what* the code is doing, that's fine. If you object to comments that explain *why* the code exists, that's silly.

    This past week, I've been bugfixing a pile of code best described as "misguided". It features the same dozen lines of code repeated thirty times, with different category names.

    There's a purpose to the repetition. It's not a good one, but there was a reason for it. Since there were no comments, I had to read the original programmer's mind.

    Good luck.

      Or to coin a slogan: "It's easier to read the programmer's mind if the programmer writes it down." :)

      stephen

Re: Commenting One's Code In A GNU/GPL/OpenSource World
by Desdinova (Friar) on Apr 01, 2002 at 08:30 UTC
    I have to disagee totally with what you are saying here. Very simply the reaon i use open source stuff is because it gives me the ability to customize it to suit my needs. Hope fully in a quick and easy way.

    While I often do learn new tricks while making these tweaks that is not the reason I use it. Software is a toll to get a job done for me. To that end if i have to dig around for all the info to understand what I need to change then I might as well write the whole thing from scratch.

    IMHO one of the best things about open source is that it tends to be written by those who have a great deal of knowledge about the subject of thier code. For me to go through and understand something like DBI or XML::* take much more than understanding perl. I would also need to know a great deal about databases and XML respectivly. In fact comments are very helpful in learning these things. I can read the comment that explains what the programmer is trying to achive with a given piece of code, which gives me a beeter understanding of the problem and then i can perldoc the functions they use to implement it. This gives me the greatest amount of understanding when I am trying to learn from someone's code.

    Lastly to your insight that good commenting allows people to make bad changes to the code without fully understanding it. My first reaction is fine they have to sleep in the bed they make, they break thier own code thats thier right, if they send me a patch with thier change I am going to make sure it makes sence before it goes into the main tree. Second a lack of comments increase the chance they will misconstrue what I am trying to and actually raise the chance of bad changes. Finally if I comment what i am trying to achive and a person more knowledgable in perl understands the goal they can actually send me a better way to do it and i get to learn.
Re: Commenting One's Code In A GNU/GPL/OpenSource World
by trs80 (Priest) on Apr 01, 2002 at 13:23 UTC
    I think commenting code is important, but if I read between the lines and apply some of my own way of thinking I can agree with this post on the following merits:
    • The purpose or purposes of the code is still unclear in the developer mind, that is it is in very early stages. Commenting at this stage *can* influence someone into thinking that is all or only what the code can/should do.
    • I see over and over again, and I say it myself, that at some point in the future you will come back and look at the code and have to wonder what you did. I have to wonder if it is possible to influence your own corrections by reading your previous comments. I feel it is and I think it has even happened to me.
    • Skill level of the intended audience. Not commenting can be a way to keep (scare) "unskilled" people away from the code, this has its advatanages and disadvatages depending on whether you want your project to used for new coders to learn from or if you are producing something delicated and by not commenting hope to keep the casual user from "hurting" themselves.
    • My favorite reason (excuse): You have become enlightened and your knowledge of the code exceeds your impatience so commenting becomes a hinderence not an aid.
    Ok so what about comments that *should* be in the code? The WHY you did that comment. I think these are far more valuable then explaining WHAT is being done. For example, in some resent code I was working on I had some trouble with rand not really being so rand so I looked up the function and read up and saw that I needed to add the srand function to get better results. I need to document the srand function call as to WHY I added it so that time isn't wasted on relearned by me or learning by someone else as to why the srand function is needed to correct a problem under a particular condition.

    Commenting also adds to the learning process. When you comment WHY you did something you reinforce what you have learned and how you applied what you learned.

    In short I agree with Revelation's argument if I interpet the type of commenting he wants to avoid correctly.
Re: Commenting One's Code In A GNU/GPL/OpenSource World
by FoxtrotUniform (Prior) on Apr 01, 2002 at 17:25 UTC

    As I read it, your argument is based on two premises which don't necessarily (or even usually) hold:

    1. Open-source code's primary purpose is to help programmers learn.
    2. Comments hinder the learning process, because they give away information that would be better learned the hard way.

    To the first premise: IMHO, open-source code is made open-source mostly to give people good tools that they otherwise wouldn't have access to, and if the code does mostly what they want it to, to give them the chance to modify it to do exactly what they want it to. If they learn something in the process, that's great, but the point is accessibility. So if you make open-source code harder to modify (by taking out the comments, for instance), you're defeating part of its purpose (the easy-to-modify bit).

    To the second premise: depends what you want to teach. I'm going to learn more, and faster, about the language, or other back-end libraries, if I have to dig through the documentation to figure out how stuff works. On the other hand, if I'm trying to learn an algorithm, I don't want to get tied down to the details of just how each step works, because I'll lose the forest for the trees.

    I don't think your argument's necessarily wrong, just a bit broad in scope. And thanks for bringing it up; I've enjoyed reading this thread.

    --
    :wq

Re: Commenting One's Code . . .
by jlongino (Parson) on Apr 01, 2002 at 17:57 UTC
    I apologize for the negative tone of my response, but I didn't find much that was positive in your original post (unless one considers that it elicited some thoughtful replies). It appears to me to promote "elitist" views that are not IMHO conducive to a learning or community environment.

    Should we infer from you first paragraph that GNU/GPL/OpenSource code shouldn't meet professional standards? Do you feel that the genius of your contribution outweighs any responsibility for professional standards?

    If you don't want morons to hack your code, don't put it where they can get to it. Do you really think morons won't "defile" your code just because they don't understand it or you don't comment it?

    I think it is commendable that you want to save coders time and educate perl scripters. But educate them in what, the art of obfuscation? If you want to educate them, show them code that is as well documented as it is coded and useful.

    --Jim

      Hear! Hear! This Saint is totally correct! If there were a ranking higher than Saint, he'd certainly earn it. It appears that Revelation does not want to be criticized. After all, he was (at the time) just a high school freshman, relatively new to Perl, so one cannot judge him too harshly. But why would he post something so "elitist" if he's not an elite? Not to sound trollish, but Revelation's elitist views seem only to reflect his ignorance and fear that he will be downvoted. I never posted anything on any math or physics forum (subjects which I know somewhat better than programming) because I'm not a professional mathematician or physicist (yet.) I'm certainly not an ignoramus, and neither is Revelation, but I know better than to post a node implying that I'm just a student. Revelation had better heed the advice of his superiors. I'm sure he doesn't want to be known as the Worst Nodes (or Codes) Upvoter any longer than he has to! -100104
Re: Commenting One's Code In A GNU/GPL/OpenSource World
by Fingo (Monk) on Apr 01, 2002 at 21:26 UTC
    Well according to your argument, a lack of comments is good for educational value. I agree with that on a level, but very often the purpose of code is not to educate, but to solve a problem. And the programmer wants whoever ends up maintaining the code to be able to do it well, not learn from it. I guess that completely uncommented code can be good for an intellectual challenge, but it is not really USEFUL.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlmeditation [id://155611]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (6)
As of 2024-04-19 12:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found