wiki:CodingGuidelines

Version 7 (modified by FrederikHeber, 13 years ago) ( diff )

added style on specific initial values


  1. Coding Style
    1. How MoleCuilder code should look like
  2. Coding hints
    1. Some hints on good code vs. bad code
      1. end of stream checking
      2. Use of new/delete, return
      3. Use of return
      4. Specific (initial) values of variables


Coding Style

How MoleCuilder code should look like

Below you find a brief but hopefully complete list on how the code of espack should look like:

  • identate by two spaces, never tabs
  • Code Style from Eclipse, see ESPACK_codestyle.xml:
    /*
     * A sample source file for the code formatter preview
     */
    #include <math.h>
    
    class Point
    {
    public:
      Point(double xc, double yc) :
        x(xc), y(yc)
      {
      }
      double distance(const Point& other) const;
    
      double x;
      double y;
    };
    
    double Point::distance(const Point& other) const
    {
      double dx = x - other.x;
      double dy = y - other.y;
      return sqrt(dx * dx + dy * dy);
    }
    

Coding hints

Some hints on good code vs. bad code

end of stream checking

Using

   std::inputstream in;
   while (!in.eof()) {
     ..
   }

is bad. Rather one should use

   std::inputstream in;
   while (in.getline(...)) {
     ..
   }

or

   std::inputstream in;
   for(int j; in >> j;;) {
     ..
   }

or

   std::inputstream in;
   in >> j;
   if (in.fail())
    ..

This involves some extra typing but ensures that in case of faulty streams the error is properly pointed at.

Use of new/delete, return

The new and delete statements are written without brackets, they are not functions, i.e.

  new pointer;
  new array[3];
  delete pointer;
  delete[] array;

Use of return

The return statements should look like this

 return bar;
 return (foo && bar);

and not like

 return (bar);
 return(foo);

Specific (initial) values of variables

In general all variables always have to be initialized (we don't care about the extra tic, even if operating system guarantees zero as default memory value).

If they have to be set to some very small or very big value, use std::numeric_limits

  double smalldouble = std::numeric_limits<double>::min();
  double largedouble = std::numeric_limits<double>::max();

Also remember that there are also ::infinity() and alikes to set an illegal value which can be checked.

Attachments (1)

Download all attachments as: .zip

Note: See TracWiki for help on using the wiki.