-
Notifications
You must be signed in to change notification settings - Fork 13
Programming Style Guide
This is a very general overview and subject to change. If you have questions, contact .
This is C++ not C so you will probably be writing or modifying classes. If you know a Class would never have any methods or be subject to inheritance a struct is fine. Typedefs are frowned upon unless it aids in portability.
It's not the 90s anymore and we prefer it that way. If there is a C++11 semantic that you want to use, feel free so long as it doesn't screw up VS2013 support.
The only exception to this is with OpenGL function calls that can accept NULL. In that case we prefer to pass 0 rather than NULL due to some platforms that enjoy throwing warnings.
Try to avoid committing any code that produces warnings to the master branch. If a warning occurs during build please fix it; it's usually trivial and helps keep the build stable.
Please prefix all virtual functions with V
class IFoo
{
public:
virtual void VTest() = 0;
};
Use trailing braces when defining blocks.
Bad:
class Foo{
};
Good:
class Foo
{
};
Bad:
if (x > 0){
}
Good:
if (x > 0)
{
}
Bad:
if (x < 0)
{
//Single Line if
}
Good:
if (x < 0)
//Single Line if
An exception to this is is if you have an if/else where only one could be single line. Use blocks for both in that case.
Bad:
if (x > 0)
//Single Line If
else
{
//Multi Line Else
}
Good:
if (x > 0)
{
//Single Line If
}
else
{
//Multi Line Else
}
class MyClass;
void MyFunction();
class MyClass
{
int myMember;
};
void MyMethod(int myParam);
#define MY_PI 3.14159
enum MyEnum
{
FIRST_VALUE,
SECOND_VALUE
};
enum class MyEnumClass: char
{
high = 'h',
low = 'l'
};
Bad:
//This adds x to y
int Add(int x, int y)
{
return x + y;
}
- If you are writing documentation that is a different story
Good:
//If you register a permission with a name that already exists, you will overwrite the existing permission
void RegisterPermission(const char* name, int id)
{
//Code
}
We really don't want to have to go through the daunting task back tracking through old code to document it. If you're unfamiliar with Doxygen
Bad:
// Comment
Good:
//Comment
Bad:
/* Multi
Line
Comment
*/
- This doesn't apply to documentation stubs
Good:
/*
Multi
Line
Comment
*/
Bad:
/*
Multi
Line
Comment
*/
Bad:
/*Multi
*Line
*Comment
*/
- This doesn't apply to copyright notices or documentation stubs
- Use .h for headers and .cpp for source files
- Header and Source names should match or at least contain their class name
- Use multiple Source files where appropriate (MyClass.hpp, MyClass.cpp, MyClassWindows.cpp, MyClassLinux.cpp)
- Source files go in
src/
and Headers go ininclude/
#pragma once
/*
Copyright notice if applicable
*/
//Pre-Include Defines
//System level includes
//Project level includes
/*
Any important info about MyClass
OR
Info for documentation engine (cldoc / doxygen / etc)
*/
class MyClass
{
friend class MyFriend;
public:
//Static Methods
//Static Variables
MyClass();
//Public Methods
//Public Variables
virtual ~MyClass();
protected:
//Static Methods
//Static Variables
//Protected Methods
//Protected Variables
private:
//Static Methods
//Static Variables
//Private Methods
//Private Variables
};
Notes:
- Do not use an #ifdef header guard
//Includes
//ALL Static Methods
//ALL Static Variables
MyClass::MyClass()
{
}
//Public Methods
MyClass::~MyClass()
{
}
/*
Make a comment that you're now writing protected methods
*/
//Protected Methods
/*
Make a comment that you're now writing private methods
*/
//Private Methods
All .h and .cpp files should start with a license notification