-
Notifications
You must be signed in to change notification settings - Fork 13
Programming Style Guide
Arsen Tufankjian edited this page Dec 21, 2015
·
12 revisions
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.
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
};
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