Best Practises » History » Version 1
quintus, 04/17/2021 05:49 PM
| 1 | 1 | quintus | # Best Practises |
|---|---|---|---|
| 2 | |||
| 3 | This page is a list of practises usually followed in the project's code base. |
||
| 4 | |||
| 5 | ## Character encoding (charset) of strings |
||
| 6 | |||
| 7 | C++ does not have a defined encoding (charset, character set) for its string types, most notably `std::string`. It is thus required to always be aware of what encoding a string is in. This project uses `std::string` as its main string type (as opposed to `std::wstring`) and ensures that the character encoding used in this strings is UTF-8. When interfacing with other software, conversion is applied as required. For example, if a programming library returns strings that are not in UTF-8, they are immediately converted to UTF-8 before storing them for later use. The Win32 API as the most prominent example uses UTF-16LE for example, thus interacting with it requires conversion from and to the UTF-8-encoded `std::string` instances used in this project. |
||
| 8 | |||
| 9 | Likewise, all files written by the programme are written in UTF-8, regardless of the platform. BOMs (byte order marks) are not to be used. |
||
| 10 | |||
| 11 | ## Type for filesystem pathes |
||
| 12 | |||
| 13 | Use C++17's `std::filesystem::path` for dealing with pathes on the filesystem. Use `std::filesystem::u8path()` to create an instance of `std::filesystem::path` from an `std::string` encoded in UTF-8. |
||
| 14 | |||
| 15 | ## Inclusion of STL and other namespaces |
||
| 16 | |||
| 17 | In header files, do not include the `std` namespace or any other namespace, but write it out in full. This is to prevent unexpected namespace changes on `#include`. |
||
| 18 | |||
| 19 | In implementation files, do include the `std` namespace. Include other namespaces if it is useful and adds to the readability. Inclusion of namespaces should normally be done towards the beginning of a `.cpp` file, though it might be useful to only include a namespace in a single function. Use readability as the goal for decision. |
||
| 20 | |||
| 21 | The `std::filesystem` namespace is annoying to type even with `std` included. In `.cpp` files, abbreviate it as `fs` like this: |
||
| 22 | |||
| 23 | ~~~~~ c++ |
||
| 24 | namespace fs = std::filesystem; |
||
| 25 | // Now you can access std::filesystem::path more |
||
| 26 | // easily as fs::path. |
||
| 27 | ~~~~~ |