Skip to content

Commit 1b90192

Browse files
vojtabiberleclaude
andcommitted
Add default-first assignment pattern to CLAUDE.md
Document the idiomatic Go pattern for variable assignment: set default value first, then override if present. This avoids the anti-pattern of resetting nil values after assignment. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent 22ea9e5 commit 1b90192

1 file changed

Lines changed: 31 additions & 0 deletions

File tree

CLAUDE.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,37 @@ func buildUID(prefix, name string) string {
226226
- **Dependency management**: Constructor-based DI; interface segregation (small interfaces)
227227
- **Observability**: Structured logging; OpenTelemetry integration; metrics for critical paths
228228
- **Early returns**: Prefer early `return` / `continue` to reduce nesting
229+
- **Default-first assignment**: Set default value first, then override if present. Avoid resetting nil values after assignment.
230+
231+
Example - instead of:
232+
```go
233+
value := someMap[key]
234+
if value == nil {
235+
value = defaultValue
236+
}
237+
```
238+
Use:
239+
```go
240+
value := defaultValue
241+
if v, ok := someMap[key]; ok {
242+
value = v
243+
}
244+
```
245+
246+
Similarly for struct fields:
247+
```go
248+
// Good: set default, override if not nil
249+
items := []string{}
250+
if data.Items != nil {
251+
items = data.Items
252+
}
253+
254+
// Bad: assign then reset if nil
255+
items := data.Items
256+
if items == nil {
257+
items = []string{}
258+
}
259+
```
229260

230261
### Testing
231262
- Test files use `*_test.go` suffix and are located next to implementation

0 commit comments

Comments
 (0)