C++ coding style
not sure if my question suits all rules for StackOverflow question, but I
think it will be helpful for future users.
Now I need to choose coding style for a few C++ projects where I'm
participating. These projects are big enough and there will be a few
programmers working on every. So we need to equalize our code style. Also
we want to choose coding style which is socially acceptable, so first I'll
tell what we already decided to have. My question is, if some of our
choices is not socially unaccepted and also what are others commonly using
C++ coding style rules.
So here what we chosen:
File naming
Start with a capital letter and have a capital letter for each new word
(no underscores, no spaces).
For example:
VeryImportantClass.h
VeryImportantClass.cpp
Namespace naming
Start with a capital letter and have a capital letter for each new word
(no underscores, no spaces). Also alignment should be appropriate.
For example:
namespace Drinks
{
namespace AlcoholDrinks
{
}
}
Namespace structure
In header file the have only functions/methods prototypes, realization in
cpp file, avoid using using namespace for realization file.
Example:
//header
namespace CommonStuff
{
namespace SystemParameters
{
bool IfWindows();
//some more stuff...
}
}
//cpp file
namespace CommonStuff
{
namespace SystemParameters
{
bool IfWindows()
{
//some stuff...
return ...;
}
}
}
Classes and structures naming
Start with a capital letter and have a capital letter for each new word
(no underscores, no spaces). No C like class prefix or S like struct
prefix. We decided - it is just more typing.
Example:
class MyClass
{
};
struct MyStruct
{
};
class or struct
In some cases it is difficult to understand if we need class or struct. If
structure just keep some grouped data - it is struct. If structure keeps
data and has methods - it is class. Exceptional methods are constructor,
destructor and comparison operators.
Example:
class MyClass
{
public:
MyClass();
~MyClass();
void SetValue(int value);
int GetValue();
void PrintValue();
private:
int m_value;
};
struct MyStruct
{
MyStruct();
~MyStruct();
int value;
};
Type names
Start with a capital letter and have a capital letter for each new word
(no underscores).
For example:
typedef std::string String;
typedef std::vector<String> StringVector;
Variable types
Use our own predefined types, we have:
typedef std::string String;
typedef std::vector<String> StringVector;
typedef unsigned char Byte;
typedef std::vector<Byte> ByteVector;
//etc.
Variable naming
Start with a lower letter and have a capital letter for each new word (no
underscores).
Example:
String messageLicenseExpired = "Your product version is expired, please...";
int importantNumber = 13;
Class variables naming
Starts with prefix m_ then word starts with a lower letter and have a
capital letter for each new word (no underscores).
Example:
int m_myVariable;
int m_otherVariable;
Constants
Use all capitals with underscores.
Example:
const String PRODUCT_NAME = "our product";
const Byte IMPORTANT_NUMBER = 13;
Constants or preprocessor
If value will be checked using #ifdef or some others, then it must be
preprocessor definition. Otherwise it is const.
For example:
#define FAILURE_FACTOR_FOR_DEBUG 50
const int MAGIC_NUMBER = 5;
//some code...
String newString = someString.substr(MAGIC_NUMBER);
//some code...
//not the best example, but I think it is understandable.
#ifdef _DEBUG
int someValue = FAILURE_FACTOR_FOR_DEBUG;
#else
int someValue = 0;
No comments:
Post a Comment