-
Notifications
You must be signed in to change notification settings - Fork 2
Code Style Guide
Naming: example_model, with first section sharing the same name as the controller class name
class ExampleModel : public QObject
{
Q_OBJECT
public:
explicit ExampleModel(QObject *parent = 0);
}Naming: example
class Example : public QMainWindow
{
Q_OBJECT;
// Listeners & UI Methods
public:
explicit Example(QWidget *parent = nullptr);
~Example();
// Controller Methods
}Naming: string_containertype.cpp
class ContainerTypeNode {};
class StringContainerType {};Follow google guidelines unless explicitly mentioned below
Code can be kept neat with linters and analysis tool. Below are some recommendations:
-
cppcheck with below arguments
cppcheck --enable=all --inconclusive --std=posix example.cpp"SOLID principles sounded to me like extremely bureaucratic programming that came from the mind of somebody that has not written a lot of code, frankly" - Stack Overflow chairman (Joel Spolsky)
Note : We think SOLID is great but know when not to use it.
-
Don't add any classes to the codebase unless absolutely needed. Best way to go about this is, don't add empty abstract classes before you implement certain functionality, create classes if necessary as you go along
-
Use QT containers instead of CPP STL containers unless it's being used in a custom container
-
Check if QT has existing functionality for the problem before implementing custom functionality or adding external libraries
-
Composition over inheritance
- Use proper tabulation of 4 spaces, do not to tabulate access identifiers
Eg:
class someclass
{
public:
void someFunction();
};
- Attach asterisk or ampersand to the variable name instead of the container name
Eg:
// Use this
int *bar;
// Instead of
int* bar;
-
Use inline functions for code that is only one line
-
Curly braces should be on a new line unless it's an inline function
-
If variables aren't being reassigned use const
-
If methods that aren't manipulating any state use const
-
Use CPP foreach loop instead of an incrementor unless necessary
Eg:
// Use this
for (const auto &e: someList )
{
}
// Intstead of
for (int i = 0; i < someList.length(); i++)
{
}
-
Do not use declarations in a header file
-
Use include guards
-
Avoid magic numbers
-
Use the
define directivewhen declaring a global variable where possible
File Names: Snake case
Classes: Initial capital and then camel case
Enums: Initial capital and then camel case
EnumValues: Initial capital and then camel case
Struct: Initial capital and then camel case
Functions: Camel case
Variables: Camel case
Constants: All caps and snake case
-
Only add includes to header files instead of CPP files unless necessary
-
Use double quotes when including custom files and angled brackets when including library files
-
The order is custom includes first then library includes
Eg:
#include "example.h"
#include "someotherexample.h"
#include <vector>
#include <QObject>-
Have sensible for ordering members in header files
-
The order priority for header file is
sub-classes,public,protectedand then finallyprivate -
The order for members is
enums,structs,data membersand finallymember functions
Eg:
class exampleClass
{
private:
class subClass
{
//...
};
public:
void somePublicFunc();
protected:
int someVariable;
void someProtectedFunc();
private:
void somePrivateFunc();
}- Order function in CPP files according to the order in the header file
- This codebase uses post-increment instead of pre-increment solely for styling and consistency purposes
-
Avoid using asserts as they take a toll on performance, remove all asserts before submitting a pull request
-
Generally, try to write exceptionless code and only throw an exception in case of a disaster