Swift: Simplifying Your Code

Jerry PM
3 min readJun 25, 2024

--

Reduce the number of lines in your code

Source: Author

In the world of software development, simplicity is often revered as a virtue. Yet, achieving simplicity in code can be a daunting task, especially when dealing with complex programming languages like Swift. However, fear not, for there are techniques and approaches that can help streamline your Swift code, transforming it from convoluted complexity to elegant simplicity.

Swift, known for its expressiveness and modern syntax, empowers developers to write clean and concise code. However, as projects grow in scale and complexity, maintaining clarity and readability becomes increasingly challenging. This is where the art of simplification comes into play.

Common code usage

  • Using Map to Transform Values in an Array
// Before
let numbers = [1, 2, 3, 4, 5]
var doubledNumbers: [Int] = []
for number in numbers {
doubledNumbers.append(number * 2)
}

// After
let doubledNumbers = numbers.map { $0 * 2 }
  • Using Higher-Order Functions to Filter Data
// Before
let numbers = [1, 2, 3, 4, 5]
var evenNumbers: [Int] = []
for number in numbers {
if number % 2 == 0 {
evenNumbers.append(number)
}
}

// After
let evenNumbers = numbers.filter { $0 % 2 == 0 }
  • Using shorthand to Declare Tuples
// Before
let coordinates = (x: 10, y: 20)
let xCoordinate = coordinates.x
let yCoordinate = coordinates.y

// After
let (xCoordinate, yCoordinate) = (x: 10, y: 20)
  • Using Tuples for Multiple Return Values
// Before
func getCoordinates() -> (Int, Int) {
return (x: 10, y: 20)
}
let coordinates = getCoordinates()
let xCoordinate = coordinates.0
let yCoordinate = coordinates.1

// After
func getCoordinates() -> (x: Int, y: Int) {
return (x: 10, y: 20)
}
let (xCoordinate, yCoordinate) = getCoordinates()

That common use for Simplifying and many more these are just a few examples that you can use.

This is a longer code

This example code when create initial name and many more, you can create more Simplifying an example from my code I ever used.

    private func getInitialFromString(_ string: String) -> String {
let components = string.components(separatedBy: " ")

if components.count == 1 {
guard let firstName = components.first else {
return ""
}
return String(firstName.prefix(1))
}

guard let firstName = components.first, let lastName = components.last else {
return ""
}

let firstInitial = String(firstName.prefix(1))
let lastInitial = String(lastName.prefix(1))

return (firstInitial + lastInitial).uppercased()
}

you can make it simple code like this use function, if you use like MVVM or VIPER you can put this code in ViewModel (MVVM), Presenter (VIPER).

private func getInitials(from fullName: String) -> String {
let components = fullName.split(separator: " ")
let firstInitial = components.first?.prefix(1) ?? ""
let lastInitial = (components.count > 1) ? (components.last?.prefix(1) ?? "") : ""

return (firstInitial + lastInitial).uppercased()
}

You can also create extensions for multiple uses like this. I think I use this often.

extension String {
private func getInitials() -> String {
let components = self.split(separator: " ")
let firstInitial = components.first?.prefix(1) ?? ""
let lastInitial = (components.count > 1) ? (components.last?.prefix(1) ?? "") : ""

return (firstInitial + lastInitial).uppercased()
}
}

// use

let fullName = "John Doe"
let initials = fullName.getInitials()
print(initials) // Output: "JD"

Transform Your Ideas into Reality with Expert iOS & Flutter Development! 🚀 Are you looking to bring your app concept to life? I specialize in crafting exceptional iOS and Flutter applications that stand out in the digital world. Let’s collaborate to turn your vision into a stunning, functional app. Contact me today to start your app development journey!”

Hire me -> https://www.upwork.com/freelancers/~01b22fa418e8c595b9

--

--