[swift-evolution] Proposal: Replace logical AND (&&) and OR (||) operators with natural language "and" and "or"

Yannik Bloscheck hello at yannikbloscheck.com
Wed Dec 16 22:11:40 CST 2015


I always found the logical AND (&&) and OR (||) operators a bit confusing.
The & as symbol for AND is pretty common in the world, but the | for OR isn’t so much.
They also need to be typed twice, because the simple & and | were already reserved for bitwise things in C. This makes it hard for beginners to understand why they have to use two characters every time, because bitwise operators are rarely used in the context of current end-user applications (I personally can’t remember any occasion where I have seen them in Apple's example projects).
To be clear: I don’t think we should get rid or change the bitwise operators (if they even exist in Swift), I just want to illustrate why natural language "and" and "or" could be better for logical AND (&&) and OR (||) operators.
So the classical && and || may not be the best choice to begin with, but if we look at them in the context in which the are used the most - if/else-statements - they seem even more odd.
If/else-statements already use natural language: "if something {a} else {b}". Therefore "if something1 and something2 {a} else {b}" just would be the next logical step.
This improves readability, because it literally can just be read. The brain doesn’t need to transfer && to "and" or || to "or". And because "and" and "or" are very short words there isn’t really much more typing needed than before.

To illustrate my point compare the readability of this two lines yourself (for more realism copy them in a text editor of your choice and color the "if", "and", "&&" in the same color as they would be in an IDE):
if something1 == true && something2 != false && something3 == true && something4 != false && something5 == true && something6 != false {…}
if something1 == true and something2 != false and something3 == true and something4 != false and something5 == true and something6 != false {...}

The && and || have a long history in programming, but maybe it’s time to replace them with something better.
Therefore I would suggest to replace && and || with natural language "and" and "or" in Swift 3 to make the code more readable.
If the proposal gets accepted I would also suggest colouring them in the same color as the "if", "else" etc. in IDEs like Xcode for optimal readability.

The impact on existing code would be the following:


if temperatureInCelsius >= 0 && raining == true {
	print("You will need an umbrella")

if cash >= 0.01 || creditcard == true {
	print("You can buy something here")


if temperatureInCelsius >= 0 and raining == true {
	print("You will need an umbrella")

if cash >= 0.01 or creditcard == true {
	print("You can buy something here")

I’m sorry if this was already suggested somewhere or if there are obvious reasons against it which I didn’t think of.

More information about the swift-evolution mailing list