Code Convention - Sosotess93/bomberman GitHub Wiki
Welcome to the bomberman wiki!
These rules exist to keep the code base manageable while still allowing coders to use C++ language features productively. This code style is strongly inspired by Google’s C++ Style Guide.
In addition to few exception made for this project, the most important parts will be copy in this wiki.
Coding style and formatting are pretty arbitrary, but a project is much easier to follow if everyone uses the same style. Individuals may not agree with every aspect of the formatting rules, and some of the rules may take some getting used to, but it is important that all project contributors follow the style rules so that they can all read and understand everyone's code easily.
Each line of text in your code should be at most 80 characters long. Few exceptions may be made.
Use only spaces, and indent 2 spaces at a time.
We use spaces for indentation. Do not use tabs in your code. You should set your editor to emit spaces when you hit the tab key.
Braces are used with if, else, for, do and while statements, even when the body is empty or contains only a single statement.
No line break before the opening brace except for function declaration. Line break after the opening brace. Line break before the closing brace. *Line break after the closing brace except for if / else or if that brace terminates a statement. Example:
if (condition) {
...
} else if (...) {
...
} else {
...
}
for (auto const& it : vec) {
...
}
while (true) {
...
}
void funDeclaration(int nb)
{
...
}
void emptyFunction(void)
{
...
}
Avoid virtual method calls in constructors, and avoid initialization that can fail if you can't signal an error.
Use a struct only for passive objects that carry data; everything else is a class. For consistency with STL, you can use struct instead of class for functors and traits.
Declaration Order Use the specified order of declarations within a class: public: before private:, methods before data members (variables), etc.
Your class definition should start with its public: section, followed by its protected: section and then its private: section. If any of these sections are empty, omit them. Within each section, the declarations generally should be in the following order:
- Using-declarations, Typedefs and Enum.
- Constants (static const data members)
- Constructors
- Destructor
- Methods, including static methods
- Data Members (except static const data members)
Example:
class MyClass : public OtherClass
{
public:
MyClass(void);
MyClass(int var);
~MyClass(void);
void SomeFunction(void);
public:
typedef uint32_t Time;
enum class ErrorType : int
{
ET_BASIC = 0,
ET_CRITIC = 1
};
private:
bool someInternalFunction();
int _someVar;
int _someOtherVar;
};
The method declaration in .cpp file must follow the prototype order.
##Naming
Filenames should be all lowercase and use underscores (_) as delimiter. C++ files should end in .cpp and header files should end in .hpp. In general, make your filenames very specific. For example, use http_server_logs.h rather than logs.h. A very common case is to have a pair of files called, e.g., foo_bar.h and foo_bar.cc, defining a class called FooBar.
Ordinarily, functions should start with a capital letter and have a capital letter for each new word (a.k.a. "upper camel case" or "Pascal case"). Such names should not have underscores. Prefer to capitalize acronyms as single words
All private member attribute's name must start with an underscore
All object member have to be explicitly marks using this-> keyword
<stdint.h>
defines types like int16_t
, uint32_t
, int64_t
, etc. You should always use those in preference to short, unsigned long long and the like, when you need a guarantee on the size of an integer. Of the C integer types, only int should be used.
Use 0 for integers, NULL for pointers, and '\0' for chars.
Prefer sizeof(varname) to sizeof(type).
Use sizeof(varname) when you take the size of a particular variable. sizeof(varname) will update appropriately if someone changes the variable type either now or later.