mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-09-11 13:36:13 +03:00
Documentation: Add section about curly braces to CodingStyle.md
This commit is contained in:
parent
b0068c387b
commit
8750e1d080
Notes:
sideshowbarker
2024-07-18 02:13:10 +09:00
Author: https://github.com/linusg Commit: https://github.com/SerenityOS/serenity/commit/8750e1d080 Pull-request: https://github.com/SerenityOS/serenity/pull/16865 Reviewed-by: https://github.com/nico ✅
@ -683,3 +683,72 @@ size_t mask_length = (size_t)((u8)-1) + 1;
|
|||||||
// This should be reinterpret_cast.
|
// This should be reinterpret_cast.
|
||||||
return (u8 const*)string.characters_without_null_termination();
|
return (u8 const*)string.characters_without_null_termination();
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### Omission of curly braces from statement blocks
|
||||||
|
|
||||||
|
Curly braces may only be omitted from `if`/`else`/`for`/`while`/etc. statement blocks if the body is a single line.
|
||||||
|
|
||||||
|
Additionally, if any body of a connected if/else statement requires curly braces according to this rule, all of them do.
|
||||||
|
|
||||||
|
###### Right:
|
||||||
|
```cpp
|
||||||
|
if (condition)
|
||||||
|
foo();
|
||||||
|
```
|
||||||
|
|
||||||
|
```cpp
|
||||||
|
if (condition) {
|
||||||
|
foo();
|
||||||
|
bar();
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
```cpp
|
||||||
|
if (condition) {
|
||||||
|
foo();
|
||||||
|
} else if (condition) {
|
||||||
|
bar();
|
||||||
|
baz();
|
||||||
|
} else {
|
||||||
|
qux();
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
```cpp
|
||||||
|
for (size_t i = i; condition; ++i) {
|
||||||
|
if (other_condition)
|
||||||
|
foo();
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
##### OK:
|
||||||
|
|
||||||
|
```cpp
|
||||||
|
if (condition) {
|
||||||
|
foo();
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
###### Wrong:
|
||||||
|
|
||||||
|
```cpp
|
||||||
|
if (condition)
|
||||||
|
// There is a comment here.
|
||||||
|
foo();
|
||||||
|
```
|
||||||
|
|
||||||
|
```cpp
|
||||||
|
if (condition)
|
||||||
|
foo();
|
||||||
|
else {
|
||||||
|
bar();
|
||||||
|
baz();
|
||||||
|
} else
|
||||||
|
qux();
|
||||||
|
```
|
||||||
|
|
||||||
|
```cpp
|
||||||
|
for (size_t i = i; condition; ++i)
|
||||||
|
if (other_condition)
|
||||||
|
foo();
|
||||||
|
```
|
||||||
|
Loading…
Reference in New Issue
Block a user