More style updates
This commit is contained in:
16
style.md
16
style.md
@@ -75,8 +75,9 @@ uint32_t initial_block_size_;
|
|||||||
### Structs
|
### Structs
|
||||||
- **PascalCase** for struct names
|
- **PascalCase** for struct names
|
||||||
- **Always use struct** - eliminates debates about complexity and maintains consistency
|
- **Always use struct** - eliminates debates about complexity and maintains consistency
|
||||||
- **Public members first, private after** - leverages struct's default public access
|
- **Public members first, private after** - puts the interface users care about at the top, implementation details below
|
||||||
- Use `private:` sections when encapsulation is needed
|
- **Full encapsulation still applies** - use `private:` sections to hide implementation details and maintain deep, capable classes
|
||||||
|
- The struct keyword doesn't mean shallow design - it means interface-first organization for human readers
|
||||||
```cpp
|
```cpp
|
||||||
struct ArenaAllocator {
|
struct ArenaAllocator {
|
||||||
// Public interface first
|
// Public interface first
|
||||||
@@ -253,6 +254,7 @@ for (auto &precondition : preconditions_) {
|
|||||||
- **Prefer unique_ptr** for exclusive ownership
|
- **Prefer unique_ptr** for exclusive ownership
|
||||||
- **shared_ptr only if shared ownership is necessary** - most objects have single owners
|
- **shared_ptr only if shared ownership is necessary** - most objects have single owners
|
||||||
- **Factory patterns** for complex construction and ownership control
|
- **Factory patterns** for complex construction and ownership control
|
||||||
|
- **STL containers with arena allocators require default construction after arena reset** - `clear()` is not sufficient
|
||||||
```cpp
|
```cpp
|
||||||
// Static factory methods for complex objects requiring specific initialization
|
// Static factory methods for complex objects requiring specific initialization
|
||||||
auto server = Server::create(config, handler); // Ensures shared_ptr semantics
|
auto server = Server::create(config, handler); // Ensures shared_ptr semantics
|
||||||
@@ -271,6 +273,12 @@ private:
|
|||||||
|
|
||||||
// Usage in Server
|
// Usage in Server
|
||||||
auto conn = std::unique_ptr<Connection>(new Connection(args));
|
auto conn = std::unique_ptr<Connection>(new Connection(args));
|
||||||
|
|
||||||
|
// STL containers with arena allocators - correct reset pattern
|
||||||
|
std::vector<Operation, ArenaStlAllocator<Operation>> operations(arena_alloc);
|
||||||
|
// ... use container ...
|
||||||
|
operations = {}; // Default construct - clear() won't work correctly
|
||||||
|
arena.reset(); // Reset arena memory
|
||||||
```
|
```
|
||||||
|
|
||||||
### Resource Management
|
### Resource Management
|
||||||
@@ -383,8 +391,8 @@ TEST_CASE("ArenaAllocator basic allocation") {
|
|||||||
- **Prefer testing through public interfaces** - focus on observable behavior rather than implementation details
|
- **Prefer testing through public interfaces** - focus on observable behavior rather than implementation details
|
||||||
- **Test the contract, not the implementation** - validate what the API promises to deliver
|
- **Test the contract, not the implementation** - validate what the API promises to deliver
|
||||||
- **Avoid testing private methods directly** - if private functionality needs testing, consider if it should be public or extracted
|
- **Avoid testing private methods directly** - if private functionality needs testing, consider if it should be public or extracted
|
||||||
- **Integration over isolation** - test components working together when practical
|
- **Both integration and unit tests** - test components in isolation and working together
|
||||||
- **Mock only external dependencies** - prefer real implementations for internal components
|
- **Prefer fakes to mocks** - use real implementations for internal components, fake external dependencies
|
||||||
```cpp
|
```cpp
|
||||||
// Good: Testing through public API
|
// Good: Testing through public API
|
||||||
TEST_CASE("Server accepts connections") {
|
TEST_CASE("Server accepts connections") {
|
||||||
|
|||||||
Reference in New Issue
Block a user