Skip to content

Commit 6f9cdc3

Browse files
ninadsachanianico
authored andcommitted
Documentation: Make the SerenityOS Patterns documentation more idiomatic
1 parent bf74a49 commit 6f9cdc3

File tree

1 file changed

+19
-20
lines changed

1 file changed

+19
-20
lines changed

Documentation/Patterns.md

+19-20
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,18 @@
33
## Introduction
44

55
Over time numerous reoccurring patterns have emerged from or were adopted by
6-
the serenity code base. This document aims to track and describe them, so they
6+
the Serenity code base. This document aims to track and describe them, so they
77
can be propagated further and the code base can be kept consistent.
88

99
## `TRY(...)` Error Handling
1010

11-
The `TRY(..)` macro is used for error propagation in the serenity code base.
11+
The `TRY(...)` macro is used for error propagation in the Serenity code base.
1212
The goal being to reduce the amount of boiler plate error code required to
1313
properly handle and propagate errors throughout the code base.
1414

15-
Any code surrounded by `TRY(..)` will attempt to be executed, and any error
15+
Any code surrounded by `TRY(...)` will attempt to be executed, and any error
1616
will immediately be returned from the function. If no error occurs then the
17-
result of the contents of the TRY will be the result of the macro's execution.
17+
result of the contents of the `TRY(...)` will be the result of the macro's execution.
1818

1919
### Examples:
2020

@@ -59,7 +59,7 @@ ErrorOr<Region*> AddressSpace::allocate_region(VirtualRange const& range, String
5959
}
6060
```
6161

62-
Note: Our `TRY(...)` macro functions similarly to the `?` [operator in rust](https://doc.rust-lang.org/book/ch09-02-recoverable-errors-with-result.html#a-shortcut-for-propagating-errors-the--operator).
62+
Note: Our `TRY(...)` macro functions similarly to the `?` [operator in Rust](https://doc.rust-lang.org/book/ch09-02-recoverable-errors-with-result.html#a-shortcut-for-propagating-errors-the--operator).
6363

6464
## `MUST(...)` Error Handling
6565

@@ -130,30 +130,30 @@ private:
130130
}
131131
```
132132

133-
## The `serenity_main(..)` program entry point
133+
## The `serenity_main(...)` program entry point
134134

135135
Serenity has moved to a pattern where executables do not expose a normal C
136-
main function. A `serenity_main(..)` is exposed instead. The main reasoning
136+
main function. A `serenity_main(...)` is exposed instead. The main reasoning
137137
is that the `Main::Arguments` struct can provide arguments in a more idiomatic
138-
way that fits with the serenity API surface area. The ErrorOr<int> likewise
138+
way that fits with the Serenity API surface area. The `ErrorOr<int>` likewise
139139
allows the program to propagate errors seamlessly with the `TRY(...)` macro,
140140
avoiding a significant amount of clunky C style error handling.
141141

142142
These executables are then linked with the `LibMain` library, which will link in
143143
the normal C `int main(int, char**)` function which will call into the programs
144-
`serenity_main(..)` on program startup.
144+
`serenity_main(...)` on program startup.
145145

146146
The creation of the pattern was documented in the following video:
147-
[OS hacking: A better main() for SerenityOS C++ programs](https://www.youtube.com/watch?v=5PciKJW1rUc)
147+
[OS hacking: A better main() for SerenityOS C++ programs](https://www.youtube.com/watch?v=5PciKJW1rUc).
148148

149149
### Examples:
150150

151-
A function `main(..)` would normally look something like:
151+
In C and C++, the main function normally looks something like:
152152

153153
```cpp
154154
int main(int argc, char** argv)
155155
{
156-
return 0;
156+
return 0;
157157
}
158158
```
159159
@@ -222,10 +222,10 @@ class MemoryManager {
222222

223223
## Static Assertions of the size of a type
224224

225-
It's a universal pattern to use `static_assert` to validate the size of a
225+
It's a universal pattern to use `static_assert` to validate that the size of a
226226
type matches the author's expectations. Unfortunately when these assertions
227227
fail they don't give you the values that actually caused the failure. This
228-
forces one to go investigate by printing out the size, or checking it in a
228+
forces one to go investigate by printing out the size, checking it in a
229229
debugger, etc.
230230

231231
For this reason `AK::AssertSize` was added. It exploits the fact that the
@@ -278,10 +278,9 @@ TEST_CASE(string_view_literal_operator)
278278
279279
## Source Location
280280
281-
C++20 added std::source_location, which lets you capture the
282-
callers **FILE** / **LINE** / **FUNCTION** etc as a default
281+
C++20 added [`std::source_location`](https://en.cppreference.com/w/cpp/utility/source_location), which lets you capture the
282+
callers **FILE** / **LINE** / **FUNCTION** etc. as a default
283283
argument to functions.
284-
See: https://en.cppreference.com/w/cpp/utility/source_location
285284
286285
`AK::SourceLocation` is the implementation of this feature in
287286
SerenityOS. It's become the idiomatic way to capture the location
@@ -310,7 +309,7 @@ int main(int, char**)
310309
```
311310

312311
If you only want to only capture `AK::SourceLocation` data with a certain debug macro enabled, avoid
313-
adding `#ifdef`'s to all functions which have the `AK::SourceLocation` argument. Since SourceLocation
312+
adding `#ifdef`'s to all functions which have the `AK::SourceLocation` argument. Since `AK::SourceLocation`
314313
is just a simple struct, you can just declare an empty class which can be optimized away by the
315314
compiler, and alias both to the same name.
316315

@@ -338,9 +337,9 @@ private:
338337

339338
There are four "contiguous list" / array-like types, including C-style arrays themselves. They share a lot of their API, but their use cases are all slightly different, mostly relating to how they allocate their data.
340339

341-
Note that `Span<type>` differs from all of these types in that it provides a _view_ on data owned by somebody else. The four types mentioned above all own their data, but they can provide `Span`'s which view all or part of their data. For APIs that aren't specific to the kind of list and don't need to handle resizing in any way, `Span` is a good choice.
340+
Note that `Span<type>` differs from all of these types in that it provides a _view_ on data owned by somebody else. The four types mentioned above all own their data, but they can provide `Span`s which view all or part of their data. For APIs that aren't specific to the kind of list and don't need to handle resizing in any way, `Span` is a good choice.
342341

343342
- C-style arrays are generally discouraged (and this also holds for pointer+size-style arrays when passing them around). They are only used for the implementation of other collections or in specific circumstances.
344343
- `Array` is a thin wrapper around C-style arrays similar to `std::array`, where the template arguments include the size of the array. It allocates its data inline, just as arrays do, and never does any dynamic allocations.
345344
- `Vector` is similar to `std::vector` and represents a dynamic resizable array. For most basic use cases of lists, this is the go-to collection. It has an optional inline capacity (the second template argument) which will allocate inline as the name suggests, but this is not always used. If the contents outgrow the inline capacity, Vector will automatically switch to the standard out-of-line storage. This is allocated on the heap, and the space is automatically resized and moved when more (or less) space is needed.
346-
- `FixedArray` is essentially a runtime-sized `Array`. It can't resize like `Vector`, but it's ideal for circumstances where the size is not known at compile time but doesn't need to change once the collection is initialized. `FixedArray` guarantees to not allocate or deallocate except for its constructor and destructor.
345+
- `FixedArray` is essentially a runtime-sized `Array`. It can't resize like `Vector`, but it's ideal for circumstances where the size is not known at compile time but doesn't need to change once the collection is initialized. `FixedArray` guarantees to not allocate or deallocate except for in its constructor and destructor.

0 commit comments

Comments
 (0)