Commenting

The following applies to Programming I, II, III, Web Programming classes. Non-programming classes will be given class specific guidelines where applicable.


Purpose

Commenting guidelines are structured to conform to professional guidelines as much as possible to help train the student in professional coding practices. All submitted code must conform to the following guidelines EXACTLY. Failure to do so will result in your submission being penalized, up to and including being rejected and an assigned grade of zero. The following guidelines, and classes in general, are designed to simulate real-world software engineering practice as much as possible.

No variations on these standards will be accepted.

Comment Headers

All code files must contain the following comment block header at the top of the file. In C/C++ this includes both header files and cpp files. In all other languages, if it has code in it, it needs a comment header (.py, .js., .php, .html, .css, etc.)

  • Python files
        #############################
        # Name: Jane Doe
        # Assignment: 1
        # Purpose of the file: 1-2 paragraphs explaining
        # the purpose of the code in the file.
        #############################
    
  • C/C++, JavaScript, PHP, and CSS files
        /**************************
        Name: Jane Doe
        Assignment: 1
        Purpose of the file: 1-2 paragraphs explaining
        the purpose of the code in the file.
        ***************************/
    
  • HTML files
        <!-- -----------------------
        Name: Jane Doe
        Assignment: 1
        Purpose of the file: 1-2 paragraphs explaining
        the purpose of the code in the file.
        ------------------------ -->
    

Functions and Methods

All functions/methods must be commented appropriately to professional standards. The following applies only to Programming I, II, III. Failure to comment functions/methods properly will result in a substantial grade penalty, up to and including a zero.

Python
  • All functions/methods begin with a docstring describing the function/method in detail. Do not use docstrings for other purposes!
  • List parameters as shown in the example.
  • List returns as shown in the example.
  • List exceptions as shown in the example.
  • Note that types have to be listed because Python is dynamically typed.
  • Comment inside the function only where absolutely necessary. If you write code properly, and document properly, you should very rarely need comments in your code.
  • Example:
    def divide(x, y):
        
        """
        This function accepts two values, tests
        them for type, rejects non-numerics with
        an exception, divides numerics x by y,
        treating them as floats, and returns the
        result. Divide by zero throws an exception.

        @param (float) x : dividend
        @param (float) y : divisor

        @return (float) x/y : quotient

        @exception (TypeError) : non-numeric in either x or y
        @exception (ZeroDivisionError) : y == 0

        @note note anything worthwhile here or leave this line out
        """

        if not isinstance(x, int) and not isinstance(x, float):
            raise TypeError("first parameter is not a numeric type")
        if not isinstance(y, int) and not isinstance(y, float):
            raise TypeError("second parameter is not a numeric type")
        if y == 0:
            raise ZeroDivisionError

        return (float(x)/float(y))
        
C/C++
  • All functions/methods begin with a description of the function/method in detail.
  • List parameters as shown in the example.
  • List returns as shown in the example.
  • List exceptions as shown in the example.
  • Note that @param and @ return types do not have to be listed because C/C++ is statically typed. Exception type is listed so the user does not have read the code to find it.
  • Comment inside the function only where absolutely necessary. If you write code properly, and document properly, you should very rarely need comments in your code.
  • Example:
    double divide(double x, double y){
                    
        /*********************************************
        This function accepts two values, casts
        them to double regardless of numeric type passed,
        divides double x by double y, and returns the
        double result. Divide by zero throws an exception.

        @param x : dividend
        @param y : divisor

        @return x/y : quotient

        @exception (int) : y==0

        @note note anything worthwhile here or leave this line out
        *********************************************/

        if(y == 0){
            throw 0;
        }

        return x/y;
    }
            

Main and/or Driver File

main( ) should be commented by the same rules as any function, with command line parameters (if any) detailed just like any parameter. Driver files without a main( ) (e.g. a Python 'script') should be commented by the same rules as any function, with command line parameters (if any) detailed just like any parameter.

Classes

Python
  • Give a description of the class.
  • List any attributes. Giving a type description is necessary since Python doesn't require a type is specified.
  • You must also comment methods as shown above (and below).
  • Example:
    class Circle:
        """
        This class defines a Circle. It must be passed
        a radius which defaults to __MINR if an invalid
        a radius which defaults to __MINR if an invalid
        value is passed (i.e. negative number).

        @attrib (float) __radius : the radius of the circle
        @attrib (int) __MINR : the default radius of the circle
        """

        def __init__(self, radius):
            """
            Constructor. Pass in a posive float
            value to set the radius.

            @param (float) radius : the radius of the circle
            @return na : na
            @exception na : na
            """
            self.__MINR = 1
            self.setRadius(radius)

        def getRadius(self):
            """
            Returns the private attribute __radius

            @param na : na
            @return (float) self.__radius : the radius of the circle
            @exception na : na
            """
            return self.__radius

        def getArea(self):
            """
            Calculates and returns the area of the circle.

            @param na : na
            @return (float) : the area of the circle
            @exception na : na
            """
            return math.pi * (self.__radius ** 2)

        def setRadius(self, radius):
            """
            Sets the radius of the circle which must be
            positive or it will default to __RMIN

            @param (float) radius : the radius of the circle
            @return na : na
            @exception na : na
            """
            if radius > 0:
                self.__radius = radius
            else:
                self.__radius = self.__MINR

    
C/C++
  • The example below applies to your class prototype in your header file. Remember, methods in the .cpp file are also commented as shown above for functions.
  • Give a description of the class, as shown below.
  • List any attributes. Giving a type is optional since C++ requires type is specified in the decleration.
  • Don't forget that methods in the cpp file must also be commented as shown above, including destructors and constructors.
  • Example:
    class Circle {
        /*********************************************
        This class defines a Cirlce. It has two constructors
        and can be instantiated with a default radius or with
        a caller assigned radius.

        @attrib radius : the radius of the circle
        *********************************************/

        public:
            /**********************
            Constructors/Destructor
            ***********************/
            Circle();
            Circle(double);
            ~Circle();

            /**********************
            Getters/Accessors
            ***********************/
            double getRadius();
            double getArea();

            /**********************
            Setters/Mutators
            ***********************/
            void setRadius(double);

        private:
            double radius;
    };
                

General Commenting and Debug Statements

All code should be commented appropriately to professional standards.

  • Do NOT leave any debug statements in your code. Do not simply comment them out, you must remove them completely prior to submission.
  • Do NOT leave any commented out code in your files.
  • Do NOT comment things that are trivial or easy to understand (unless you need the comment for yourself for some reason in which case it's okay).
  • DO comment anything that needs explaining and/or is unusual.
  • DO comment wherever you want to convey information for grading purposes. For example, you couldn't get something quite right, but you understand how it was supposed to work, explain your thought process in a comment. This will likely result in a higher grade if it is obvious you understand what should have been done and you were trying to do it.