Chapter 8: Swift Comments

1. Why do we write comments at all?

Comments are notes for humans — the compiler completely ignores them.

Main reasons people write comments:

  • Explain why something is done (not just what)
  • Document public API so others (or future you) understand how to use it
  • Temporarily disable code during debugging
  • Add legal notices / license / copyright
  • Mark sections in long files
  • Leave TODOs, FIXMEs, warnings

2. The two main types of comments in Swift

Type A: Single-line comment — //

Most common style — used everywhere.

Swift

Very common patterns:

Swift

Type B: Multi-line comment — /* … */

Used less often, but still important in specific cases.

Swift

Realistic examples where people actually use /* */:

Swift
Swift

3. Special comment styles that modern Swift developers love

3.1 // MARK: – Section headers (very very common in Xcode)

Xcode shows these in the jump bar (the dropdown above the editor).

Swift

You can also add a separator line:

Swift

3.2 Documentation comments — /// (super important!)

These generate beautiful documentation when you Option-click a symbol in Xcode.

Swift

When someone Option-clicks this function in Xcode, they see:

  • Summary
  • Parameters table
  • Returns
  • Throws

Very professional — almost every public function in good libraries uses ///.

3.3 /** … */ — old-style doc comments (still works, less common now)

Swift

Most people now prefer /// because it’s cleaner.

4. Good habits & bad habits (real developer advice)

Good habits:

  • Explain WHY, not just WHAT Bad: // add 1 to counter Good: // increment retry counter after failed network attempt
  • Keep comments up to date — delete or fix outdated ones
  • Use // MARK: to organize long files
  • Use /// for public functions, types, properties
  • Use // TODO: / // FIXME: / // NOTE: consistently

Bad habits (please avoid):

Swift

5. Quick reference table – when to use each style

Style Syntax Best used for Frequency in modern code
// Single line inline notes, temporary disable, TODO/FIXME ★★★★★ (most common)
// MARK: Section header organizing code in files (shows in Xcode jump bar) ★★★★★
/// Documentation public API, functions, structs, properties ★★★★ (very important)
/* … */ Multi-line block long explanations, ASCII art notices, JSON examples ★★ (occasional)
/** … */ Old doc block legacy code or some open-source projects ★ (rare now)

6. Small realistic examples you can copy-paste

Example 1 – Clean function documentation

Swift

Example 2 – Organized struct

Swift

Would you like to continue with:

  • Writing good documentation comments in more detail
  • Using TODO / FIXME / MARK effectively
  • Comment styles in SwiftUI vs UIKit vs server-side Swift
  • How to generate documentation from comments (DocC)
  • Or move to another topic (variables, functions, printing, etc.)

Just tell me what you’d like next — we’ll keep the same detailed, teacher-like style 😊

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *