-
Notifications
You must be signed in to change notification settings - Fork 1
Coding Style (CPP)
The C++ code in this project follows the following standard.
-
All variables (arguments), namespaces, functions (methods) and files should be in
camelCase
. Classes should be inPascalCase
. -
Names may contain ONE underscore (
_
) at the beginning or the end of the name, in order to diffrentiate from the keywords or other variables in the parent scope. -
Good:
class MyClass
{
int myVariable = 0;
void myFunction(
int myArgumentA,
int myArgumentB_,
int _myArgumentC
);
};
- Bad:
class my_class // Not PascalCase.
{
int my_variable = 0; // Not camelCase.
void my_function(
int my_argument_a, // Not camelCase.
int my_argument_b, // Not camelCase.
int __my_argument_c // Not camelCase, and two underscores.
);
};
-
Use 4 spaces for indentation. Do NOT use tabs.
-
Good:
void myFunction()
{
if (true)
{
// Do something.
}
}
- Bad:
void myFunction()
{
// Not indented properly.
if (true)
{
// Do something.
}
}
- Braces should be on the next line of the statement, and should NOT be on the same line.
- Braces should NOT be indented.
Tip
- Single-line statements need not have braces. Most formatters will get rid of them anyway.
- Indent single-line statements for differentiation.
- Good:
void myFunction()
{
if (true)
{
// Do something.
}
}
- Good:
void myFunction()
{
if (true)
doSomething();
}
- Bad:
void myFunction() {
if (true) { // Braces on the same line.
// Do something.
}
}
- Bad:
void myFunction()
{
if (true)
{
// Braces indented.
}
}
-
Both single-line (
//
) and multi-line (/* */
) comments are allowed. There is not a strict rule for this. -
Use English for comments. Be clear and concise with the wording.
-
Add ONE space before the comment delimiter (
//
), if it is a single-line comment, and after it. -
Good:
// This is a single-line comment.
/*
* This is a multi-line comment.
*/
int main()
{
doSomething(); // This is a in-line comment.
}
- Bad:
//This is a single-line comment.
/*
This is a multi-line comment. There is no space after the delimiter, or at the beginning.
*/
int main()
{
doSomething();//This is a single-line comment.
}
-
Use
#pragma once
for header guards. Do not#define
macros for this. -
Good:
#pragma once
class MyClass
{
// Class definition.
};
- Bad:
#ifndef MY_CLASS_H
#define MY_CLASS_H
class MyClass
{
// Class definition.
};
#endif
-
Use namespaces to group related classes and functions.
-
Put everything, except the
main
function, in a namespace. -
Add a comment to the end of the namespace.
-
Example:
namespace steppable::__internals::arithmetic
{
class MyClass
{
// Class definition.
}
void myFunction()
{
// Function definition.
}
} // namespace steppable::__internals::arithmetic
Important
For the time being, put everything into the steppable::__internals::arithmetic
namespace.
You can use features from the C++ 23 standard. Although the old functions are more compatible, you are encouraged to use the more modern features.
util.hpp
also provides a range of functions. See API Reference for more information.
Caution
Do NOT write using namespace std;
in the code. Doing so makes it easy to override the standard functions.
Some Steppable functions may have the same names as the STL functions, but they take in strings instead.
using namespace std;
will cause confusion and errors while compiling.
Note
- It is fine to write
using namespace
with anything insidestd
orsteppable
, but notstd
orsteppable
itself. - The statement
namespace a = b;
is fine as well, as long asa
is notstd
or anything starting withsteppable
.
Good:
#include <iostream>
using std::cout;
namespace a = b;
int main()
{
cout << a::myFunction();
}
Bad:
#include <iostream>
using namespace std;
namespace steppable
{
int main()
{
cout << myFunction();
}
}
-
Always put a space after a comma.
-
Always put a space before and after an operator.
-
Always put a space after a bracket if you are creating any sort of list (e.g., initializing an array or initializer list).
-
Good:
int myFunction(int myArgumentA, int myArgumentB)
{
int myArray[] = { 1, 2, 3 };
int myVariable = myArgumentA + myArgumentB;
}
- Bad:
int myFunction(int myArgumentA,int myArgumentB)
{
int myArray[] = {1,2,3};
int myVariable = myArgumentA+myArgumentB;
}
-
Each file should be structured as follows:
- License header.
- Header guards.
- Include statements.
- Using statements.
- Namespace.
- Variables / constants / pointers.
- Class / struct / union definitions
- Function definitions
-
The order of the elements in the file should be as follows:
- Public members.
- Protected members.
- Private members.
Tip
If you are still confused by these rules, the following tools may help you make the code comply with them automatically.
-
ClangFormat can help you format your code according to the standards. There is a
.clang-format
file in the project root directory, which you can use to format your code.To format your code, you can use the following command:
clang-format -i <file>
-
add_copyright_header.py
is a Python script that can add a header to your files. You can use this script to add headers to your files instead of adding them manually.To use the script, you can use the following command (you need to have Python 3 installed on your system):
python3 add_copyright_header.py <file>
Copyright (C) Andy Zhang, 2023-2024. Licensed under the MIT License.
- Getting the Source - Cloning the repository to your local machine
- Build the Project (CMake) - Compiling the source code of Steppable using CMake
- Build the Project (build.py) - Compiling the source code of Steppable using build.py
- Workflow - How to contribute to this repository
- Coding Style (Python) - Style guidelines for Python code in this project
- Coding Style (C++) - Style guidelines for CPP code in this project
- Directory Structure - The directory structure used by this project.
- Using the API - How to use the Steppable API
- Supported Platforms
- Performance - Some benchmarks of Steppable for reference
- Status - Status of Steppable, at a glance