Skip to content
AgileLogics
4 min read

Comments and Code Style

Prerequisites: Functions

A comment is a note for humans; the compiler ignores it.

// A single-line comment

/* A comment
   spanning several lines */

let taxRate = 0.2 // a comment after code

Documentation comments

Comments starting with /// document a declaration. Xcode and VS Code show them when you hover over or autocomplete the name:

/// Returns the price including tax.
/// - Parameter price: The price before tax.
/// - Returns: The price with 20% tax added.
func withTax(_ price: Double) -> Double {
    price * 1.2
}

What to comment

Clear names explain what code does. Comments are for why — reasoning the reader can't see:

// Bad: repeats the code
count += 1 // add one to count

// Good: explains a non-obvious reason
// The API numbers pages from 1, so shift our 0-based index.
let page = index + 1

Swift style in brief

  • lowerCamelCase for variables, constants, and functions; UpperCamelCase for types.
  • Four-space indentation; the opening brace on the same line.
  • Prefer let over var.
  • Name things for clarity at the call site: remove(at: index), not removeAt(index).
  • Booleans read as assertions: isEmpty, hasAccess.

Apple's official Swift API Design Guidelines (on swift.org) explain the naming conventions in depth. In Level 2 you'll set up swift-format or SwiftLint to check style automatically.

What's next

You now know enough Swift to build real programs. Try the Beginner Projects, then continue to Level 2 — Intermediate, which starts with structs.