Skip to content

Commit c0b5ddd

Browse files
Copilotlpcox
andcommitted
Document constructor naming conventions in CONTRIBUTING.md
Co-authored-by: lpcox <15877973+lpcox@users.noreply.github.com>
1 parent 958ec62 commit c0b5ddd

1 file changed

Lines changed: 73 additions & 0 deletions

File tree

CONTRIBUTING.md

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,79 @@ awmg/
246246
- Add Godoc comments for all exported functions, types, and packages
247247
- Mock external dependencies (Docker, network) in tests
248248

249+
### Constructor Naming Conventions
250+
251+
The codebase uses three distinct constructor patterns. Follow these conventions consistently:
252+
253+
#### 1. `New*(args) *Type` - Standard Constructors
254+
255+
Use for simple object creation without error handling or complex initialization.
256+
257+
```go
258+
// Creates a new instance of the type directly
259+
func NewConnection(ctx context.Context) *Connection { ... }
260+
func NewRegistry() *Registry { ... }
261+
func NewSession(sessionID, token string) *Session { ... }
262+
```
263+
264+
**When to use:**
265+
- Object creation is always successful (no errors to return)
266+
- Direct instantiation of struct with provided parameters
267+
- Most common pattern in the codebase (35+ usages)
268+
269+
#### 2. `Create*(args) (Type, error)` - Factory Patterns
270+
271+
Use for factory functions that perform registry lookups or complex configuration-based initialization.
272+
273+
```go
274+
// Looks up a guard type from registry and creates it
275+
func CreateGuard(name string) (Guard, error) { ... }
276+
277+
// Complex initialization with potential failures
278+
func CreateHTTPServerForMCP(cfg *Config) (*http.Server, error) { ... }
279+
```
280+
281+
**When to use:**
282+
- Registry-based object creation (looking up registered types)
283+
- Complex configuration that might fail
284+
- Need to validate parameters and return errors
285+
- Factory pattern with type selection logic
286+
287+
#### 3. `Init*(args) error` - Global State Initialization
288+
289+
Use for initializing global singletons, loggers, or package-level state.
290+
291+
```go
292+
// Initializes global file logger singleton
293+
func InitFileLogger(dir string) error { ... }
294+
295+
// Initializes global JSON logger singleton
296+
func InitJSONLLogger(dir string) error { ... }
297+
```
298+
299+
**When to use:**
300+
- Initializing global variables or package-level state
301+
- Singleton initialization that should only happen once
302+
- Setting up loggers, configuration, or other shared resources
303+
- Typically returns an error if initialization fails
304+
305+
#### Examples from Codebase
306+
307+
**Standard Constructors (`New*`):**
308+
- `NewConnection`, `NewHTTPConnection` (mcp package)
309+
- `NewUnified`, `NewSession` (server package)
310+
- `NewRegistry`, `NewNoopGuard` (guard package)
311+
- `NewLabel`, `NewAgentLabels` (difc package)
312+
313+
**Factory Patterns (`Create*`):**
314+
- `CreateGuard` (guard package) - registry lookup
315+
- `CreateHTTPServerForMCP` (server package) - complex config-based creation
316+
317+
**Global Initialization (`Init*`):**
318+
- `InitFileLogger`, `InitJSONLLogger`, `InitMarkdownLogger`, `InitServerFileLogger` (logger package)
319+
320+
**When in doubt:** Use `New*` for most constructors. Only use `Create*` when implementing factory patterns with type selection, and `Init*` for global state initialization.
321+
249322
### Debug Logging
250323

251324
Use the logger package for debug logging:

0 commit comments

Comments
 (0)