Skip to content

Commit e92991f

Browse files
author
MPCoreDeveloper
committed
docs: replace magic strings with strong-typed references in all Option<T>/Fin<T> examples
1 parent 9bbea2c commit e92991f

3 files changed

Lines changed: 16 additions & 10 deletions

File tree

docs/FUNCTIONAL_NULL_SAFETY.md

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -100,13 +100,18 @@ if (rows != null && rows.Count > 0)
100100
#### Functional (`Option<T>`) — Safe
101101

102102
```csharp
103-
// One expression. Compiler ensures you handle absence. Zero exceptions possible.
104-
var email = (await fdb.GetByIdAsync<UserDto>("Users", 99))
103+
// Strong-typed table reference and named constants — no magic strings.
104+
// Compiler ensures you handle absence. Zero exceptions possible.
105+
var email = (await fdb.GetByIdAsync<UserDto>(Tables.Users, userId))
105106
.Map(u => u.Email)
106-
.Bind(e => string.IsNullOrEmpty(e) ? Option<string>.None : Option<string>.Some(e))
107-
.IfNone("no-email");
107+
.Bind(Option.FromNullOrEmpty) // built-in helper, no ternary
108+
.IfNone(Defaults.NoEmail); // named constant, not a magic string
108109
```
109110

111+
> **Best practice:** Always use strongly-typed table references (e.g., `Tables.Users`) and named
112+
> constants (e.g., `Defaults.NoEmail`) instead of raw strings. `Option<T>` composes cleanly with
113+
> either approach — but strong typing eliminates the "magic string" class of bugs entirely.
114+
110115
#### Error Handling: `Fin<T>` vs Try/Catch
111116

112117
```csharp
@@ -121,7 +126,7 @@ catch (Exception ex)
121126
}
122127

123128
// Functional — errors as values
124-
var result = await fdb.InsertAsync("NonExistent", dto);
129+
var result = await fdb.InsertAsync(Tables.Orders, dto);
125130
result.Match(
126131
Succ: _ => Log("ok"),
127132
Fail: err => Log(err.Message)); // Zero-cost: no exception thrown
@@ -193,7 +198,7 @@ foreach (var row in rows)
193198
{
194199
var email = row
195200
.Map(u => u.Email)
196-
.IfNone("no-email");
201+
.IfNone(Defaults.NoEmail);
197202

198203
Console.WriteLine(email);
199204
}

docs/NULLABLE_VS_OPTIONAL_REBUTTAL.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,10 @@ The statement *"runtime data semantics cannot always be fully proven at compile
4444

4545
## "But Your Example Is Full of Magic Strings"
4646

47-
A fair critique of the `Option<T>` showcase code:
47+
A fair critique of an earlier version of the `Option<T>` showcase code:
4848

4949
```csharp
50+
// ⚠️ Original example (now updated in docs) — shown here for context
5051
var email = (await fdb.GetByIdAsync<UserDto>("Users", 99))
5152
.Map(u => u.Email)
5253
.Bind(e => string.IsNullOrEmpty(e) ? Option<string>.None : Option<string>.Some(e))
@@ -55,7 +56,7 @@ var email = (await fdb.GetByIdAsync<UserDto>("Users", 99))
5556

5657
> *"What if `"Users"` should be `"User"`? What about `"no-email"` — is that a prefix convention? String-based keys, magic strings, ternaries for the logic you're promoting... This is just runtime errors with extra steps."*
5758
58-
**This critique is valid — and it targets the example, not the concept.** Let's separate the two.
59+
**This critique was valid — and we've since updated all documentation examples.** But the deeper point is worth addressing: the complaint targets the *example*, not the *concept*. Let's separate the two.
5960

6061
### What the critique actually proves
6162

src/SharpCoreDB.Functional/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,9 @@ Source: `tests/SharpCoreDB.Functional.Tests/FunctionalSqlSyntaxTests.cs`
8787
var dbf = database.Functional();
8888

8989
var result = await dbf
90-
.GetByIdAsync<User>("Users", 42, cancellationToken: ct)
90+
.GetByIdAsync<User>(Tables.Users, userId, cancellationToken: ct)
9191
.Map(opt => opt.Map(user => user with { LastSeenUtc = DateTime.UtcNow }))
92-
.Map(opt => opt.ToFin("User not found"));
92+
.Map(opt => opt.ToFin(Errors.UserNotFound));
9393

9494
result.Match(
9595
Succ: _ => Console.WriteLine("updated"),

0 commit comments

Comments
 (0)