Proposal: Replace logical AND (&&) and OR (||) operators with natural language "and" and "or"


(Yannik Bloscheck) #1

Hello!

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:

Current:

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

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

Proposal:

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.


(Chéyo Jiménez) #2

https://lists.swift.org/pipermail/swift-evolution/2015-December/000032.html
https://lists.swift.org/pipermail/swift-evolution/2015-December/000101.html

···

On Wednesday, December 16, 2015, Yannik Bloscheck via swift-evolution < swift-evolution@swift.org> wrote:

Hello!

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:

Current:

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

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

Proposal:

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.
_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org <javascript:;>
https://lists.swift.org/mailman/listinfo/swift-evolution


(Matt Armitage) #3

I actually tend to find the &&, and the || easier to read while skimming
through that code. I think it's also more cohesive with the rest of the
operators to be symbols.

···

On Wed, Dec 16, 2015 at 11:50 PM J. Cheyo Jimenez via swift-evolution < swift-evolution@swift.org> wrote:

https://lists.swift.org/pipermail/swift-evolution/2015-December/000032.html
https://lists.swift.org/pipermail/swift-evolution/2015-December/000101.html

On Wednesday, December 16, 2015, Yannik Bloscheck via swift-evolution < > swift-evolution@swift.org> wrote:

Hello!

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:

Current:

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

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

Proposal:

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.
_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution


(Yannik Bloscheck) #4

You are right, the current way is more cohesive with the rest of the operators but that’s also kind of the problem. The && and || disappear in the sea of two digit operators in the statements (like ==, !=, <=, >=).
The one digit versions & and | wouldn’t have this problem, but sadly they are already used as bitwise operators and a change to them would be very confusing.
Still I think reading the answer from Chris Lattner at https://lists.swift.org/pipermail/swift-evolution/2015-December/000032.html (linked by J. Cheyo Jimenez) I think the && and || seem to be here to stay.

···

On 2015, December 17, at 13:12, Matt Armitage via swift-evolution <swift-evolution@swift.org> wrote:

I actually tend to find the &&, and the || easier to read while skimming through that code. I think it's also more cohesive with the rest of the operators to be symbols.
On Wed, Dec 16, 2015 at 11:50 PM J. Cheyo Jimenez via swift-evolution <swift-evolution@swift.org <mailto:swift-evolution@swift.org>> wrote:
https://lists.swift.org/pipermail/swift-evolution/2015-December/000032.html
https://lists.swift.org/pipermail/swift-evolution/2015-December/000101.html

On Wednesday, December 16, 2015, Yannik Bloscheck via swift-evolution <swift-evolution@swift.org <mailto:swift-evolution@swift.org>> wrote:
Hello!

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:

Current:

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

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

Proposal:

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.
_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org <>
https://lists.swift.org/mailman/listinfo/swift-evolution
_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org <mailto:swift-evolution@swift.org>
https://lists.swift.org/mailman/listinfo/swift-evolution