Skip to content

Code Style

Hekbas edited this page Feb 24, 2024 · 1 revision

Code Style

C++

Our engine's code is exclusively written in English, meaning that the whole code, including comments should be written in English. This standard ensures consistency and facilitates collaboration across the development team.

Code Formatting

Before committing changes, ensure that the code is properly formatted. Utilize formatting shortcuts such as CTRL+K, CTRL+D, or use extension tools like CodeMaid to automate the process. Consistently formatted code enhances readability and maintains a uniform coding style throughout the project.

Productivity Standards

Encourage proactive collaboration and effective communication to mitigate potential bottlenecks and streamline development processes. Please, remember that this is a team work, do not hesitate in asking questions to your partners.

By adhering to these guidelines, we foster a conducive environment for collaborative development, promoting code clarity, and ensuring project integrity.

General Conventions

Variables
  • Use understandable and simple words. Avoid useless variables such as 'mondongo', 'placeholder', 'a', 'b'...
  • If the variable name is composed of various words, use lowerCamelCase.
int health; // simple word
float cooldown; 
String characterName; // lowerCamelCase example

In classes, order the variables whether they are public or not. Also, if many variables are instantiated in a single class, a comment that helps ordering the variables is recommended. Then, remember to declare the variables, some of the bugs may occur of not having the variables initially declared.

class Example {
public:

  int health;
  float cooldown;
  String exampleName;

private:

  // Set of variables 1
  int id;  
  list<int> skillIDList;
  
  // Set of variables 2
  bool hasAwaken = false;
  bool transformed = true;
  bool isAlive = true;
  
}
Functions
  • The code should be as much encapsulated as possible, to identify the purpose of the lines in the code.
  • Function names should be named in UpperCamelCase.
class Person 
{
  private:
    string name;
    int age;

  public:
    Person(string name, int age) 
    {
      this->name = name;
      this->age = age;
    }

    void SetName(string name) 
    {
      this->name = name;
    }

    string GetName() 
    {
      return name;
    }

    void SetAge(int age) 
    {
      this->age = age;
    }

    int GetAge() 
    {
      return age;
    }
};
 
int main() 
{
  Person person("John Doe", 30);
 
  cout << "Name: " << person.GetName() << endl;
  cout << "Age: " << person.GetAge() << endl;
 
  person.SetName("Jane Doe");
  person.SetAge(32);
 
  cout << "Name: " << person.GetName() << endl;
  cout << "Age: " << person.GetAge() << endl;
 
  return 0;
}

For newly created functions in .h and .cpp, the name of the function and of the variables should be self explanatory. Follow the example:

void SaveToTextFile(const std::string& filePath, const std::string& content) 
{
  std::ofstream file(filePath);
  if (file.is_open()) 
  {
    file << content;
    file.close();      
  } 
  else 
  {
    // handle errors
  }
}

As have you seen in the previous example, remember to use constor &when needed in order to save memory.

Class & Struct

Both classes and structs should be well separated depending if the variables or functions are private or public. Remember to add an empty space to separate the variable types that are not from the same type or from the same topic.

Class Example

class Example
{
public:
    // Constructor & Destructor
    Example();
    ~Example();

    // Updates
    public PreUpdate();
    public Update();
    public PostUpdate();

    // Utility functions
    public Jump();
    public LoadMeshes();
    public LoadTextures();

    // Getters & Setters
    public GetVarD();
    public SetVarD(int number);

private:
    // Private functions
    private Reload();

public:
    // Public variables
    int varA;
    int varB;
    int varC;

private:
    // Private variables
    int varD;
    int varE;
};

Struct Example

struct Example
{        
    // Variables
    int varA;
    int varB;

    // Functions
    void Sum(int varA, int varB);
    
    // Constructor & Destructor
    Example();
    ~Example();
};

Include

The order of including files to another file should be ordered as: editor files, engine files, third party files and then std files.

// 1. Editor files
#include "EditorModule.h"
// 2. Engine files
#include "EngineModule.h"
// 3. Third party files
#include "imgui.h"
#include "imgui_impl_sdl2.h"
#include "imgui_impl_opengl3.h"
#include "imgui_stdlib.h"
// 4. Std files
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <filesystem>

Conditionals

if (isAlive)
{
   // do something
}

// Do not do this
if (isAlive == true)
{
  // do something
}

// If the condition only contains a single statement.
if (isAlive) isAlive = false;
else isAlive = true;

// or use ternaries
isAlive ? isAlive = false : isAlive = true;
// but if as a coder you do not know how to do ternaries, use the previous example

These guidelines ensure consistency and readability in the codebase.

Loops

for loops

Use for loops when the number of iterations is known. Follow variable conventions from code-conventions.

for (int i = 0; i < n; ++i) 
{
    // your code
}

// Or single statement loops
int counter = 0;
for (int i = 0; i < n; ++i) counter++;

while loops

Use while loops when the number of iterations is unknown. Make sure to specify exit conditions to prevent infinite loops.

bool isAlive() { ... }
while (isAlive()) 
{
    // Loop body
}

// Or single statement loops
int i = 0;
while (i < n) i++;

do-while loops

Use do-while loops when you want to execute the loop body at least once. Make sure to specify exit conditions to prevent infinite loops.

bool isAlive() { ... }
do 
{
    // This will execute at least once
} while (isAlive());

// Or single statement loops
int i = 0;
do ++i;
while (i < n) i++;

Loop control statements

Use break and continue statements to control loop flow. Avoid excessive nesting of loops to maintain code readability. Please, if you are using complex loops, encapsulate the loop into separate functions for improved clarity and maintainability.

For further information, refer to CppCoreGuidelines.