Page 1 of 1

FipS' C++ coding style

PostPosted: Fri Sep 09, 2011 3:02 pm
by fips
After all the years of coding in C++ and experiencing all the possible C++ coding styles in both commercial and hobby programming I've come to the conclusion that it's time to make things simple. So I've decided to get rid of both the camelAndPascalCase (we are not chemists, Right?!) for it's inconsistency and ugliness and returned back to the roots.

So I identified 3 basic rules to follow:
1) Use lower-case as much as possible.
2) Follow the style of C++ standard library.
3) Minimize typical name clashes.

And came with 3 different naming patterns, sufficient for all my needs:
Code: Select all

Here're the rules in detail:
Code: Select all
Type_name (classes, enums, templates, typedefs, etc.)
_member_variable (s_static_variable, g_global_variable)
Template_parameter_T (or just T)
getters: value()
setters: set_value()

And here're some code snippets:
Code: Select all
namespace fs { namespace math {

template <typename T>
class Matrix4
    // ...

typedef Matrix4<float> Matrix4_f; // postfix matches the type if convenient

template <typename T, typename Whatever_param_T = void>
class Axial_box
    typedef Whatever_param_T Whatever_param;
    // ...

typedef Axial_box<float> Axial_box_f;

}} // namespace fs::math

Code: Select all
namespace fs { namespace sg {

class Scene_node

    typedef std::shared_ptr<Scene_node> Scene_node_ptr;
    typedef math::Matrix4_f Transform;
    typedef math::Axial_box_f Volume;

    enum Visibility_mode


    const Scene_node * parent() const; // without 'get_'
    Scene_node * parent();
    void set_parent(Scene_node *parent);

    const int num_children() const;
    const Scene_node_ptr child(int index) const;
    Scene_node_ptr child(int index);
    void add_child(Scene_node_ptr child);

    const Transform & world_transform() const;
    const Volume & world_volume() const;
    Visibility_mode visibility_mode() const;
    void set_visibility_mode(Visibility_mode vm);


    Scene_node(const Scene_node &);
    Scene_node & operator = (const Scene_node &);

    Scene_node *_parent;
    bool _visible;
    Visibility_mode _visibility_mode;

    typedef std::vector<Scene_node_ptr> Nodes;
    Nodes _nodes;

    static int s_num_nodes; // also use 'g_' for globals

}} // namespace fs::sg

Code: Select all
namespace fs { namespace sg {

int Scene_node::s_num_nodes = 0;


void Scene_node::set_parent(Scene_node *parent)
    _parent = parent;

// ...

}} // namespace fs::sg