PHP commenting standards

后端 未结 3 573
走了就别回头了
走了就别回头了 2021-02-07 19:46

I need to comment massive amounts of information in only a handful of files, and when I look around Google and here at Stack Overflow, I continue to find results matching co

3条回答
  •  独厮守ぢ
    2021-02-07 20:03

    Taken from http://www.kevinwilliampang.com/2008/08/28/top-10-things-that-annoy-programmers/

    Comments that explain the “how” but not the “why”

    Introductory-level programming courses teach students to comment early and comment often. The idea is that it’s better to have too many comments than to have too few. Unfortunately, many programmers seem to take this as a personal challenge to comment every single line of code. This is why you will often see something like this code snippit taken from Jeff Atwood’s post on Coding Without Comments:

    r = n / 2; // Set r to n divided by 2
    // Loop while r - (n/r) is greater than t
    while ( abs( r - (n/r) ) > t ) {
        r = 0.5 * ( r + (n/r) ); // Set r to half of r + (n/r)
    }
    

    Do you have any idea what this code does? Me neither. The problem is that while there are plenty of comments describing what the code is doing, there are none describing why it’s doing it.

    Now, consider the same code with a different commenting methodology:

    // square root of n with Newton-Raphson approximation
    r = n / 2;
    while ( abs( r - (n/r) ) > t ) {
        r = 0.5 * ( r + (n/r) );
    }
    

    Much better! We still might not understand exactly what’s going on here, but at least we have a starting point.

    Comments are supposed to help the reader understand the code, not the syntax. It’s a fair assumption that the reader has a basic understanding of how a for loop works; there’s no need to add comments such as “// iterate over a list of customers”. What the reader is not going to be familiar with is why your code works and why you chose to write it the way you did.

    also... phpdoc

提交回复
热议问题