The curious case of Optionals — #1 Emptiness

Swift introduced optionals so that a variable could also have a state of emptiness?

Introduction

Optionals & emptiness have a lot in common but before we discuss their relationship, let’s try to understand a few basic things:

  • What is an optional
  • What defines emptiness
  1. Optional — It allows a variable to have 2 states, it could either have a value or nil. nil means it doesn’t contain any value which is one of the forms of emptiness. Lets see an example:
// these could be api values from your server
let num1: Int? = Int("123") // 123
let num2: Int? = Int("abc") // nil

Here both num1 & num2 are of type Int?(optional Int) but...

  • num1 has value 123; you can use it to show on the screen via label or something
  • num2 is nil; you can use it to show some placeholder or some form of empty state UI

So optionals allow you to have nil values which could be used to represent emptiness!

2. Emptiness — It means a state of nothingness, i.e. its value doesn’t exist or doesn’t make any sense. We can represent it in 2 ways:

  • nil — as we have just read above, you can create an optional variable which could hold a nil value to represent the state of emptiness. This approach is project independent & will hold true for most cases.
  • empty-value — you can assign a value for a type which could represent the state of emptiness. This approach is very project specific, totally dependent on the business logic & can’t be generalised! Lets take few types and their proposed empty values..
let name: String = ""
let likes: Int = 0
let height: Double = 0.0
let starred: Bool = false
let feedList: [Feed] = []

Now you can say that these are nothing but default values for primitive types & yeah you are right! These are default values which you can use to define emptiness. String & Arrays already have a Bool property isEmpty which could be used to define emptiness. If, in your app, an empty string or an empty array is equivalent of empty states then making it optional will only add more complexity and nothing else.

Lets conclude the relationship between optional & emptiness!

#1.
let name1: String? = ""
if let name = name1, !name.isEmpty { ... }
#2.
let name2: String = ""
if !name2.isEmpty { ... }
#3. 
let list: [String]? = ["a", "b", "c"]
if let list = list, !list.isEmpty { ... }
#4. 
let list: [String] = ["a", "b", "c"]
if !list.isEmpty { ... }
  • As you can clearly see via the example above, using an optional String/Array only adds extra checks in your code which looks redundant as well. Its not a very big deal but it still adds some extra code (less code means less bugs)!
  • If your business logic is such that having a nil value is handled in a different way than having empty Strings/Arrays or other default values (which is normal as well, business logic is weird) then you have to add those extra checks to make sure its handled properly 👍
As a simple rule, you can always start with a non-optional variable, to avoid any side-effects. As you progress, depending on your business logic, you can make it optional if it’s required & justifiable 👍
Coming soon — The curious case of Optionals — #2 Overuse