← Back to archive

Development · Swift

Variables, constants, and naming in Swift

Choose var or let, use clear names, and sketch control flow before writing complex logic.

Read the Korean original on Velog ↗English translation

Development archive cover

Programs store values and refer to them by name. Swift distinguishes mutable variables, declared with var, from constants, declared with let.

var and let

var score = 100
score = 200

let maximumScore = 300
// maximumScore = 400 // Compile-time error

Prefer let when a value does not need to change. It communicates intent and prevents accidental reassignment.

Naming in Swift

  • Use lowerCamelCase for variables and functions, such as currentScore and calculateTotal().
  • Use UpperCamelCase for types and protocols, such as UserProfile.
  • Prefer words that explain purpose and context over obscure abbreviations.
  • Describe what a value means rather than merely repeating its type.
let userPhoneNumber = "010-0000-0000"
func calculateOrderTotal() -> Int { 0 }

Sketch the flow first

A flowchart can expose missing cases and make complex logic easier to discuss before implementation.

Clear names and explicit control flow communicate both what the code does and what its author intended.

← Back to archive