AnyDSL

AnyDSL

Coding Styles


Contents


General Hints

class Foo {
//...
    /// Returns the depth.        <-- BAD: elide this comment
    int get_depth() { return depth_; }
};


// clear list                     <-- BAD: elide this comment
list.clear();

// for all elements in map        <-- BAD: elide this comment
for (auto elem : map) {
    //...
}

As a rough rule of thumb create one .cpp/.h pair for each class. However, there are several exceptions to this rule:

General Formatting

set expandtab
set autoindent
set shiftround
set backspace=indent,eol,start
set ts=4
set softtabstop=4
set shiftwidth=4

Preprocessor

Includes

Sort several headers of one group alphabetically

Example (from impala/parser.cpp):

#include "impala/parser.h"

#include <algorithm>
#include <sstream>

#include "thorin/util/array.h"
#include "thorin/util/assert.h"
#include "thorin/util/push.h"

#include "impala/ast.h"
#include "impala/lexer.h"
#include "impala/prec.h"
#include "impala/type.h"

Misc

#ifndef _NDEBUG
    foo();
#else
    bar();
#endif

Source Code Formatting

This is best shown by example:

#ifndef THORIN_FOO_H
#define THORIN_FOO_H

namespace thorin {

class Foo {
public:
    Foo(int i, float f)
        : i_(i)
        , f_(f)
    {}

    bool is_zero() const { return i_ == 0; }
    float do_something() { return f_ += i_; }
    int do_something_more_involved();
    int do_something_simple();

private:
    int i_;
    float f_;
};

}

#endif

In the cpp file:

#include "thorin/foo.h"

namespace thorin {

int Foo::do_somthing_more_involved() {
    while (i_ < 0) {
        for (size_t i = 0, e = num(); i != e; ++i)
            some_array[i]++; // some comment
    }

    switch (i_) {
        case 0:
            f();
            break;
        case 1:
            g();
            break;
        default:
            h();
            break;
    }

    return 23;
}

int Foo::do_somthing_simple() { return ++i_; }

}

General

Statements

switch (i_) {
    case  0: f(); break;
    case  1: g(); break;
    default: h(); break;
}

Expressions

Types

Classes