Swift API Design Standards
Source: swift.org - API Design Guidelines
The codebase of Pheron Agent is designed to be completely "Swiftic" (conforming to the philosophy of the Swift language). Based on Apple's official API design guidelines, this document defines the rules to be applied throughout the project.
1. Naming Rules
- Clarity is Primary: Names should clearly indicate what they mean rather than being overly short (
remove(at: index)vsremove(index)). - CamelCase Usage: Types, Classes, Structs, and Protocols must use
UpperCamelCase(e.g.,InferenceActor,ToolExecutable), whereas variables and functions must uselowerCamelCase(e.g.,executeTask,memoryPressureMonitor). - Boolean Naming: Variable names containing or returning a Bool should begin with prefixes like
is,has, orcan(e.g.,isWorkspaceIsolationEnabled). - Protocols: Protocols describing a capability should end with
-able,-ible, or-ing(e.g.,Sendable,Executable).
2. API Structure and Argument Labels
- Fluency: Function calls should read like an English sentence.
- Correct:
x.insert(y, at: z) - Incorrect:
x.insert(y, position: z)
- Correct:
- The first argument typically defines the primary purpose of the function, and its label should be treated as part of the function name. However, use argument labels if argument types are complex or if the context demands them.
3. Code Structure and 'Swiftic' Practices
- Type Safety: Avoid using "String Typing" (e.g., keeping IDs or commands as plain text) to represent values. Instead, use an
enumor strongly typedstruct(e.g., Binary IDs). - Optionals and Error Management: Force-unwrapping (
!) is prohibited. Useguard letorif letfor unexpected states. For asynchronous operations that can throw errors, adoptthrowsand Swift 6's structured error management practices (Typed Throws, Result, etc.). - Prefer Value Types: To prevent Automatic Reference Counting (ARC) overhead and complex state management, use
structinstead ofclassfor data models that do not require actor isolation.