What are your “hard rules” about commenting your code?

前端 未结 21 1836
野性不改
野性不改 2020-12-31 16:09

I have seen the other questions but I am still not satisfied with the way this subject is covered.

I would like to extract a distiled list of things

相关标签:
21条回答
  • 2020-12-31 16:10

    I document every class, every function, every variable within a class. Simple DocBlocks are the way forward.

    I'll generally write these docblocks more for automated API documentation than anything else...

    For example, the first section of one of my PHP classes

    /**
     * Class to clean variables
     *
     * @package     Majyk
     * @author      Martin Meredith <martin@sourceguru.net>
     * @licence     GPL (v2 or later)
     * @copyright   Copyright (c) 2008 Martin Meredith <martin@sourceguru.net>
     * @version     0.1
     */
    class Majyk_Filter
    {
        /**
         * Class Constants for Cleaning Types
         */
        const Integer            = 1;
        const PositiveInteger    = 2;
        const String             = 3;
        const NoHTML             = 4;
        const DBEscapeString     = 5;
        const NotNegativeInteger = 6;
    
        /**
         * Do the cleaning
         *
         * @param   integer Type of Cleaning (as defined by constants)
         * @param   mixed   Value to be cleaned
         *
         * @return  mixed   Cleaned Variable
         *
         */
    

    But then, I'll also sometimes document significant code (from my init.php

    // Register the Auto-Loader
    spl_autoload_register("majyk_autoload");
    
    // Add an Exception Handler.
    set_exception_handler(array('Majyk_ExceptionHandler', 'handle_exception'));
    
    // Turn Errors into Exceptions
    set_error_handler(array('Majyk_ExceptionHandler', 'error_to_exception'), E_ALL);
    
    // Add the generic Auto-Loader to the auto-loader stack
    spl_autoload_register("spl_autoload");  
    

    And, if it's not self explanatory why something does something in a certain way, I'll comment that

    0 讨论(0)
  • 2020-12-31 16:12

    I usually comment a method before I write it. I'll write a line or two of comments for each step I need to take within the function, and then I write the code between the comments. When I'm done, the code is already commented.

    The great part about that is that it's commented before I write the code, so there are not unreasonable assumptions about previous knowledge in the comments; I, myself, knew nothing about my code when I wrote them. This means that they tend to be easy to understand, as they should be.

    0 讨论(0)
  • 2020-12-31 16:13
    1. I comment public or protected functions with meta-comments, and usually hit the private functions if I remember.
    2. I comment why any sufficiently complex code block exists (judgment call). The why is the important part.
    3. I comment if I write code that I think is not optimal but I leave it in because I cannot figure out a smarter way or I know I will be refactoring later.
    4. I comment to remind myself or others of missing functionality or upcoming requirements code not present in the code (TODO, etc).
    5. I comment to explain complex business rules related to a class or chunk of code. I have been known to write several paragraphs to make sure the next guy/gal knows why I wrote a hundred line class.
    0 讨论(0)
  • 2020-12-31 16:13

    I wrote a full article on bad comments. Enjoy :)

    How bad comments are born in your code

    0 讨论(0)
  • 2020-12-31 16:15

    Pre-ambles only; state a class's Single Responsibility, any notes or comments, and change log. As for methods, if any method needs substantial commenting, it is time to refactor.

    0 讨论(0)
  • 2020-12-31 16:18

    Write readable code that is self-explanatory as much as possible. Add comments whenever you have to write code that is too complex to understand at a glance. Also add comments to describe the business purpose behind code that you write, to make it easier to maintain/refactor it in the future.

    0 讨论(0)
提交回复
热议问题