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

前端 未结 21 2032
野性不改
野性不改 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 
     * @licence     GPL (v2 or later)
     * @copyright   Copyright (c) 2008 Martin Meredith 
     * @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

提交回复
热议问题