Merge pull request #9253 from heinrich5991/pr_ddnet_contributing_if_int

Document that using integers in boolean contexts is not recommended
This commit is contained in:
Dennis Felsing 2024-11-15 22:15:11 +00:00 committed by GitHub
commit aa408ef8c8
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -30,17 +30,17 @@ With the exception of base/system.{h,cpp}
For single words For single words
- `int length = 0;` :x: - `int length = 0;`
- `int Length = 0;` :white_check_mark: - `int Length = 0;`
For multiple words: For multiple words:
- `int maxLength = 0;` :x: - `int maxLength = 0;`
- `int MaxLength = 0;` :white_check_mark: - `int MaxLength = 0;`
### Variable names should be descriptive ### Variable names should be descriptive
:x: Avoid: Avoid:
```C++ ```C++
for(int i = 0; i < MAX_CLIENTS; i++) for(int i = 0; i < MAX_CLIENTS; i++)
@ -55,7 +55,7 @@ for(int i = 0; i < MAX_CLIENTS; i++)
} }
``` ```
:white_check_mark: Instead do: Instead do:
```C++ ```C++
for(int ClientId = 0; ClientId < MAX_CLIENTS; ClientId++) for(int ClientId = 0; ClientId < MAX_CLIENTS; ClientId++)
@ -133,14 +133,14 @@ Do not use the `goto` keyword in new code, there are better control flow constru
Do not set variables in if statements. Do not set variables in if statements.
:x:
```C++ ```C++
int Foo; int Foo;
if((Foo = 2)) { .. } if((Foo = 2)) { .. }
``` ```
:white_check_mark:
```C++ ```C++
int Foo = 2; int Foo = 2;
@ -149,6 +149,22 @@ if(Foo) { .. }
Unless the alternative code is more complex and harder to read. Unless the alternative code is more complex and harder to read.
### Using integers in boolean contexts should be avoided
```C++
int Foo = 0;
if(!Foo) { .. }
```
```C++
int Foo = 0;
if(Foo != 0) { .. }
```
### Methods with default arguments should be avoided ### Methods with default arguments should be avoided
Default arguments tend to break quickly, if you have multiple you have to specify each even if you only want to change the last one. Default arguments tend to break quickly, if you have multiple you have to specify each even if you only want to change the last one.
@ -161,13 +177,13 @@ Try finding descriptive names instead.
While the code base already has a lot of methods that start with a ``Get`` prefix. If new getters are added they should not contain a prefix. While the code base already has a lot of methods that start with a ``Get`` prefix. If new getters are added they should not contain a prefix.
:x:
```C++ ```C++
int GetMyVariable() { return m_MyVariable; } int GetMyVariable() { return m_MyVariable; }
``` ```
:white_check_mark:
```C++ ```C++
int MyVariable() { return m_MyVariable; } int MyVariable() { return m_MyVariable; }
@ -175,7 +191,7 @@ int MyVariable() { return m_MyVariable; }
### Class member variables should be initialized where they are declared ### Class member variables should be initialized where they are declared
Instead of doing this :x:: Instead of doing this :
```C++ ```C++
class CFoo class CFoo
@ -184,7 +200,7 @@ class CFoo
}; };
``` ```
Do this instead if possible :white_check_mark:: Do this instead if possible :
```C++ ```C++
class CFoo class CFoo
@ -211,17 +227,17 @@ Use `bool` instead. And `true` means success and `false` means failure.
See https://github.com/ddnet/ddnet/issues/6436 See https://github.com/ddnet/ddnet/issues/6436
### filenames ### Filenames
Code file names should be all lowercase and words should be separated with underscores. Code file names should be all lowercase and words should be separated with underscores.
:x:
```C++ ```C++
src/game/FooBar.cpp src/game/FooBar.cpp
``` ```
:white_check_mark:
```C++ ```C++
src/game/foo_bar.cpp src/game/foo_bar.cpp