Location of ! in Boolean negation expressions


#1

Consider code like

guard !parameters.contains(where: { !validValueRange.contains($0) }) else …

Oftentimes I need to write negation expressions like this. The location of the exclamation marks really bugs me when writing and reading this code. The natural English ordering would be something like

“Make sure parameters does not contain an element such that validValueRange does not contain this element.”

But the programming-language-imposed ordering is

“Make sure NOT parameters contains an element such that NOT validValueRange contains this element.”

See how much harder the programming language version is to understand? Most of the time I write the positive version first because it comes out so naturally, and then I add the exclamation marks afterwards. It really burdens my mind every time I need to write code like this. Let’s come up with a solution to address this!

Here’s my zero-thought solution:

guard parameters.!contains(where: { validValueRange.!contains($0) }) else …

I’d love to hear alternate solutions and whether other people are having this problem too!

Darren


(Xiaodi Wu) #2

Changes to Swift syntax aren't really in scope for the Swift 4 Stage 1
evolution process. See this list:
https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20160725/025676.html

···

On Sat, Aug 6, 2016 at 2:37 AM, Darren Mo via swift-evolution < swift-evolution@swift.org> wrote:

Consider code like

guard !parameters.contains(where: { !validValueRange.contains($0) }) else …

Oftentimes I need to write negation expressions like this. The location of
the exclamation marks really bugs me when writing and reading this code.
The natural English ordering would be something like

“Make sure parameters does not contain an element such that
validValueRange does not contain this element.”

But the programming-language-imposed ordering is

“Make sure NOT parameters contains an element such that NOT
validValueRange contains this element.”

See how much harder the programming language version is to understand?
Most of the time I write the positive version first because it comes out so
naturally, and then I add the exclamation marks afterwards. It really
burdens my mind every time I need to write code like this. Let’s come up
with a solution to address this!

Here’s my zero-thought solution:

guard parameters.!contains(where: { validValueRange.!contains($0) }) else …

I’d love to hear alternate solutions and whether other people are having
this problem too!

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


(Anton Zhilin) #3

One solution to this problem would be to add negative method versions
wherever possible. For example: 'all', 'any', 'some', 'none' methods
instead of just 'contains(where:)'.
Plus, we could add 'unless' alongside 'guard'. But these features were
postponed to Stage 2.

···

2016-08-06 10:37 GMT+03:00 Darren Mo via swift-evolution < swift-evolution@swift.org>:

Consider code like

guard !parameters.contains(where: { !validValueRange.contains($0) }) else …

Oftentimes I need to write negation expressions like this. The location of
the exclamation marks really bugs me when writing and reading this code.
The natural English ordering would be something like

“Make sure parameters does not contain an element such that
validValueRange does not contain this element.”

But the programming-language-imposed ordering is

“Make sure NOT parameters contains an element such that NOT
validValueRange contains this element.”


(Johannes Neubauer) #4

I think your syntax is too near to forced unwrapping. It's just do I put the ! Before or after the dot. This becomes even harder to read if you want to use both at the same time. Then you end up with !.!

Further on, if you want to negate a complete expression, the ! Is again before the parentheses. This will "look" inconsequent.

Last but not least it is uncommon.

All the best
Johannes

···

Von meinem iPhone gesendet

Am 06.08.2016 um 09:37 schrieb Darren Mo via swift-evolution <swift-evolution@swift.org>:

Consider code like

guard !parameters.contains(where: { !validValueRange.contains($0) }) else …

Oftentimes I need to write negation expressions like this. The location of the exclamation marks really bugs me when writing and reading this code. The natural English ordering would be something like

“Make sure parameters does not contain an element such that validValueRange does not contain this element.”

But the programming-language-imposed ordering is

“Make sure NOT parameters contains an element such that NOT validValueRange contains this element.”

See how much harder the programming language version is to understand? Most of the time I write the positive version first because it comes out so naturally, and then I add the exclamation marks afterwards. It really burdens my mind every time I need to write code like this. Let’s come up with a solution to address this!

Here’s my zero-thought solution:

guard parameters.!contains(where: { validValueRange.!contains($0) }) else …

I’d love to hear alternate solutions and whether other people are having this problem too!

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


(James Andrews) #5

Hi,

The .! syntax is a bit gross I think. The problem is that programmers
are very familiar with the NOT <something> semantics. So while changing
it might be pleasing from a natural language perspective, it will seem
strange from the programmer perspective. I guess you cant win!

I like the suggestion of adding a negative form of the method. However
syntax changes are not going to happen for a while now I believe.

Thanks,
James

···

On Sat, Aug 6, 2016, at 07:32 PM, Anton Zhilin via swift-evolution wrote:

2016-08-06 10:37 GMT+03:00 Darren Mo via swift-evolution <swift-
evolution@swift.org>:

Consider code like

guard !parameters.contains(where: { !validValueRange.contains($0) })
else …

Oftentimes I need to write negation expressions like this. The
location of the exclamation marks really bugs me when writing and
reading this code. The natural English ordering would be
something like

“Make sure parameters does not contain an element such that
validValueRange does not contain this element.”

But the programming-language-imposed ordering is

“Make sure NOT parameters contains an element such that NOT
validValueRange contains this element.”

One solution to this problem would be to add negative method versions
wherever possible. For example: 'all', 'any', 'some', 'none' methods
instead of just 'contains(where:)'.
Plus, we could add 'unless' alongside 'guard'. But these features were
postponed to Stage 2.
_________________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution