Code linting
A linter will parse your code in search of bad practices and point them to you.
Code formatting
A code formatter is an automated tools that help you format source code automatically.
Code formatters are useful for a variety of reasons. First and foremost, they help to standardize code formatting, which makes it easier to read and understand code. This is particularly important when working on large projects like EDRES with multiple developers, where everyone needs to be able to read and understand each other's code.
The main benefits of a standardized coding style are :
- Higher code readability
- Easier merging since there are no style conflicts
The importance of code formatting in the code base
If any difference in pushed file is found between formatted file & pushed files will result in a failure of the CI pipeline.
How to set up a code formatter on vscode
- Python : https://code.visualstudio.com/docs/python/formatting
- C++ : https://code.visualstudio.com/docs/cpp/cpp-ide#\_code-formatting
C++
clang-format
Clang-format is a code formatter for the following languages : C / C++ / Java / JavaScript / JSON / Objective-C / Protobuf / C#
This CLI tool works in a straightforward manner. It needs :
- A file to format.
- Optionnaly a formatting style, basic formatting style is LLVM.
Installation
wget https://apt.llvm.org/llvm.sh
chmod u+x llvm.sh
./llvm.sh 20 # require sudo
apt install clang-format-20
Usage
To format your code using a .clang-format file style you need to use the following command
clang-format -style=<string> file.c/cpp/...
The style argument can take multiple values but the one that interesses use is the file
value which allows to specify how the code can through a .clang-format
file :
clang-format -i -style=file:. <path/to/file/to/format>
Arguments
-
-i
tells to make an inplace edition : the input file is the output file -
-style=file:.
tells that we have our own .clang-format file and that the path to it is.
ifclang-format
doesn't find it there it will look in a parent directory until he finds it.
Python
ruff
Ruff is an extremely fast Python linter and code formatter, written in Rust.
Installation
pip install ruff
Usage
For formatting
ruff format
For linting
ruff check