Comments on: On the use of comments in code http://compsci.ca/blog/on-the-use-of-comments-in-code/ Programming, Education, Computer Science Wed, 30 Sep 2020 08:31:44 -0400 http://wordpress.org/?v=2.8.4 hourly 1 By: Tony http://compsci.ca/blog/on-the-use-of-comments-in-code/comment-page-1/#comment-53740 Tony Sat, 22 Sep 2007 23:14:55 +0000 http://compsci.ca/blog/on-the-use-of-comments-in-code/#comment-53740 Good point Mike, especially the last one! Todays hardware is fast, cheap, and scales easily - it is much better to have robust code that is easy to understand and maintain, than very clever code that runs faster because of some hack. Good point Mike, especially the last one! Todays hardware is fast, cheap, and scales easily – it is much better to have robust code that is easy to understand and maintain, than very clever code that runs faster because of some hack.

]]>
By: Mike Minutillo http://compsci.ca/blog/on-the-use-of-comments-in-code/comment-page-1/#comment-53261 Mike Minutillo Sat, 22 Sep 2007 03:03:52 +0000 http://compsci.ca/blog/on-the-use-of-comments-in-code/#comment-53261 If ever you do find that you are leaving comments explaining what you are doing instead of why it is a good sign that the code needs refactoring. In fact, this is one of the no. 1 reasons I have found for refactoring code. When some programmer leaves a comment like "The following line returns the value on the top of the stack and then decrements the stack counter but only if the stack isn't emtpy", I immediately get the chills. This is about 6 different concerns and should be split up. I have also become a big fan of long method and field names which lead to so-called "self-describing code". If you do this then y = x z becomes weekly_salary = normal_weekly_salary this_weeks_bonus and you won't need comments except where you explain an algorithm or a programmer decision to do things a certain way. It also is a good indicator for when thinsg should be split up. A method which takes an open file handle and is called calculate_sales_order_and_print_receipt_and_close_file looks silly and is primed for refactoring. Big Hint of the day: You win more points in the field for maintainability over efficiency or cleverness. It is a hard lesson to take in but one well worth noting. If ever you do find that you are leaving comments explaining what you are doing instead of why it is a good sign that the code needs refactoring. In fact, this is one of the no. 1 reasons I have found for refactoring code. When some programmer leaves a comment like “The following line returns the value on the top of the stack and then decrements the stack counter but only if the stack isn’t emtpy”, I immediately get the chills. This is about 6 different concerns and should be split up.

I have also become a big fan of long method and field names which lead to so-called “self-describing code”. If you do this then y = x z becomes weekly_salary = normal_weekly_salary this_weeks_bonus and you won’t need comments except where you explain an algorithm or a programmer decision to do things a certain way. It also is a good indicator for when thinsg should be split up. A method which takes an open file handle and is called calculate_sales_order_and_print_receipt_and_close_file looks silly and is primed for refactoring.

Big Hint of the day: You win more points in the field for maintainability over efficiency or cleverness. It is a hard lesson to take in but one well worth noting.

]]>
By: mlankton http://compsci.ca/blog/on-the-use-of-comments-in-code/comment-page-1/#comment-50526 mlankton Sun, 16 Sep 2007 13:27:15 +0000 http://compsci.ca/blog/on-the-use-of-comments-in-code/#comment-50526 If you are coding in a commercial setting, someone will be maintaining your code down the road. Make it easy on them and leave good comments. If you are coding in a commercial setting, someone will be maintaining your code down the road. Make it easy on them and leave good comments.

]]>
By: Tony http://compsci.ca/blog/on-the-use-of-comments-in-code/comment-page-1/#comment-47905 Tony Thu, 06 Sep 2007 18:49:03 +0000 http://compsci.ca/blog/on-the-use-of-comments-in-code/#comment-47905 Spot on Aziz, you redeemed yourself! Spot on Aziz, you redeemed yourself!

]]>
By: Aziz http://compsci.ca/blog/on-the-use-of-comments-in-code/comment-page-1/#comment-47846 Aziz Thu, 06 Sep 2007 12:38:12 +0000 http://compsci.ca/blog/on-the-use-of-comments-in-code/#comment-47846 Lazy in the sense I didn't want to find a good example to post, I was merely bringing up the style of outlining methods and their purpose (using comments) before writing the actual code. I personally program in java, and thoroughly comment for javadoc. An "add()" method in a Fraction class may have a description of merely "adds two Fraction objects", but can have more detailed notes such as whether or not the resulting Fraction is reduced or how it handles certain input. Inside code blocks such as methods where javadoc isn't generated, I highly support self-documenting code. code such as z = x y is self-explanatory in what it does, but perhaps not why. I agree that a comment "z is sum of x and y" is in most cases redundant, but a comment of "adding the x and y values produces this object's z value because of their Freudian relationship" (fake) or something similar. :) Lazy in the sense I didn’t want to find a good example to post, I was merely bringing up the style of outlining methods and their purpose (using comments) before writing the actual code.

