Choosing Trailing Closures
The Rule of Kevin (courtesy of Kevin Ballard, one of my technical reviewers and an amazing iOS developer at Postmates) adds parentheses around trailing closures when the argument is functional (that is, it returns a value, avoiding state changes and mutating data), restricting bare braces to procedural calls (updates state and/or has side effects). This style creates consistent readability. You always know when a value is expected to return because the parentheses tell you so.
Here are examples of functional elements, each using a brace-parenthesis combination:
| // Select words that are at least 5 characters long |
| let words = sentence.characters.split(separator: " ") |
| .filter({ $0.count > 4 }) |
| .map({ String ... |
Get Swift Style now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.