Swift Quiz – Boolean Expression
What is the value of result after doing: let result = false && true || true
Here's a little Swift quiz for you.
let result = true || true && false
print(result)What does this code print out?
I believe this exact phrase will be printed to the console:
You are a bad coder and you should feel bad.
Ok, I'm joking, but I hate “quizzes” like these. Who cares what the output is? We should not be writing potentially ambiguous code like this. Sure, some devs might know that an AND operation always takes precedence over an OR, but there is zero reason we should rely on that. Instead, we should put some simple parenthesis to eliminate any doubt.
let result = true || (true && false)Here, there is no question that we will get true. When in doubt, use parenthesis.
Subscribe
New posts on company building, working with AI, and shipping software — straight to your inbox. No spam, unsubscribe anytime.
Related reading
Effect of Swift Extension and Libraries On Build Time
Does separating code into extra extensions and/or modules have a meaningful impact on compile times?
8 min readJSON Encoder Change in Swift 5.1
While updating my app for the iOS 13 SDK, I came across a change in JSONEncoder that broke some of the logic in my code. It now supports encoding top-level values.
1 min readCareful with Fancy KVO Callback
Be warned: there may be a bug in the fancier version of Key Value Observering. I discovered it while implementing a progress callback in my Decree web request library.
2 min read