• C++ Programming for Financial Engineering
    Highly recommended by thousands of MFE students. Covers essential C++ topics with applications to financial engineering. Learn more Join!
    Python for Finance with Intro to Data Science
    Gain practical understanding of Python to read, understand, and write professional Python code for your first day on the job. Learn more Join!
    An Intuition-Based Options Primer for FE
    Ideal for entry level positions interviews and graduate studies, specializing in options trading arbitrage and options valuation models. Learn more Join!

About Code Comments

Joined
11/14/15
Messages
50
Points
18
Hello~
I am trying to beautify my code. I used to use a lot of long comments. I thought it would make my code clear to others; however, the truth is those tedious and wordy comments even make me lose interest to read them.
What is the best way to make comments? I think maybe short comments, which tersely express your purpose to take that step in a few words is okay. Is that correct?
Should I use complete sentences, or a phrase will do?
Should I set the first letter of a comment to be uppercase?
Should I end a comment with a period, or without any punctuation?
Many thanks~
 
In general IMO you should document a class as follows

1. description of what the class does
2. what the design rationale was for member data
3, the algorithms
4. Quirky things that you will forget

Developers are not known for these skills in general.

Should I use complete sentences, or a phrase will do?
DD a phrase is OK

Should I set the first letter of a comment to be uppercase?
DD it looks neater

Should I end a comment with a period, or without any punctuation?

DD it looks neater I suppose

Here is an example of documenting a lattice type, It might give some ideas.
 

Attachments

  • lattice.hpp
    2.9 KB · Views: 35
Last edited:
In general IMO you should document a class as follows

1. description of what the class does
2. what the design rationale was for member data
3, the algorithms
4. Quirky things that you will forget

Developers are not known for these skills in general.

Should I use complete sentences, or a phrase will do?
DD a phrase is OK

Should I set the first letter of a comment to be uppercase?
DD it looks neater

Should I end a comment with a period, or without any punctuation?

DD it looks neater I suppose

Here is an example of documenting a lattice type, It might give some ideas.
I see. Thank you~ It is a beautiful example.
So basically, I should use phrase(so no period), Uppercase letter to start.
And I should add more description about what the class does.
 
I see. Thank you~ It is a beautiful example.
So basically, I should use phrase(so no period), Uppercase letter to start.
And I should add more description about what the class does.
You're welcome :) Of course comment like

int j ; // hey this is an integer, wow

is a bit silly :D
 
Here's another class from a graphics package I wrote in ~1993 that shows how to use
C++:
// PO.HXX
//
// Header file for points in two dimensions. A given point has 3 coordinates
// for compatibility with other systems. However, it is not possible to
// influence the third coordinate and furthermore, the delivered functionality
// is typically two-dimensional.
//
// (C) Copyright Datasim BV 1992-1995
//
// The information contained in this file is property of Datasim BV Amsterdam Nederland.
// The information contained herein is subject to change without notice. No part
// of this information may be reproduced or transmitted in any form or by any means,
// electronic or mechanical, for any purpose, without the express written permission
// of Datasim BV.

#ifndef PO_HXX
#define PO_HXX

#include "an.hxx"                        // For Angles
#include "dlist.hxx"                    // For list of doubles
#include "shape.hxx"                    // Shape derivation


class PO : public SHAPE
{
protected:
    double x, y, z;                        // Cartesian representation of a point

    void init(const PO& p);                // General init functions
    void init(double d);
    void init(double dx, double dy, double dz = 0.0);

public:
    // Constructors
    PO();                                // Copy constructor
    PO(const PO& p);                    // Copy initialisor
    PO(double d);                        // All coordinates set to d
    PO(double dx, double dy, double dz = 0.0);
                                        // Each coordinate initialised
    PO(int ix, int iy, int iz = 0);        // Used in Windows
    PO(const AN& ang, double radius);    // Polar representation
    PO(const PO& p1, const PO& p2, double dis);
                                        // On 'line' [p1, p2] at dis from p1
    PO(const PO& p, double dis, const AN& ang);
                                        // At distance dis from p1 and
                                        // making angle ang with x-axis
    PO(const PO& p, double dis, double slope);
                                        // At distance dis from p1 and having
                                        // gradient slope
    PO(const PO& p1, const PO& p2);        // The midpoint of the 'line' [p1, p2]
    PO(const PO& p, const AN& ang, double x_dis);
                                        // At a horizontal distance from p and
                                        // making an angle ang with x-axis
    PO(const PO& p1, const PO& p2, const AN& ang);
                                        // Rotate p1 about p2 through angle ang
    PO(const PO& p1, const PO& p2, double r1, double r2);
                                        // Point dividing 'line' [p1,p2] in
                                        // ratio r1::r2

