Skip to content

Commit cbd7dc1

Browse files
authored
Merge pull request #24 from Expensify/aldo_remove-initializer-list-section
Remove initializer_list section
2 parents 212820d + 0c890bb commit cbd7dc1

1 file changed

Lines changed: 5 additions & 31 deletions

File tree

cpp.md

Lines changed: 5 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,7 @@ This section outlines common patterns that cause unnecessary JSON copying in C++
342342

343343
### 1. Use References for Read-Only Operations
344344

345-
When declaring a `JSON::Value` as `const`, it cannot be modified, so we should always use references to avoid unnecessary copies.
345+
When declaring a `JSON::Value` as `const`, it cannot be modified, so we should always use references to avoid unnecessary copies when iterating or passing it into a function.
346346

347347
#### Examples
348348

@@ -396,33 +396,7 @@ These cases are easy to spot when declaring variables as `const` because the con
396396

397397
**Related PR:** [Example implementation](https://github.com/Expensify/Auth/pull/16628/files)
398398

399-
### 2. Avoid Initializer List Constructor for Large Objects
400-
401-
The `JSON::Value(initializer_list)` constructor always creates copies due to a limitation in C++ when using `initializer_list`, which inherently involves copying. This is not an issue specific to the `JSON::Value` implementation. For large objects (transactions, reportActions, reports, personalDetails, participants, etc.), use assignment with `move()` instead to avoid unnecessary copies.
402-
403-
#### Examples
404-
405-
**❌ Bad: Forces copies**
406-
```cpp
407-
return JSON::Value({
408-
{"structuredReportsForOnyx", move(structuredReportsForOnyx)},
409-
{"structuredReportsNVPsForOnyx", move(structuredReportsNVPsForOnyx)},
410-
{"lastMessageData", move(lastMessageData)}
411-
});
412-
```
413-
414-
**✅ Good: Avoids copies**
415-
```cpp
416-
JSON::Value returnValue(JSON::OBJECT);
417-
returnValue["structuredReportsForOnyx"] = move(structuredReportsForOnyx);
418-
returnValue["structuredReportsNVPsForOnyx"] = move(structuredReportsNVPsForOnyx);
419-
returnValue["lastMessageData"] = move(lastMessageData);
420-
return returnValue;
421-
```
422-
423-
**Related PR:** [Example implementation](https://github.com/Expensify/Auth/pull/14365)
424-
425-
### 3. Use Move Semantics and Helper Functions
399+
### 2. Use Move Semantics and Helper Functions
426400

427401
When data is no longer needed after an operation, use `move()` to transfer ownership and avoid copies. Combine with `JSON::Value` helper functions for cleaner code.
428402

@@ -482,7 +456,7 @@ Value& operator=(Value&& v);
482456
483457
**Related PR:** [Example implementation](https://github.com/Expensify/Auth/pull/14820)
484458
485-
### 4. Handle Ternary Operations with References Carefully
459+
### 3. Handle Ternary Operations with References Carefully
486460
487461
Assigning ternary operator results to references can cause copies if one side creates a temporary object.
488462
@@ -503,7 +477,7 @@ const JSON::Value& customLists = policy.hasMember("customLists") ? policy["custo
503477
const JSON::Value& customLists = policy.getMemberWithDefault("customLists", JSON::Utils::EMPTY_ARRAY);
504478
```
505479

506-
### 5. Optimize Functions Returning by Value
480+
### 4. Optimize Functions Returning by Value
507481

508482
Functions returning by value always create copies, even when assigned to reference variables.
509483

@@ -552,7 +526,7 @@ With these overloads, the dangerous code above will call the correct overload an
552526

553527
**Related PR:** [Example implementation](https://github.com/Expensify/Auth/pull/15772/files#r2157875973) (see overloads added in JSON::Value)
554528

555-
### 6. Optimize Transformer Functions
529+
### 5. Optimize Transformer Functions
556530

557531
These are functions that take parameters, transform the data, and return it in a different shape.
558532

0 commit comments

Comments
 (0)