Week 8: Your Swift Code Works, But It Doesn’t Make Sense.

Dylan Chen
May 17 · 2 min read
Photo by Farzad Nazifi on Unsplash

Hi Junior iOS Developers,

“Your code works, but it doesn’t make sense.” You may have heard this from a senior developer. If you are coming from a low-level programming language or you just started learning Swift, this story should be beneficial for you. In this story, we’ll talk about some readability issues. The code should work perfectly fine but just doesn’t make sense.

There are multiple ways to check if an element exists in an array. Here are my suggestions for some common cases.

To check if names contains “Mike”:

let names = ["Dylan", "Jenny", "Amy", "Mike", "Jenny"]
// Not recommanded
names.filter { $0 == "Mike" }.count > 0
// Recommanded
names.contains { $0 == "Mike" }
// Not recommanded
names.filter { $0 == "Mike" }.isEmpty
// Recommanded
names.contains { $0 == "Mike" }
// Not recommanded
names.first (where: { $0 == "Mike" })
// Recommanded
names.contains { $0 == "Mike" }

In this section, Let’s talk about suggestions for finding the maximum and minimum values.

To find the maximum value from numbers .

let numbers = [1, 2, 3, 4, 5, 6, 7]
// Not recommanded
numbers.sort (where: { $0 > $1 }).first
// Recommanded
names.max
// Not recommanded
numbers.sort (where: { $0 > $1 }).last
// Recommanded
names.min

To transform arrays from an array of objects into one array.

struct Person {
let name: String
let children: [String]
}
let group = [
Person(name: “Bob”, children: [“John”, “Mike”]),
Person(name: “Katty”, children: [“Jenny”]),
Person(name: “Zoe”, children: [“Ben”, “James”, “Steve”])
]
// Not recommanded
group.map { $0.children }.reduce {[], +}
// Recommanded
group.flatMap { $0.children }

This is all I have for Week 8. Hope you found this information useful and time-saving. As usual, I’ll be posting another story about iOS development next week. See you then!

Medium Claps — Made in Flinto by Thuy Gia Nguyen on Dribbble

Please clap if you enjoyed this story. Follow me. I’ll see you in subsequent stories :)

YOU MAY ALSO BE INTERESTED IN:

Geek Culture

Proud to geek out. Follow to join our +500K monthly readers.

Dylan Chen

Written by

iOS Engineer at Lunchbox Technologies. Buy me a coffee at: https://www.buymeacoffee.com/dylancfe15

Geek Culture

A new tech publication by Start it up (https://medium.com/swlh).

Dylan Chen

Written by

iOS Engineer at Lunchbox Technologies. Buy me a coffee at: https://www.buymeacoffee.com/dylancfe15

Geek Culture

A new tech publication by Start it up (https://medium.com/swlh).

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store