VISH  0.2
Coding Style and Hints

Pass by Const Reference

When defining functions, always pass parameters that are not native C++ types as a const reference, if not special reasons require otherwise.

Correct:

void f(const MyClass&C);

Incorrect:

void f(MyClass C);

Rationale: This is basic C++ knowledge. Passing data structures by value is very bad, as it might not only require unnecessary copying in memory, but also require unnecessary calls to constructor and destructor functions.

Variable Initialization

Always initialize each variable right when it is defined.

Correct:

void f()
{
int     i = 0;
}

Incorrect:

void f()
{
int     i;
        i = 0;
}

Rationale: The habit of first defining variables and later initializing them stems from ancient C, which required all variables to be defined at the beginning of a coding block. Since it is not always possible to assign values there, one had to do it later. In C++, one may define variables anywhere. Thus variables should always be initialized at definition to avoid cases of uninitialized variables, which result in runtime faults. In case of classes, initialization during construction (one function call) is different from first invoking the default constructor and then calling the assignment operator (two function calls).

Variable Declaration Style

As a particular coding habit, it has turned out to be useful to see a variable declaration as an "prefix" to a statement. Statements are indented within coding blocks for aesthetic beauty. We use tabulators that correspond to eight spaces. Some consider this indentation much too wide, because then you end up too easily at the right border of the screen. However, we consider exactly this a feature of the wide indentation, because too many indentation levels are a clear sign of spaghetti code and bad code design. A large indentation thus helps to keep functions short and simple.

With variable declarations considered as a "prefix" to statements, we do not indent them. They are left at the same level as the surrounding block. This coding convention is in clear opposition to nearly all other coding styles found elsewhere. Nevertheless, once one is used to it, it supports well the idea of short variable lifetimes, and to overcome the ancient roots of C++ in early C.

Correct:

void    f()
{
int     i = 0;
        ...
}

Incorrect:

void    f()
{
        int i = 0;
        ...
}

Constant Variables

Make all variables and references to variables constant if no particular reason counts against. Correct:

std::vector&g();
void    f()
{
const std::vector&data = g();
}

Incorrect:

void    f()
{
std::vector&data = g();
}

Variable Lifetime

Variables should have the shortest lifetime possible. While late definition (Variable Initialization) leads to late birth of a variable, the use of coding blocks helps to keep their life short.

Correct:

void f()
{
        for(int i = 0; i<10; i++)
        {
        double j = i*i / 100.0;
            ...
        }
}

Incorrect:

void f()
{
int     i=0;
double  j=0;
        for(int i = 0; i<10; i++)
        {
                j = i*i / 100.0;
                ...
        }
}

Rationale: Resources occupied by variables should be freed as early as possible. While those are mostly negligble for native types as in the above example code, the situation will be very different for complex objects.

Hints Create coding blocks even within some function:

void f()
{
        {
        int     k =0;
        }
}

Define variables within an if-statement:

Correct:

        if (int i = myfunction() )
        {
                printf("myfunction() returned %d\n", i);
        }

Incorrect:

int     i;
        if (i = myfunction() )
        {
                printf("myfunction() returned %d\n", i);
        }