The following naming conventions for files, classes, function, variables, etc. intend to simplify the usage of RELACS-related libraries.
It is a generally accepted standard to use lowercase file names
for all source code and header files
and to use the extensions .cc
and .h
, respectively.
Ideally, each source code file contains just one class.
The source code files should have the same name as the class they contain.
Example: If the class is named MyClass
, then the source code and the header file
are named
myclass.h myclass.cc
The standard name for makefiles is
Makefile
In naming classes, function, variables, etc. we follow the convention used by the Qt library. No underscores '_' are used.
For example, use
ThisIsMyVariable
instead of
this_is_my_variable
The only exception is const_iterator to conform with the STL (http://www.sgi.com/tech/stl/).
Class, struct, and typedef names start with an uppercase letter.
Example:
class MyClass;
Enum types as well as their members also start with an uppercase letter.
Example:
enum Color { Transparent=-1, Black=0, White, Red, Green, Blue };
Member functions have a lowercase first letter.
Their arguments and local variables are all lowercase.
Use the const keyword for the arguments and the function
whenever it is appropriate.
Functions that set variables of a class are named setXXX()
.
Functions retrieving data from a class don't start with get
(getXXX()
),
instead they are just named xxx()
to keep the code better readable.
Examples:
void setText( const string &text, int flag ); // set some text to text double width( void ) const; // returns a width if ( width() > 0 ) ... // reads better than: if ( getWidth() > 0 )
Member variables are never public! Use (inline) member functions to provide acsess to member variables. Use an uppercase first letter for member variables to avoid conflicts with the corresponding member function and local variables.
Example:
class MyClass { public: // Return the value of the member variable Number: inline double number( void ) const { return Number; }; // Set the value of the member variable Number to number: inline void setNumber( double number ) { Number = number; }; private: // A member variable: double Number; };
For reading and writing files use
void load( ... ); void save( ... );
respectively.
For reading and writing to devices use
void read( ... ); void write( ... );
A container is a class which organizes an array of some data type T. For these classes the following standard functions are usefull and should be provided if possible. They conform to the STL (http://www.sgi.com/tech/stl/).
int size( void ) const;
Returns the number of data elements the container currently holds.
bool empty( void ) const;
Returns true if there aren't any data elements stored in the container.
Equivalent to (size()== 0)
.
void resize( int newsize, T dflt=0 );
Changes the number of data elements to newsize
. If this number is larger
than the size of the container, then the new data elements are initialized with
dflt
.
void clear( void );
Removes all data elements from the container.
Equivalent to resize( 0 )
.
int capacity( void ) const;
The maximum number of data elements the container can hold without reallocating memory.
void reserve( int newsize );
Allocates memory so that the container can hold at minimum newsize
data elements.
If newsize
is larger than the size of the container
all data elements are kept and the size does not change.
If newsize
is smaller, then nothing is done,
the container will keep its previous capacity().
T operator[]( int index ) const; T &operator[]( int index );
Read and set the data element specified by index
.
No range checking is performed in order to optimize performance speed.
You have to make sure that index
specifies an existing data element.
T at( int index ) const; T &at( int index );
Read and set the data element specified by index
.
In contrast to operator[]
these functions first check the validity
of index
.
T front( void ) const; T &front( void ); T back( void ) const; T &back( void );
Read and set the first or last data element, respectively. You have to make sure that they really exist, i.e. that the container is not empty().
void copy( int range, T *data ) const; void copy( int range, vector< T > &data ) const;
The copy
functions allow to copy a range of data elements of the container
to different types of container data
.
void assign( T *data, int range ); void assign( vector< T > &data, int range );
The assign
functions allow to copy a range of data elements from
different types of containers data
into the container.
Before copying data all data elements of the container are removed.
void append( T *data, int range ); void append( vector< T > &data, int range );
The append
functions allow to append a range of data elements from
different types of containers data
to the already existing data elements
of the container.
const_iterator begin( void ) const; const_iterator end( void ) const;
Return an iterator to the first and behind the last element of the container, respectively.