# Coding Style
###### tags: `electrical_system` `NTURT`
## Code format
The "esthetic" part of the code (everything other than naming) should follow `google` format, [Google C++ Style Guide](https://google.github.io/styleguide/cppguide.html). And I strong recommand you to read this file as it contains a lot of information about better coding.
### Code formatter
In order to force all code to follow the same "esthetic", code formatter is used, especially, [clang-format](https://clang.llvm.org/docs/ClangFormatStyleOptions.html), and there's a VS code extension for it [xaver.clang-format](https://marketplace.visualstudio.com/items?itemName=xaver.clang-format).
:::info
Note: In order to use clang-format to format your code according to google style, simply add a `.clang-format` file in the root directory of your project with only one line:
```=
BasedOnStyle: Google
```
:::
## Naming convension
### Variable and functions
:::info
snake_case: All lower case letter with space replaced by underscore.
:::
Ex. (C++)
```cpp=
std::string my_state = "i'm fine\n";
std::string how_are_you() {
return my_state;
}
int what_day_it_is(int day) {
return day;
}
```
### Class/Struct name
:::info
PascalCase: Capital for the first letter of every word, without space.
:::
Ex. (C++)
```cpp=
struct MyComputer {
bool it_is_fine_ = false;
};
```
**Not like this**
```cpp=
struct MyComputer_t {
bool it_is_fine_ = false;
};
```
### Preprocessor
#### Define
:::info
SCREAM_CASE: All capital letters with space replaced by underscore.
:::
Ex. (C++)
```cpp=
#define ONE 1
#define MY_AGE 20
```
#### Include guard
:::info
SCREAM_CASE: All capital letters with space replaced by underscore.
:::
Ex. (C++)
```cpp=
#ifndef YAMEL_LOADER_HPP
#define YAMEL_LOADER_HPP
...
#endif // YAMEL_LOADER_HPP
```
without underscore suffixes and prefixes
### Member variables/functions for classes
:::info
Underscore after the member variable name, with function using the normal snake_case naming.
:::
:::info
The order for the accessible of class data is `public`, `private`, and `protected`.
:::
Ex. (C++)
```cpp=
class SomeClass {
public:
SomeClass(bool state, int age) :
state_(state), age_(age) {
// do nothing
}
bool get_state() {
return state_;
}
private:
bool state_;
int age_;
protected:
double weight_;
};
```
**Not like this**
```cpp=
class SomeClass {
protected:
double weight_;
private:
bool state;
int age;
public:
SomeClass(bool state, int age) :
state(state), age(age) {
// do nothing
}
bool get_state_() {
return state;
}
};
```
## Cmake
### File Heirarchy
```
project_root
├── app
│ └── ...
├── doc
│ └── ...
├── include
│ └── ...
├── scripts
│ └── ...
├── src
│ └── ...
├── test
│ └── ...
├── .git
│ └── ...
├── CMakeLists.txt
├── README.md
├── software_development_log.md
└── .gitignore
```
where
- app: Contains executables source files that will be compiled as executables
- doc: Contains Documentation (page description for doxygen) and files read/write during run time (ex. yaml files)
- include: Contains header files (hpp)
- scripts: Contains scripting lauguage (python, bash) files that is interprted at run time
- src: Contains library source files that will only be compiled as library
- test: Contains test soruce files that will be compiled as executables
- .git contains git
- CMakeLists.txt: Instruction for cmake to compile the code
- README.md: The usages and functionalities of this project
- software_development_log.md: Development process of this project
- .gitignore: Files to ignore
### CMakeLists
:::info
1. The target of cmake function is in the same line of the function, otherwise not in the same line.
2. Tab is eight spaces long.
:::
Ex. (cmake)
```cmake=
cmake_minimum_required(VERSION 3.16)
project(project CXX)
set(CMAKE_CXX_STANDARD 17)
include_directories(
include
)
add_executable(executable
executable.cpp
)
```
## Documentation format
For documentation style, please checkout: [Documentation Format](/qh3LSmI9TQC1OI_7pyxDFQ)