avatarPaul O'Neill

Summary

This article discusses the use of predicates with Swift Arrays to make code more expressive and understandable.

Abstract

Swift Arrays are a powerful tool in programming, with many methods available for use. However, most developers only use a handful of these methods, often overlooking the use of predicates. Predicates allow for customization and the use of these methods on custom data types. The article provides examples of using predicates with the min method and demonstrates how using a variety of methods can make code more readable and understandable.

Opinions

  • The author believes that Swift Arrays are underutilized and that many developers only use a few of the available methods.
  • The author suggests that using predicates with custom data types is a powerful and underutilized feature of Swift Arrays.
  • The author emphasizes the importance of making code readable and understandable, and suggests that using a variety of methods from Swift Arrays can help achieve this.
  • The author provides examples of how using predicates and other methods can make code more expressive and readable.
  • The author encourages developers to get to know the many methods available in Swift Arrays and start writing better code.

Swift Arrays are powerful

Arrays have such commonplace in programming. Swift Arrays in particular are easy to use and incredibly powerful. The amount of methods that we get for free from Swift is quite spectacular and often overlooked. Just look at the documentation for Swift Arrays. The list is lengthy. Of all the codebases that I have worked on, I only see a handful of these methods ever used. There are probably 4–5 methods used with the addition of creating extensions to get the functionality needed.

In this article, I will explain the use of predicates and how it helps us use all of these methods with our own custom data types. Also, you’ll see why it’s beneficial to use a variety of these methods in your code and not just the standard ones.

Use Custom Predicates

As you look through the documentation, you’ll see that most of the methods have an additional closure for the API that gives you the option to use a custom “predicate”. This is powerful and underutilized. Often, we’re not working with basic types like Ints or Strings, but rather custom data types that represent the data in our apps. Using a custom predicate allows for customization and using these methods on those custom data types. Let’s look at an example of using the min method:

When using an Array of integers, Swift is smart enough to handle figuring out what the “min” integer is. But what if we use a custom data type that we created? That’s where predicates come in:

We can specify what “min” applies to in this predicate. In this case, it’s the price attribute of our custom data type. Most of the methods of Swift Arrays allow for a predicate for such a case. Pretty neat!

Be More Expressive

Getting your code to work the way you want is only part of the battle. Another important concept is making your code readable. You need to make code readable so that other people reading your code will be able to understand it in a reasonable amount of time. You might also be the person reading it after several weeks of not looking at it. It’s worth the time and effort to add readability to your code. The many methods from Arrays will greatly help your code be more expressive and understandable. Here’s a simple example:

This method does what the comment in the code describes. It’ll pass all the unit tests. How could we make this better? What if we used another method from Swift Arrays to handle this:

By using the `first` method we add another level of readability. Now the reader will read: “The first bus to match this predicate”. Not only is it slightly less code, but it’s much better at describing what’s going on in code. That’s a nice win for everybody!

Let’s look at another common example using the filter method.

Again, this code get’s the job done. The filter method returns all of the food items that are not expired. While this works, let’s look at a more descriptive way:

Here we use the removeAll method. It reads like this: “Remove all food items where the expiration date is passed today”. This code is much more readable and simple.

Summary

Swift Arrays have a plethora of methods that help us do the many operations that we need from our arrays. This is great because now we can be more precise and descriptive about our code. Even with our custom data types, we can use predicates to operate in a custom way. Get to know these methods and start writing better code!

Swift
iOS Development
Arrays
Array Methods
Apple Development
Recommended from ReadMedium