DRY in Swift: Write Clean

Jerry PM
3 min readApr 10, 2025

In the world of iOS development, writing clean and efficient code is key.

One of the most powerful yet simple principles you can adopt is DRYDon’t Repeat Yourself.

This philosophy not only improves your code quality but also makes your apps easier to scale and maintain.

A while ago, I saw someone’s post on LinkedIn about DRY in Swift code, and today I’d like to talk about it as well.

Before diving into this topic, feel free to check out some of my other articles as well

  1. Swift Tips #1
  2. Swift Tips #2
  3. Swift Tips #3
  4. XCode Tips #1
  5. XCode Tips #2
  6. XCode Tips #3
  7. Xcode Tips #4

What Is DRY?

DRY stands for Don’t Repeat Yourself. It’s a core programming principle that encourages developers to avoid code duplication.

The idea is simple: every piece of knowledge or logic should exist in a single, unambiguous place in your codebase.

Instead of copying and pasting the same logic multiple times, DRY encourages creating reusable components — such as functions, extensions, or protocols — that can be called wherever needed.

For example, if you’re repeatedly formatting a date string in various parts of your app, consider centralizing that logic in a single utility function or extension.

Why DRY Matters

Implementing DRY can have a transformative effect on your code and your workflow. Here are some key benefits:

  • Improved Readability: Centralizing logic means your code becomes easier to read and understand at a glance.
  • 🐞 Reduced Errors: Repetition increases the risk of inconsistencies and bugs. DRY code minimizes that.
  • 🔧 Simplified Maintenance: Need to make a change? Update one place, not ten.

By reducing redundancy, you’re also making your team’s life easier — or your future self’s, when you return to the project months later.

DRY in Action: Swift Practices

Here are some concrete ways you can apply DRY principles in your Swift projects:

1. Use Functions

Encapsulate reusable logic in functions. Instead of duplicating the same code block, abstract it into a method:

func formatCurrency(_ amount: Double) -> String {
let formatter = NumberFormatter()
formatter.numberStyle = .currency
return formatter.string(from: NSNumber(value: amount)) ?? "$0.00"
}

2. Leverage Extensions

Extensions allow you to add functionality to existing types — perfect for reusable helpers:

extension String {
var isValidEmail: Bool {
// Simple regex validation
return self.contains("@") && self.contains(".")
}
}

3. Utilize Protocols

Use protocols to define shared behavior across multiple types:

protocol Reusable {
static var reuseIdentifier: String { get }
}

extension Reusable {
static var reuseIdentifier: String {
return String(describing: Self.self)
}
}

Final Thoughts: Embrace DRY for Clean Code

The DRY principle is more than just a coding rule — it’s a mindset. Adopting DRY in your Swift development will lead to better code quality, fewer bugs, and faster iteration.

Whether you’re working solo or on a team, applying DRY can significantly streamline your codebase and improve your overall development experience.

Start small. Refactor often. And keep your code DRY.

Here’s another article that discusses the DRY principle — feel free to check it out here.

Thank you for reading! If you enjoyed it, please consider clapping 👏 and following for more updates! 🚀

--

--

No responses yet