engine/CODE_STYLE.TXT

161 lines
3.5 KiB
Plaintext
Raw Permalink Normal View History

2024-05-31 22:51:14 +00:00
This document is here to standardise the code style throughout the repo.
################################################################################
######################
# NAMING CONVENTIONS #
######################
# Variables
All variable names in all contexts are snake_case.
Examples: i, my_var, num, player_camera_controller
Static local variables begin with `s_`.
Example:
`
void MyFunc() {
static int s_count = 0;
}
`
Private and protected non-static member variables begin with `m_`.
Private and protected static member variables begin with `s_`.
Public member variables have no prefix and they are accessed with `this->`.
Example:
`
class MyClass {
static int s_instance_count;
bool m_active;
public:
const char* debug_name;
}
`
Global variables, whether extern or static, are prefixed with `g_`.
Example: (in a header file)
`
extern const char* const g_resources_directory;
`
# Functions and methods
Function and method names use lowerCamelCase.
Examples:
`
void doThing();
int addInts(int a, int b);
void MyClass::reset() {}
`
# Classes, Structs, and Enums
Classes, struct, enums, and other types use UpperCamelCase.
Examples:
`
class MyClass;
struct SomeStructure;
enum class Options {
ENABLE,
DISABLE
};
using IntVector = std::vector<int>;
`
# Namespaces
Namespaces only contain lowercase letters. No other characters, including underscores, are used in namespace names.
Example: `namespace engine::util {`
################################################################################
################
# CLASS LAYOUT #
################
Classes are laid out as follows:
`
class X {
/* using declarations */
/* static data members */
/* non-static data members */
public:
/* default constructor */
/* other constructors */
/* copy constructor */
/* move constructor */
/* destructor */
/* copy assignment operator */
/* move assignment operator */
/* other operator overloads */
/* all public static methods */
/* all public non-static methods */
protected:
/* all protected static methods */
/* all protected non-static methods */
private:
/* all private static methods */
/* all private non-static methods */
};
`
2024-06-01 22:03:39 +00:00
Structs are laid out as follows:
`
struct Y {
/* static data members */
/* non-static data members */
/* static methods */
/* non-static methods */
};
`
Avoid having access specifiers in structs.
Structs should almost always be POD. If not, consider using a class instead.
2024-05-31 22:51:14 +00:00
Inlined methods are to be avoided as much as possible. Even for trivial getters/setters.
Inline methods are only used when doing so provides a tangible performance gain.
Classes that should not be copied and/or moved should have their copy and/or move constructors
and copy/move assignment operators explicitly deleted.
Ensure that constructors with a single argument are marked `explicit` unless implicit construction
2024-06-01 22:03:39 +00:00
is intended.
################################################################################
######################
# INCLUDE DIRECTIVES #
######################
Header files should be included in the following order:
- Header corresponding to source file
- C standard library includes (as, e.g., cstdint)
- C++ STL includes
- External libraries from, e.g., 'dependencies/' directory or Vulkan SDK. Use angle brackets.
- Libraries in the source tree. Use double quotes. (TODO: Move these into 'dependencies/')
- Header files from the current project. Use double quotes.
Each section should be sorted alphabetically (In VS: highlight the includes, right-click -> #include directives -> sort).