Swift Academy · Lesson

Style guide, API design guidelines

Use clear names , thoughtful argument labels , sensible defaults , and concise doc comments to design friendly Swift APIs.

Lesson 2 of 38 steps

Style guide, API design guidelines is a free Swift Academy lesson on CoddyKit. This is lesson 2 of 3. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Swift Academy learning path, and your progress syncs across the web and the CoddyKit app. The Swift Academy course includes 3 lessons in total.

Principles

Good APIs are readable, predictable, and small.

  • Names that describe intent
  • Useful argument labels
  • Defaults for common cases
  • Concise docs and examples

Naming basics

Actions => verbs, data => nouns. Avoid abbreviations that hide meaning.

// Prefer clear, simple names.
// BAD:
func doCalc(_ a: Int, _ b: Int) -> Int { a + b }

// GOOD:
func sum(_ a: Int, _ b: Int) -> Int { a + b }

// BAD (ambiguous):
struct Cfg { let v: Int }
// GOOD (nouns for data types):
struct Configuration { let retries: Int }

print(sum(2, 3))  // 5

Argument labels

Labels help call sites read naturally: remove(at:), insert(_:at:).

// Choose labels that explain a parameter's role.
// BAD:
func remove(_ index: Int) { print("remove", index) }

// BETTER:
func remove(at index: Int) { print("remove at", index) }

// Mixed labels:
func insert(_ item: String, at index: Int) {
    print("insert", item, "at", index)
}

remove(at: 2)
insert("a", at: 1)

Good defaults

Use default parameters to keep common calls short while still offering flexibility.

// Provide defaults to cover the 80% case.
func greet(_ name: String, times: Int = 1, shout: Bool = false) {
    let msg = shout ? "HELLO, \\(name)!" : "Hello, \\(name)!"
    for _ in 0..<times { print(msg) }
}

greet("Ana")                 // default: once, not shouting
greet("Ben", times: 2)
greet("Cara", shout: true)

Effects & mutability

Make effects visible: use mutating methods for state changes and expose read-only views with private(set) when appropriate.

// Prefer pure functions when possible; name mutating effects explicitly.
struct Counter {
    private(set) var value = 0
    mutating func increment(by amount: Int = 1) { value += amount }
}

var c = Counter()
c.increment()
c.increment(by: 3)
print("value =", c.value) // 4

Documenting APIs

Doc comments tips:

  • Start with a single-sentence summary.
  • State what it does, not how.
  • Show a tiny example call.
  • Note preconditions or performance gotchas only if important.

Label usage rationale

Quick check: When should you add an external label?

Recap

Recap: Prefer clear names, add labels that read well, offer defaults for common calls, make effects explicit, and keep docs short with an example.

Free to start

Learn Swift with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
122
Lessons
409

Frequently Asked Questions

Is the “Style guide, API design guidelines” lesson free?

Yes — any 3 lessons of the Swift Academy learning path, including “Style guide, API design guidelines”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Swift Academy course includes 3 lessons in total.

What will I learn in “Style guide, API design guidelines”?

Use clear names , thoughtful argument labels , sensible defaults , and concise doc comments to design friendly Swift APIs. You practise Swift Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Swift Academy?

No prior experience is required. Swift Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 2 of 3.

How long does the “Style guide, API design guidelines” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Swift Academy lesson?

Yes. Every Swift Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. SwiftFormat / SwiftLint basics
  2. Style guide, API design guidelines
  3. Documenting code (DocC intro)
← Back to Swift Academy