I personally program in java, and thoroughly comment for javadoc. An “add()” method in a Fraction class may have a description of merely “adds two Fraction objects”, but can have more detailed notes such as whether or not the resulting Fraction is reduced or how it handles certain input.

Inside code blocks such as methods where javadoc isn’t generated, I highly support self-documenting code. code such as z = x y is self-explanatory in what it does, but perhaps not why. I agree that a comment “z is sum of x and y” is in most cases redundant, but a comment of “adding the x and y values produces this object’s z value because of their Freudian relationship” (fake) or something similar. :)

]]>
By: Tony http://compsci.ca/blog/on-the-use-of-comments-in-code/comment-page-1/#comment-47698 Tony Thu, 06 Sep 2007 04:40:29 +0000 http://compsci.ca/blog/on-the-use-of-comments-in-code/#comment-47698 Guys, you should be less lazy when it comes to comments. ;) I can't comment on the usefulness of such docs with seemingly redundant descriptions - I've never had to build them. Now, since this blog focuses more on the education level programming, I'm going to maintain my original point - tell me <em>why</em> you are coding a certain way! Guys, you should be less lazy when it comes to comments. ;)

I can’t comment on the usefulness of such docs with seemingly redundant descriptions – I’ve never had to build them. Now, since this blog focuses more on the education level programming, I’m going to maintain my original point – tell me why you are coding a certain way!

]]>
By: Bashar http://compsci.ca/blog/on-the-use-of-comments-in-code/comment-page-1/#comment-47690 Bashar Thu, 06 Sep 2007 04:00:29 +0000 http://compsci.ca/blog/on-the-use-of-comments-in-code/#comment-47690 Aziz: This was the point I wanted to point but got lazy also. Tony: Those comments would be useful when you build the RoR docs. Aziz: This was the point I wanted to point but got lazy also.
Tony: Those comments would be useful when you build the RoR docs.

]]>
By: Aziz http://compsci.ca/blog/on-the-use-of-comments-in-code/comment-page-1/#comment-47678 Aziz Thu, 06 Sep 2007 02:36:34 +0000 http://compsci.ca/blog/on-the-use-of-comments-in-code/#comment-47678 I was going to mention this but was too lazy. I couldn't be bothered to come up with good examples. I was describing the way a book said to layout method blocks and comments before the code. And I include redundant comments like that in code for building javadocs :). When I comment at all. I was going to mention this but was too lazy. I couldn’t be bothered to come up with good examples. I was describing the way a book said to layout method blocks and comments before the code.

And I include redundant comments like that in code for building javadocs :) . When I comment at all.

]]>
By: wtd http://compsci.ca/blog/on-the-use-of-comments-in-code/comment-page-1/#comment-47354 wtd Wed, 05 Sep 2007 01:13:16 +0000 http://compsci.ca/blog/on-the-use-of-comments-in-code/#comment-47354 But, OJ, don't you know? Everything must be commented. ;) But, OJ, don’t you know? Everything must be commented. ;)

]]>
By: Tony http://compsci.ca/blog/on-the-use-of-comments-in-code/comment-page-1/#comment-47012 Tony Mon, 03 Sep 2007 03:27:02 +0000 http://compsci.ca/blog/on-the-use-of-comments-in-code/#comment-47012 Aziz - OJ is right. <blockquote><p> * Add this number to another. public Number add(Number num) </p></blockquote> This is clearly redundant. If you are writing comments in style that Bashar's <em>"// Z is the sum of X & Y"</em> professor is looking for, it should be a red flag. Maybe it's just more difficult to write meaningful comments for such a generic example. It's easy to understand that <em>class Number</em> represents a "number", without explanation. Though I still don't get why we have to represent it in the first place. Where is it used? Why is it implemented this certain way? <strong>Are there any special caveats or edge cases that should be pointed out?</strong> I think the latter is an especially useful type of a comment. Aziz – OJ is right.

* Add this number to another.
public Number add(Number num)

This is clearly redundant. If you are writing comments in style that Bashar’s “// Z is the sum of X & Y” professor is looking for, it should be a red flag.

Maybe it’s just more difficult to write meaningful comments for such a generic example. It’s easy to understand that class Number represents a “number”, without explanation. Though I still don’t get why we have to represent it in the first place. Where is it used? Why is it implemented this certain way? Are there any special caveats or edge cases that should be pointed out? I think the latter is an especially useful type of a comment.

]]>