Pro Tip: Boolean chaining

You might occasionally reach a state where a status is defined by a sequence of boolean values. This is common occurrence when dealing with state machines, here’s an abstracted example:


var isConnected : Bool = true
var hasData : Bool = true
var isDataStale: Bool = false

var isDataReadyForPresentation : Bool {
  return isConnected && hasData && !isDataStale
}

Depending on the complexity of your state machine, the definition of the variable ‘isDataReadyForPresentation‘ would be sufficient, but if calculating this value depends on a sequence of  a large number of values, you quickly will loose readability on a long line of boolean conditions.

Continue reading “Pro Tip: Boolean chaining”