wiki:CodeGuideExamples

Standard Include Header

/*
* This file is part of rasdaman community.
*
* Rasdaman community is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Rasdaman community is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with rasdaman community.  If not, see <http://www.gnu.org/licenses/>.
*
* Copyright 2003, 2004, 2005, 2006, 2007, 2008, 2009 Peter Baumann /
rasdaman GmbH.
*
* For more information please see <http://www.rasdaman.org>
* or contact Peter Baumann via <baumann@rasdaman.com>.
*/
/*************************************************************
 *
 * PURPOSE: 
 *
 * COMMENTS:
 *
 * BUGS:
 *
 ************************************************************/

Standard Include Header (LGPL)

/*
* This file is part of rasdaman community.
*
* Rasdaman community is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Rasdaman community is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with rasdaman community.  If not, see <http://www.gnu.org/licenses/>.
*
* Copyright 2003, 2004, 2005, 2006, 2007, 2008, 2009 Peter Baumann / rasdaman GmbH.
*
* For more information please see <http://www.rasdaman.org>
* or contact Peter Baumann via <baumann@rasdaman.com>.
*/
/*************************************************************
 *
 * PURPOSE: 
 *
 * COMMENTS:
 *
 * BUGS:
 *
 ************************************************************/

Standard Source Header

/*
* This file is part of rasdaman community.
*
* Rasdaman community is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Rasdaman community is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with rasdaman community.  If not, see <http://www.gnu.org/licenses/>.
*
* Copyright 2003, 2004, 2005, 2006, 2007, 2008, 2009 Peter Baumann /
rasdaman GmbH.
*
* For more information please see <http://www.rasdaman.org>
* or contact Peter Baumann via <baumann@rasdaman.com>.
*/
/*************************************************************
 *
 * PURPOSE: 
 *
 * COMMENTS:
 *
 * BUGS:
 *
 ************************************************************/

Standard Source Header (LGPL)

/*
* This file is part of rasdaman community.
*
* Rasdaman community is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Rasdaman community is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with rasdaman community.  If not, see <http://www.gnu.org/licenses/>.
*
* Copyright 2003, 2004, 2005, 2006, 2007, 2008, 2009 Peter Baumann / rasdaman GmbH.
*
* For more information please see <http://www.rasdaman.org>
* or contact Peter Baumann via <baumann@rasdaman.com>.
*/
/*************************************************************
 *
 * PURPOSE: 
 *
 * COMMENTS:
 *
 * BUGS:
 *
 ************************************************************/

Standard Inline Header

/*
* This file is part of rasdaman community.
*
* Rasdaman community is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Rasdaman community is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with rasdaman community.  If not, see <http://www.gnu.org/licenses/>.
*
* Copyright 2003, 2004, 2005, 2006, 2007, 2008, 2009 Peter Baumann /
rasdaman GmbH.
*
* For more information please see <http://www.rasdaman.org>
* or contact Peter Baumann via <baumann@rasdaman.com>.
/
/**
 * INLINE SOURCE:
 *
 * MODULE:
 * CLASS:
 *
 * COMMENTS:
 *
*/

Standard Make Header

# 
# MAKEFILE FOR:  
#
# This file is part of rasdaman community.
#
# Rasdaman community is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Rasdaman community is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with rasdaman community.  If not, see <http://www.gnu.org/licenses/>.
#
# Copyright 2003, 2004, 2005, 2006, 2007, 2008, 2009 Peter Baumann /
# rasdaman GmbH.
#
# For more information please see <http://www.rasdaman.org>
# or contact Peter Baumann via <baumann@rasdaman.com>. 
# Top Level makefile. This points to the various modules that have to be build
# and/or deployed
#
#
# COMMENTS: 
#
##################################################################

Standard Make Header (LGPL)

# 
# MAKEFILE FOR:  
#
# This file is part of rasdaman community.
#
# Rasdaman community is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Rasdaman community is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with rasdaman community.  If not, see <http://www.gnu.org/licenses/>.
#
# Copyright 2003, 2004, 2005, 2006, 2007, 2008, 2009 Peter Baumann /
# rasdaman GmbH.
#
# For more information please see <http://www.rasdaman.org>
# or contact Peter Baumann via <baumann@rasdaman.com>.
#
#
# COMMENTS:
#
##################################################################

Recomendation 12

Correct:

inline int 
getLenght()
{
    ...
}

Wrong:

inline int getLenght()
{
    ...
}

Macros vs inline functions

Wrong:

#define SQUARE(x) ((x)*(x))         // wrong
int a = 2
int b = SQUARE(a++)                 // a == 6
 

Right:

inline int
square( int x );                    // right
{
  return (x*x)
};
int c = 2;
int d = square(c++);                // d == 4

Constants vs Standalone Values

Wrong:

if(iterations <= 0)
    iterations = 5;

Correct:

//Default number of iterations in units
const int defaultIterationsNumber = 5;

...

if(iterations <= 0)
    iterations = defaultIterationsNumber;

Macros vs const variables

#define BUFSIZE 7            // no type checking

const int bufSize = 7        // type checking takes place

enum  size { BufSize = 7 };  // type checking takes place

Standard Method Declaration

/**
* Description of addNumbers
* @param n1 the first argument.
* @param n2 the second argument.
* @return The return value
*/
template <class P>
int
addNumbers(int n1, int n2)
{
    ...
}

Case statement

switch(tag)
{
  case A:
    // do something
    // break is missing and foo() is also called in case A    // wrong
  
  case B:
    foo();
    // do something else
    break;

  default:
    // if no match in above cases, this is executed
    break;
}

Dynamic array allocation and deallocation