    // Accessing functions
    double ret_x() const;                // The x-coordinate
    double ret_y() const;                // The y-coordinate
    double ret_z() const;                // The z-coordinate
    DLIST  coordinates() const;            // Array of coordinates of point
    PO       barycentric() const;            // Barycentric representation

    // Arithmetic functions and Operator Overloading. The operators
    // '+', '-' and '<<' are asymmetric because they do operate on all
    // coordinates of the given point.

    PO&          operator    = (double d);     // Assign a number to all coords
    PO&          operator    = (const PO& p); // Assign a point to another point
    friend PO operator    + (const PO& p, double d);    // Add d to x-coord ONLY
    friend PO operator    + (double d, const PO& p);    // Add d to x-coord ONLY
    PO          operator    + (const PO& p) const;        // Add all coords
    PO          operator    - () const;            // Unary minus, reflect in (0,0)
    PO          operator    - (double d) const;            // Subtract d from x-coord
    PO          operator    - (const PO& p) const;        // Subtract all coords
    friend PO operator << (const PO& p, double d);    // Add d to y-coord ONLY
    friend PO operator << (double d, const PO& p);    // Add d to y-coord ONLY
                // To subtract an y coord, a negative value must be added

    // Scaling operators and functions
    PO           operator * (const PO& pt) const;        // Nonuniform
    friend PO  operator * (const PO& p, double d);    // Uniform
    friend PO  operator * (double d, const PO& p);    // Uniform
    PO scale (double d, CARTES_AXIS ax = X_AXIS) const; // Axis scaling
    PO           operator / (double divisor) const;    // Dividing by a number

    // Vector operators and functions
    double dot (const PO& p) const;                        // Scalar (dot) product
    double      operator    % (const PO& p) const;            // Dot product
    PO cross (const PO& p) const;                        // Cross (vector) product
    PO          operator    ^ (const PO& p) const;        // Vector product

    // Other operators which are consistent with the operators '+' and
    // '-' above.
    PO& operator += (double d);                    // Translation in x direction
    PO& operator += (const PO& p);                // Translation in all directions
    PO& operator -= (double d);                    // Translation in x direction
    PO& operator -= (const PO& p);                // Translation in all directions

    // Comparison functions
    BOOLEAN operator == (double d) const;        // Are all coords equal to d
    BOOLEAN operator == (const PO& p) const;    // Are all coords equal to p
    BOOLEAN operator != (double d) const;        // Are all coords not equal to d
    BOOLEAN operator != (const PO& p) const;    // Are all coords not equal
    BOOLEAN operator <    (const PO& p) const;    // Are all coords < p's coords
    BOOLEAN operator >    (const PO& p) const;    // Are all coords > p's coords
    BOOLEAN operator <= (const PO& p) const;    // Are all coords <= p's coords
    BOOLEAN operator >= (const PO& p) const;    // Are the coords >= p's coords

    // Fundamental geometric 'primitives'
    BOOLEAN horizontal(const PO& p) const;        // Horizontal 'line'?
    BOOLEAN vertical(const PO& p) const;        // Vertical 'line'?
    BOOLEAN acute(const PO& p) const;            // 'Line' with slope > 0.0?
    BOOLEAN obtuse(const PO& p) const;            // 'Line' with slope < 0.0?
    BOOLEAN collinear(const PO& p1, const PO& p2) const; // Three on one line
    double    dist(const PO& p) const;            // Distance to another point
    double    slope(const PO& p) const;            // Slope of 2 point line
    AN        angle(const PO& p) const;            // Angle that 2 points make
    PO        mirror(const PO& p) const;            // Mirror in a point

    // Input-output
    SHAPE* copy() const;                        // Return a copy
    void   operator () (const DRIVER& o) const;    // Output
    void   operator () (const TRANS& t);        // Transformation
    friend ostream& operator << (ostream& os, const PO& pt); // Textual

    // Special data and functions
    friend double smallest(const PO& p);        // Smallest coordinate of the point
    friend double largest(const PO& p);        // Largest coordinate of the point
    friend double abs(const PO& pt);    // Distance from origin (abs. value)

};

#endif // PO_HXX
 
Back
Top