Avoid writing comments in code

Source: Internet
Author: User

If you use a lot of comments to "Decorate" the Code is a good thing, adding a large part of comments to the code is a icing on the cake. Is that true? In fact, this is not exactly the case. If you are too late, good intentions will also do bad things. '*************************************** * ********* 'Name: copyString ''purpose: This routine copies a string from the source 'string (source) to the target string (target ). ''algorithm: It gets the length of "source" and then copies each 'character, one at a time, into "target ". it uses 'the loop index as an array index into both "source" 'and "target" and increments the loop/array Index' after eac H character is copied. ''inputs: input The string to be copied ''Outputs: output The string to receive the copy of" input "''interface Assumptions: none' Modification History: none ''author: Dwight K. coder 'date Created: 10/1/04 'Phone: (555) 222-2255 'ssn: 111-22-3333' Eye Color: green' Maiden Name: none' Blood Type: AB-'Mother's Maiden Name: none' Favorite Car: Pontiac Aztek 'personalized Lic Ense Plate: "Tek-ie "'********************************** * ************** I often do not read comments written by developers. It seems that they don't understand. In fact, their code has already told me how it works. What we need to comment out is why the program works like this. Code comments have been widely misunderstood and abused, so that you are beginning to doubt their original values-is there a need for comments? What are you expecting? Be careful! Here is a piece of code without any comments: r = n/2; while (abs (r-(n/r)> t) {r = 0.5 * (r + (n/r);} System. out. println ("r =" + r); do you understand what this code is intended? It looks clear, but what is it? Let's add a line of comment: // solve the square root r = n/2 of n using the "Newton-laferson" approximation; while (abs (r-(n/r)> t) {r = 0.5 * (r + (n/r);} System. out. println ("r =" + r); this should be what I want. Isn't it? It seems like a good compromise between the two extremes of writing an epic comment without any comments at all and every line-Are you satisfied? Not necessarily! Instead of adding comments, I prefer to re-construct this Code as follows: private double SquareRootApproximation (n) {r = n/2; while (abs (r-(n/r)> t) {r = 0.5 * (r + (n/r);} return r;} System. out. println ("r =" + SquareRootApproximation (r); I did not add a line of comments, but this piece of mysterious code is very easy to understand now.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.