int n = 7
T* myT = new T[n];  // T is type with defined constructors and destructors

//........

delete myT;         // No! Destructor only called for first object in array a.
delete [10] myT ;   // No! Destructor called on memory out of bounds in array a.
delete [] myT ;     // OK, and always safe.

Standard Class Definition

Example class definitions in accordance with the style rules

class String : private Object
{
public:
    String();
    String(const String&);
    unsigned getLenght() const;
    inline Encoding getEncoding() const;
    inline void setEncoding(Encoding newEncoding);
    
protected:
    int checkIndex( unsigned index ) const;    
    
private:
    unsigned noOfChars;
    Encoding encoding;
    
};

Instead of writing like this:

class String
{
  public:
    int getLength() const // No !!
    {
      return length;
    };
    
  private:
    int length;
};

Do it this way:

class String
{
  public:
    int getLength() const;
    
  private:
    int length;
};

inline int
String::getLength() const
{
  return len ;
}

Classes with dynamic member data

Declaration examples of the assignment operator

MySpezialClass&
MySpezialClass::operator= (const MySpezialClass msp);     // no

void
MySpezialClass::operator= (const MySpezialClass msp);     // well

const MySpezialClass&
MySpezialClass::operator= (const MySpezialClass msp);     // recommanded

Class definition

class DangerousBlob
{
  public:
    const DangerousBlob& operator=(const DangerousBlob& dbr);
   
  private:
    char* cp;
};

Definition of assignment operator

const DangerousBlob&
DangerousBlob::operator=(const Dangerous& dbr)
{
  if ( this != &dbr )          // Guard against assigning to the "this" pointer
  { 
    // ...
    delete cp;                 // Disastrous if this == &dbr
    // ...
  }
}

Constant references as return types

class Account
{
  public:
    Account ( int myMoney ):moneyAmount(myMoney) { };
    const int& getSafemoney()  const { return moneyAmount;};
    int&       getRiskyMoney() const { return moneyAmount;};  // no

  private:
     int moneyAmount;
};

Account myAcc(10);    
myAcc.getSafeMoney()  += 100000;  // compilation error: assignment to constant
myAcc.getRiskyMoney() += 1000000; // myAcc::moneyAmount = 1000010 !!

Comment: Method definition within the class definition is forbidden by rule.

Parameter declaration

int setPoint( int, int )     // wrong
int setPoint( int x, int y )           

int
setPoint( int x, int y )
{
  //....
}

Return type

int
calculate ( int j )
{
  return 2*j;
}

void
noReturnType( char* xData, char* yFile)
{
  //....
}

Include directive

// file is PrintData.cc

#include "PrintData.hh"    // user include file

#include <iostream.h>      // include file of the system library

Avoid global data

class globale
{
  public:
    //........

  protected:
    const char* functionTitle = "good style";

    int   constGlobal;
    char* varGlobal;
}

Formating of functions

void foo (); // no
void foo();  // better

// right
int
myComplicateFunction( unsigned unsignedValue,
                      int intValue
                      char* charPointerValue );

// wrong
int myComplicateFunction (unsigned unsignedValue, int intValue char* charPointerValue);

Formating of pointer and reference types

char*
object::asString()
{
  // something
};

char* userName = 0;
int   sfBlock  = 42;
int&  anIntRef = sfBlock;

Assignment operator

MySpezialClass&
MySpezialClass::operator=( const MySpezialClass& msp ); // no

const MySpezialClass&
MySpezialClass::operator=( const MySpezialClass& msp ); // recommended

Reference vs pointer

// Unnecessarily complicated use of pointers
void addOneComplicated ( int* integerPointer )
{
  *integerPointer += 1:
}
addOneComplicated (&j)


// Write this way instead
void addOneEasy ( int& integerReference )
{
  integerReference +=1:
}
addOneEasy(i);

Call-by-value vs call-by-constant-reference

// this may lead to very inefficient code.
void foo( String s );
String a;
foo(a)               // call-by-value

// the actual argumment is used by the function
// but it connot be modified by the function.
void foo( const String& s );
String c;
foo(c);              // call-by-constant-reference

Avoid continue

while ( /* something */ )
{
  if (/* something */)
  {
    // do something
    continue;                // Wrong!
  }
  // do something
}

// By using an extern `else' clause, continue is avoided and the code
// is easier to understand

while ( /* something */ )
{
  if (/* something */)
  {
    // do something
  }
  else
  {
    // do something
  }
}

Paranthesis

// Interpreted as (a<b)<c, not (a<b) && (b<c)
if (a<b<c)
{
  //...
}

// Interpreted as a & (b<8), (a&b) <8
if (a & b<8)
{
  //..
}

// when parenthese are recommended
int i = a>=b && c < d && e+f <= g+h;        // no
int j = (a>=b)&&(c<d) && (( e+f) <= (g+h)); // better

Include Files

Include file for the class PackableString

#ifndef PACKABLESTRING_HH
#define PACKABLESTRING_HH

#include "string.hh".
#include "packable.hh".

/**
 * A test class with elaborate description.
/*

class Buffer:public String:public Packable
{
  public:
    class PackableString (const String& s);
    class Buffer* put (class Buffer* outbuffer);
    //.......
};

#endif

Implementation file for the class PackableString

// PackableString.cc
// not recommanded <../include/iostream.h> Wrong

#include <iostream.h> // Right
#include "PackableString.hh"
// to be able to use Buffer instances, buffer.hh must be included.
#include "buffer.hh"

Buffer*
PackableString::put(Buffer* outbuffer)
{
    //......
}

Last modified 5 years ago Last modified on May 13, 2012 11:56:25 AM