Swift Comments That Matter
Compatibility note: this SKILL.md is the portable skill entrypoint for runtimes that load skill packages directly, including Cursor-compatible installs.
Canonical standard lives at standards/swift-comments-that-matter/STANDARD.md.
Purpose
Use this skill to produce comments that help future maintainers make safe changes.
Default rule:
- If text explains what the code does, improve the code.
- If text explains why code exists or what must not break, write the comment.
When To Use This Skill
Apply this skill when:
- reviewing
/// comments in Swift code
- refactoring low-signal comments
- documenting API contracts and invariants
- clarifying hidden behavior, side effects, or concurrency risks
Start Here
- Apply the decision flow in this file.
- Use the rewrite pattern: no comment -> bad -> good -> best.
- Follow writing guardrails and voice calibration.
Read When Needed
- Tone and context: docs/context.md
- Decision details: docs/decision-rules.md
- Quality checks: docs/checklist.md
- Example constraints: docs/example-constraints.md
- DocC boundaries: docs/docc-guidance.md
- Golden references: docs/golden-examples.md
- Review red flags: docs/review-red-flags.md
Quick Routing
- Quick review path:
docs/checklist.md
- Rewrite path:
docs/decision-rules.md + docs/golden-examples.md
- Review anti-pattern path:
docs/review-red-flags.md
- DocC boundary path:
docs/docc-guidance.md
Core Principle
If the text explains what the code does -> improve the code.
If the text explains why it exists or what must not break -> write the comment.
Contract Hierarchy
Prefer stronger sources of truth before prose:
- encode the contract in types, isolation, ownership, availability, diagnostics, or API shape;
- verify the contract with tests or preconditions when it is executable;
- comment only the decision, boundary, trade-off, or failure mode still invisible;
- move broader system context to DocC when it no longer belongs to one symbol.
Refactor-First Rule
Before writing a comment, first consider:
- renaming variables/functions/types
- extracting smaller functions
- simplifying control flow
If readability can be improved, refactor first.
Decision Flow
Before writing a comment, ask:
- Can naming or structure remove the need for the comment?
- Can types, actor isolation, ownership, availability, diagnostics, tests, or preconditions express the contract?
- Is there hidden framework behavior not obvious from code?
- Does correctness depend on identity, ordering, lifetime, cancellation, isolation, ownership, or backpressure?
- Is there a generated artifact or external source of truth?
- Does the comment include a verifiable condition for remaining true or being removed?
- Could another developer misuse this API or flow without compiler/test feedback?
If all answers are "no", do not comment.
Preferred Comment Sections
Use only when needed:
Important:
Why:
Assumption:
Constraint:
Invariant:
Risk:
Side Effects:
Concurrency:
Cancellation:
Isolation:
Ownership:
Lifetime:
Observation:
Backpressure:
Performance:
Compatibility:
Generated:
Prefer the most precise section. Use Concurrency: only for broad concurrency context; use Cancellation:, Isolation:, Ownership:, or Backpressure: when one of those is the actual risk.
When Not To Comment
Do not comment when:
- the sentence only repeats implementation
- the name can carry the meaning after small refactor
- the statement is generic and risk-free
- the comment adds length but no new decision context
Example Pattern Rule
Each scenario must include:
- No comment version (clean but incomplete)
- Bad comment version (common anti-pattern)
- Good comment version (useful)
- Best version (intent + constraints + reasoning)
Required Scenarios
The skill must cover:
- token refresh with concurrency issues
- download manager deduplication invariant
- cache eviction assumptions
- payment logic business constraints
- pricing and currency rounding constraints
- SwiftUI async lifecycle edge cases
- SwiftUI identity, lazy-container state lifetime, and repeatable
onAppear
- Observation dependencies that are registered implicitly by framework reads
- cancellation boundaries, cleanup, rollback, and point-of-no-return behavior
- actor isolation, task ownership, and non-copyable ownership/lifetime constraints
- stream ordering, cancellation, and backpressure
- generated-code source-of-truth and regeneration boundaries
- local diagnostic exceptions with a removal condition
- performance constraints backed by reproducible evidence
- compatibility workarounds with explicit removal conditions
- background task scheduling limitations
- analytics side effects
- public API contracts in frameworks
Anti-Patterns
Avoid:
- "This function does..."
- "This method is responsible for..."
- "This class..."
- line-by-line narration
- tutorial-style toy examples
- long comments with no constraints or risk
- TODOs without owner, condition, or exit criteria
- "temporary" or workaround comments without version, issue, or removal event
- performance claims without metric, fixture, trace, or benchmark
- comments that contradict types, compiler diagnostics, or tests
Writing Guardrails
When generating comments:
- NEVER start with:
- "This function..."
- "This method..."
- "This class..."
- Prefer direct statements, constraints, risks, and reasoning.
- Keep comments concise. Remove filler words.
Voice Calibration
Write like an experienced iOS engineer speaking to another engineer:
- human and direct
- no buzzwords
- no marketing tone
- no over-polished generic phrasing
DocC Boundary Rule
Use inline /// comments for:
- contracts
- invariants
- assumptions
- side effects
- local compatibility or performance constraints
Use DocC articles for:
- architecture explanations
- system flows
- domain concepts
- cross-module behavior
- version-dependent behavior spanning multiple symbols
Generated code boundary:
- do not add manual documentation to generated artifacts;
- document the schema, specification, or stable wrapper that owns the contract;
- include the regeneration command or process when it is not discoverable.
Compatibility Note
Examples target modern Swift codebases using Swift 6.3+/Xcode 27 concepts, Swift Concurrency, SwiftUI lifecycle behavior, Observation, and generated-code workflows. The principle is retrocompatible with older Swift projects: encode or test what you can, then comment only what remains invisible.
Examples
- examples/bad-comments.swift
- examples/better-comments.swift
- examples/concurrency.swift
- examples/invariants.swift
- examples/api-contracts.swift
- examples/pricing-rounding.swift
- examples/modern-contracts.swift
Additional Resources
- docs/principles.md
- docs/context.md
- docs/decision-rules.md
- docs/checklist.md
- docs/example-constraints.md
- docs/golden-examples.md
- docs/docc-guidance.md
- docs/review-red-flags.md
1---2name: swift-comments-that-matter3description: Write high-value Swift comments that explain intent, invariants, constraints, side effects, Swift Concurrency risks, SwiftUI lifecycle behavior, generated-code boundaries, and contracts not fully expressed by types or tests. Use when reviewing or authoring comments in iOS/macOS/watchOS/tvOS/visionOS codebases, especially to replace low-signal "what it does" comments with concise "why, what must not break, and when this guidance can be removed" documentation.4---56# Swift Comments That Matter78Compatibility note: this `SKILL.md` is the portable skill entrypoint for runtimes that load skill packages directly, including Cursor-compatible installs.9Canonical standard lives at `standards/swift-comments-that-matter/STANDARD.md`.1011## Purpose1213Use this skill to produce comments that help future maintainers make safe changes.1415Default rule:16- If text explains what the code does, improve the code.17- If text explains why code exists or what must not break, write the comment.1819## When To Use This Skill2021Apply this skill when:22- reviewing `///` comments in Swift code23- refactoring low-signal comments24- documenting API contracts and invariants25- clarifying hidden behavior, side effects, or concurrency risks2627## Start Here28291. Apply the decision flow in this file.302. Use the rewrite pattern: no comment -> bad -> good -> best.313. Follow writing guardrails and voice calibration.3233## Read When Needed3435- Tone and context: [docs/context.md](docs/context.md)36- Decision details: [docs/decision-rules.md](docs/decision-rules.md)37- Quality checks: [docs/checklist.md](docs/checklist.md)38- Example constraints: [docs/example-constraints.md](docs/example-constraints.md)39- DocC boundaries: [docs/docc-guidance.md](docs/docc-guidance.md)40- Golden references: [docs/golden-examples.md](docs/golden-examples.md)41- Review red flags: [docs/review-red-flags.md](docs/review-red-flags.md)4243## Quick Routing4445- Quick review path: `docs/checklist.md`46- Rewrite path: `docs/decision-rules.md` + `docs/golden-examples.md`47- Review anti-pattern path: `docs/review-red-flags.md`48- DocC boundary path: `docs/docc-guidance.md`4950## Core Principle5152If the text explains what the code does -> improve the code.53If the text explains why it exists or what must not break -> write the comment.5455## Contract Hierarchy5657Prefer stronger sources of truth before prose:581. encode the contract in types, isolation, ownership, availability, diagnostics, or API shape;592. verify the contract with tests or preconditions when it is executable;603. comment only the decision, boundary, trade-off, or failure mode still invisible;614. move broader system context to DocC when it no longer belongs to one symbol.6263## Refactor-First Rule6465Before writing a comment, first consider:66- renaming variables/functions/types67- extracting smaller functions68- simplifying control flow6970If readability can be improved, refactor first.7172## Decision Flow7374Before writing a comment, ask:751. Can naming or structure remove the need for the comment?762. Can types, actor isolation, ownership, availability, diagnostics, tests, or preconditions express the contract?773. Is there hidden framework behavior not obvious from code?784. Does correctness depend on identity, ordering, lifetime, cancellation, isolation, ownership, or backpressure?795. Is there a generated artifact or external source of truth?806. Does the comment include a verifiable condition for remaining true or being removed?817. Could another developer misuse this API or flow without compiler/test feedback?8283If all answers are "no", do not comment.8485## Preferred Comment Sections8687Use only when needed:88- `Important:`89- `Why:`90- `Assumption:`91- `Constraint:`92- `Invariant:`93- `Risk:`94- `Side Effects:`95- `Concurrency:`96- `Cancellation:`97- `Isolation:`98- `Ownership:`99- `Lifetime:`100- `Observation:`101- `Backpressure:`102- `Performance:`103- `Compatibility:`104- `Generated:`105106Prefer the most precise section. Use `Concurrency:` only for broad concurrency context; use `Cancellation:`, `Isolation:`, `Ownership:`, or `Backpressure:` when one of those is the actual risk.107108## When Not To Comment109110Do not comment when:111- the sentence only repeats implementation112- the name can carry the meaning after small refactor113- the statement is generic and risk-free114- the comment adds length but no new decision context115116## Example Pattern Rule117118Each scenario must include:1191. No comment version (clean but incomplete)1202. Bad comment version (common anti-pattern)1213. Good comment version (useful)1224. Best version (intent + constraints + reasoning)123124## Required Scenarios125126The skill must cover:127- token refresh with concurrency issues128- download manager deduplication invariant129- cache eviction assumptions130- payment logic business constraints131- pricing and currency rounding constraints132- SwiftUI async lifecycle edge cases133- SwiftUI identity, lazy-container state lifetime, and repeatable `onAppear`134- Observation dependencies that are registered implicitly by framework reads135- cancellation boundaries, cleanup, rollback, and point-of-no-return behavior136- actor isolation, task ownership, and non-copyable ownership/lifetime constraints137- stream ordering, cancellation, and backpressure138- generated-code source-of-truth and regeneration boundaries139- local diagnostic exceptions with a removal condition140- performance constraints backed by reproducible evidence141- compatibility workarounds with explicit removal conditions142- background task scheduling limitations143- analytics side effects144- public API contracts in frameworks145146## Anti-Patterns147148Avoid:149- "This function does..."150- "This method is responsible for..."151- "This class..."152- line-by-line narration153- tutorial-style toy examples154- long comments with no constraints or risk155- TODOs without owner, condition, or exit criteria156- "temporary" or workaround comments without version, issue, or removal event157- performance claims without metric, fixture, trace, or benchmark158- comments that contradict types, compiler diagnostics, or tests159160## Writing Guardrails161162When generating comments:163- NEVER start with:164 - "This function..."165 - "This method..."166 - "This class..."167- Prefer direct statements, constraints, risks, and reasoning.168- Keep comments concise. Remove filler words.169170## Voice Calibration171172Write like an experienced iOS engineer speaking to another engineer:173- human and direct174- no buzzwords175- no marketing tone176- no over-polished generic phrasing177178## DocC Boundary Rule179180Use inline `///` comments for:181- contracts182- invariants183- assumptions184- side effects185- local compatibility or performance constraints186187Use DocC articles for:188- architecture explanations189- system flows190- domain concepts191- cross-module behavior192- version-dependent behavior spanning multiple symbols193194Generated code boundary:195- do not add manual documentation to generated artifacts;196- document the schema, specification, or stable wrapper that owns the contract;197- include the regeneration command or process when it is not discoverable.198199## Compatibility Note200201Examples target modern Swift codebases using Swift 6.3+/Xcode 27 concepts, Swift Concurrency, SwiftUI lifecycle behavior, Observation, and generated-code workflows. The principle is retrocompatible with older Swift projects: encode or test what you can, then comment only what remains invisible.202203## Examples204205- [examples/bad-comments.swift](examples/bad-comments.swift)206- [examples/better-comments.swift](examples/better-comments.swift)207- [examples/concurrency.swift](examples/concurrency.swift)208- [examples/invariants.swift](examples/invariants.swift)209- [examples/api-contracts.swift](examples/api-contracts.swift)210- [examples/pricing-rounding.swift](examples/pricing-rounding.swift)211- [examples/modern-contracts.swift](examples/modern-contracts.swift)212213## Additional Resources214215- [docs/principles.md](docs/principles.md)216- [docs/context.md](docs/context.md)217- [docs/decision-rules.md](docs/decision-rules.md)218- [docs/checklist.md](docs/checklist.md)219- [docs/example-constraints.md](docs/example-constraints.md)220- [docs/golden-examples.md](docs/golden-examples.md)221- [docs/docc-guidance.md](docs/docc-guidance.md)222- [docs/review-red-flags.md](docs/review-red-flags.md)