Skip to main content

Command Palette

Search for a command to run...

Clean Coding in Swift

Updated
2 min read
Clean Coding in Swift
C

Passionate about: Defensive Programming • Effective Engineering Documentation • Emotion-Centered Design and Microcopy

One of the most satisfying things as a developer is seeing a verbose piece of code get converted to a cleaner, more elegant variation of what it was before. I love when teammates make these types of suggestions on code reviews, and wanted to share a few examples of this that I’ve encountered over the past few months.

Storing a series of checks in an array

Sometimes, if you have a long chain of OR checks where the values being checked are of the same type, it may be more concise to store the values in an array, and execute all the checks in one go at the end. Ultimately, I think this one comes down to personal preference.

// ⭐⭐⭐ BEFORE ⭐⭐⭐ //
if state == .validPassword ||
    state == .passwordTooLong ||
    state == .passwordTooShort ||
    state == .passwordInvalidSpecialChar ||
    state == .passwordInvalidChar ||
    state == .passwordDefaultInvalid ||
    state == .passwordNeedsLowercase ||
    state == .passwordNeedsUppercase {
        passwordValidation.text = viewModel.passwordValidationMessage
}

// ⭐⭐⭐ AFTER ⭐⭐⭐ //
let passwordStates: [NewCreateAdminPasswordVMState] = [                                                           
    .validPassword,
    .passwordTooLong,
    .passwordTooShort,
    .passwordInvalidSpecialChar,
    .passwordInvalidChar,
    .passwordDefaultInvalid,
    .passwordNeedsLowercase,
    .passwordNeedsUppercase
                                                       ]
if passwordStates.contains(state) {
    passwordValidation.text = viewModel.passwordValidationMessage
}

Converting a series of if-let statements to a compactMap operation

When you have a series of if-let statements that all contain similar logic, see if they can be condensed so that the logic is applied just once.

// ⭐⭐⭐ BEFORE ⭐⭐⭐ //
if let username = account.username {
  accountDetails.append(username)
}

if let subscriptionType = account.subscriptionType {
  accountDetails.append(subscriptionType)
}

if let lastPaymentDate = account.lastPaymentDate {
  accountDetails.append(lastPaymentDate)
}

return accountDetails

// ⭐⭐⭐ AFTER ⭐⭐⭐ //
return [
  account.username,
  account.subscriptionType,
  account.lastPaymentDate
]
  .compactMap { $0 }

Converting if-let blocks to nil coalescing one-liners

Similar to the example above, if the logic in your if-let statements is simply assigning the unwrapped value to a property, consider using the more concise nil coalescing syntax.

// ⭐⭐⭐ BEFORE ⭐⭐⭐ //
if let username = response["Username"] {
  self.username = username
}

if let subscriptionType = response["SubscriptionType"] {
  self.subscriptionType = subscriptionType
}

if let lastPaymentDate = response["LastPaymentDate"] {
  self.lastPaymentDate = lastPaymentDate
}

// ⭐⭐⭐ AFTER ⭐⭐⭐ //
self.username = response["Username"] ?? username // Note that this only works if the username property already has a default value
self.subscriptionType = response["SubscriptionType"] ?? subscriptionType
self.lastPaymentDate = response["LastPaymentDate"] ?? lastPaymentDate