Skip to content

Latest commit

 

History

History
135 lines (114 loc) · 2.78 KB

File metadata and controls

135 lines (114 loc) · 2.78 KB

FsLib code style guidelines

Note: I wrote these as I went. They are in no particular order.

  1. Braces should always be on their own line with one exception: When being used to declare a function in a class that has no code. Ex:
if(expression)
{
    // Code.
}

class Derived : public Base
{
    public:
        // In this case, it prevents two unneeded lines. This is acceptable and more readable.
        Derived() : Base(variable) {};
};

All blocks and statements should include braces. Even if there is only one line of code executed after the statement is evaluated.

  1. Namespaces should always be lowercase:
namespace fslib
{

}
  1. Classes and types: Upper camel case. Ex:
class ClassName
{
    public:
        ClassName() = default;
    
    private:
        // Member variables.
};

typedef struct
{
    int data;
} StructName;

typedef enum
{
    Type1,
    Type2,
    Type3
} EnumName;
  1. Member functions and methods: Lower snake case. Ex:
class ClassName
{
    public:
        ClassName() = default;

        void member_function_name();
};
  1. Member methods should be called prefixed with the class they belong to to avoid confusion when inheritance and polymorphism is involved. Ex:
class BaseClass
{
    public:
        void member_method();
}

class DerivedClass : public BaseClass
{
    public:
        void member_method_b()
        {
            BaseClass::member_method();
        }
}
  1. Class member variables: Prefixed by either m_ for members, or sm_ for static members followed by lower camel case for the variable name. Ex
class ClassName
{
    public:
        ClassName() = default;

    private:
        int m_classNameVariable = 0;
        static inline int sm_staticSharedVariable = 0;
};
  1. Functions: Lower snake case, prefixed with the namespace they belong to. No namespace x{ in source files. Ex:
void fslib::function_name()
{
    // Do stuff here.
}
  1. Function arguments: Lower camel case. Ex:
void fslib::function_name(int argName)
  1. Local variables: Lower camel case. Ex:
int numberHere = 0;
  1. Global variables should be avoided at all costs. If it can't be, they should be prefixed with g_ followed by lower camel case name. Ex:
int g_globalVariableName = 0;
  1. Static and anonymous namespace variables should be prefixed by s_ followed by lower camel case. Ex:
static int s_variableName = 10;

namespace
{
    constexpr int s_variableName = 10;
}
  1. C style macros and all constants should be all capital with underscores. C style macros should be avoided and used sparingly unless they can drastically clean up code. Ex:
#define MACRO_NAME(x)

static constexpr int CONST_VALUE_NAME = 10;

namespace
{
    constexpr int CONST_VALUE_NAME = 10;
}