FipS' C++ coding style

> Coding, hacking, computer graphics, game dev, and such...
User avatar
fips
Site Admin
Posts: 166
Joined: Wed Nov 12, 2008 9:49 pm
Location: Prague
Contact:

FipS' C++ coding style

Post 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

First_style
second_style
THIRD_STYLE
Here're the rules in detail:

Code: Select all

Type_name (classes, enums, templates, typedefs, etc.)
function_name()
variable_name
_member_variable (s_static_variable, g_global_variable)
namespace::name
file_name.h
Template_parameter_T (or just T)
getters: value()
setters: set_value()
PREFIX_Enum_value
MACRO_NAME
macro_parameter_
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
{
 public:

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

    enum Visibility_mode
    {
        VM_Always_shown,
        VM_Hidden
    };

    Scene_node();

    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);

 private:

    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;

Scene_node::Scene_node():
_parent(0),
_visible(true),
_visibility_mode(VM_Always_shown)
{
}

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

// ...

}} // namespace fs::sg