swift-style by johnrogers/claude-swift-engineering
npx skills add https://github.com/johnrogers/claude-swift-engineering --skill swift-style编写清晰、可读 Swift 代码的风格规范。
清晰性 > 简洁性 > 一致性
代码应编译无误且无警告。
UpperCamelCase — 类型、协议lowerCamelCase — 其他所有情况// 推荐
let maximumWidgetCount = 100
func fetchUser(byID id: String) -> User
左侧边距应为正常执行路径。避免嵌套 if 语句。
// 推荐
func process(value: Int?) throws -> Result {
guard let value = value else {
throw ProcessError.nilValue
}
guard value > 0 else {
throw ProcessError.invalidValue
}
return compute(value)
}
广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
使用扩展和 MARK 注释:
class MyViewController: UIViewController {
// 核心实现
}
// MARK: - UITableViewDataSource
extension MyViewController: UITableViewDataSource { }
除非编译器要求,否则避免使用 self。
// 推荐
func configure() {
backgroundColor = .systemBackground
}
只读属性省略 get:
var diameter: Double {
radius * 2
}
仅当参数为单个闭包时使用尾随闭包语法。
在清晰的情况下让编译器推断类型。对于空集合,使用类型注解:
var names: [String] = []
// 推荐
var items: [String]
var cache: [String: Int]
var name: String?
private 而非 fileprivateinternal(它是默认值)resource.request().onComplete { [weak self] response in
guard let self else { return }
self.updateModel(response)
}
// 或 ///,避免 /* */使用无大小写枚举进行命名空间管理:
enum Math {
static let pi = 3.14159
}
使用 URL、ID、UUID 之外的缩写 — 像 cfg、mgr、ctx、desc 这样的缩写会损害可读性。请完整拼写:configuration、manager、context、description。仅 URL、ID、UUID 这三个是例外。
嵌套 guard/if 语句 — 深层嵌套使代码难以理解。使用提前返回和 guard 语句来保持正常路径左对齐。
不一致的 self 使用 — 要么始终省略 self(推荐),要么始终使用它。混合使用会使代码扫描更困难,并混淆捕获语义。
过于泛化的类型名称 — Manager、Handler、Helper、Coordinator 等名称过于模糊。名称应解释职责:PaymentProcessor、EventDispatcher、ImageCache、NavigationCoordinator。
隐含的访问控制 — 不要跳过访问控制。显式的 private、public 有助于未来的维护者理解模块边界。internal 是默认值,因此省略它。
每周安装量
1.3K
代码仓库
GitHub 星标数
176
首次出现
2026年1月23日
安全审计
安装于
codex1.1K
opencode1.1K
gemini-cli1.1K
github-copilot1.1K
kimi-cli1.1K
amp1.1K
Code style conventions for clean, readable Swift code.
Clarity > Brevity > Consistency
Code should compile without warnings.
UpperCamelCase — Types, protocols
lowerCamelCase — Everything else
Clarity at call site
No abbreviations except universal (URL, ID)
// Preferred let maximumWidgetCount = 100 func fetchUser(byID id: String) -> User
Left-hand margin is the happy path. Don't nest if statements.
// Preferred
func process(value: Int?) throws -> Result {
guard let value = value else {
throw ProcessError.nilValue
}
guard value > 0 else {
throw ProcessError.invalidValue
}
return compute(value)
}
Use extensions and MARK comments:
class MyViewController: UIViewController {
// Core implementation
}
// MARK: - UITableViewDataSource
extension MyViewController: UITableViewDataSource { }
Avoid self unless required by compiler.
// Preferred
func configure() {
backgroundColor = .systemBackground
}
Omit get for read-only:
var diameter: Double {
radius * 2
}
Trailing closure only for single closure parameter.
Let compiler infer when clear. For empty collections, use type annotation:
var names: [String] = []
// Preferred
var items: [String]
var cache: [String: Int]
var name: String?
private over fileprivateinternal (it's the default)resource.request().onComplete { [weak self] response in
guard let self else { return }
self.updateModel(response)
}
// or ///, avoid /* */Use case-less enum for namespacing:
enum Math {
static let pi = 3.14159
}
Abbreviations beyond URL, ID, UUID — Abbreviations like cfg, mgr, ctx, desc hurt readability. Spell them out: configuration, manager, context, description. The three exceptions are URL, ID, UUID.
Nested guard/if statements — Deep nesting makes code hard to follow. Use early returns and guards to keep the happy path left-aligned.
Inconsistent self usage — Either always omit self (preferred) or always use it. Mixing makes code scanning harder and confuses capture semantics.
Weekly Installs
1.3K
Repository
GitHub Stars
176
First Seen
Jan 23, 2026
Security Audits
Gen Agent Trust HubPassSocketPassSnykPass
Installed on
codex1.1K
opencode1.1K
gemini-cli1.1K
github-copilot1.1K
kimi-cli1.1K
amp1.1K
React 组合模式指南:Vercel 组件架构最佳实践,提升代码可维护性
102,200 周安装
Overly generic type names — Manager, Handler, Helper, Coordinator are too vague. Names should explain responsibility: PaymentProcessor, EventDispatcher, ImageCache, NavigationCoordinator.
Implied access control — Don't skip access control. Explicit private, public helps future maintainers understand module boundaries. internal is default, so omit it.