# isNonEmpty

**URL:** <https://forums.swift.org/t/isnonempty/16684>\
**Category:** Pitches\
**Created:** [October 4, 2018, 10:48am UTC](https://forums.swift.org/t/isnonempty/16684 "2018-10-04T10:48:19Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![JJJ](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jjj/32/12193_2.png) [@JJJ](https://forums.swift.org/u/JJJ)\
**Post date:** [October 4, 2018, 10:48am UTC](https://forums.swift.org/t/isnonempty/16684/1 "2018-10-04T10:48:19Z")

</div>

I know this is frowned upon, as we cannot create inverses for all boolean properties, but the inverse to isEmpty is used a lot and really improves readability, IMHO.

We are building and maintaining a large Swift application and have defined isNonEmpty for all types and protocols that have isEmpty, and would suggest others do the same - or preferably that it gets into the standard library 👍

```
if !isEmpty {
  // use it
}

```

Becomes:

```
if isNonEmpty {
  // use it
}

```

Actually, IMHO, the readability of ! is exacerbated here by the initial letter of isEmpty being the visual inverse: i / ! :-)

---

<div class="post-metadata">

**Author:** ![woolsweater](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/woolsweater/32/2402_2.png) [@woolsweater](https://forums.swift.org/u/woolsweater)\
**Post date:** [October 4, 2018, 2:10pm UTC](https://forums.swift.org/t/isnonempty/16684/2 "2018-10-04T14:10:53Z")

</div>

This was also discussed a few months ago: [`isNotEmpty` on Array - #9 by gwendal.roue](https://forums.swift.org/t/isnotempty-on-array/15228/9) (and I don't believe that's the first time).

---

<div class="post-metadata">

**Author:** ![JJJ](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jjj/32/12193_2.png) [@JJJ](https://forums.swift.org/u/JJJ)\
**Post date:** [October 4, 2018, 3:38pm UTC](https://forums.swift.org/t/isnonempty/16684/3 "2018-10-04T15:38:23Z")

</div>

Thanks!

Yeah, it's obviously not a goal of the standard library to add all sorts of small convenience wrappers like this, but I think this is special in that it will really help readability. I think readability is a great argument.

If's not uncommon that you have a long expression ending in .isEmpty. You will then always have to go back to the start of the expression to check for a possible negation symbol (that does not stand-out visually). With isNonEmpty this reads fluently.

It is indeed very easy to just add isNonEmpty to each project, without any reasonable risk of getting it wrong. But if isNonEmpty was part of the standard library, it would potentially improve the readability of all Swift source code, in learning material and code examples.

I think isNonEmpty is a better name than isNotEmpty, as this is not just a negation of isEmpty; we want to check for the property "non-empty". I also think it makes the code read more like proper English.

---

<div class="post-metadata">

**Author:** ![Max\_Howell1](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/max_howell1/32/10049_2.png) [@Max\_Howell1](https://forums.swift.org/u/Max_Howell1)\
**Post date:** [October 4, 2018, 6:37pm UTC](https://forums.swift.org/t/isnonempty/16684/4 "2018-10-04T18:37:36Z")

</div>

`hasElements` might be less brain-taxing.

---

<div class="post-metadata">

**Author:** ![JJJ](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jjj/32/12193_2.png) [@JJJ](https://forums.swift.org/u/JJJ)\
**Post date:** [October 4, 2018, 6:41pm UTC](https://forums.swift.org/t/isnonempty/16684/5 "2018-10-04T18:41:48Z")

</div>

But does a non-empty range have elements? Perhaps it does. But a non-empty geometry does not, I think.

---

<div class="post-metadata">

**Author:** ![Jon\_Hull](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jon_hull/32/678_2.png) [@Jon\_Hull](https://forums.swift.org/u/Jon_Hull)\
**Post date:** [October 5, 2018, 10:29am UTC](https://forums.swift.org/t/isnonempty/16684/7 "2018-10-05T10:29:46Z")

</div>

I know this is a frequently suggested (and seemingly rejected) thing, but I do want to point out that at least 95% of my code which contains `.isEmpty` also contains a `!`.

We might want to consider an exception to the "we can't provide negated versions of anything/everything that returns Bool" heuristic in this case because it is so frequent...

---

<div class="post-metadata">

**Author:** ![nick.keets](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/nick.keets/32/2023_2.png) [@nick.keets](https://forums.swift.org/u/nick.keets)\
**Post date:** [October 5, 2018, 11:20am UTC](https://forums.swift.org/t/isnonempty/16684/8 "2018-10-05T11:20:24Z")

</div>

Your last sentance should be: "We might want to consider an exception in this case because it is so frequent **in my code**". Otherwise you are making a logical leap.

---

<div class="post-metadata">

**Author:** ![JJJ](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jjj/32/12193_2.png) [@JJJ](https://forums.swift.org/u/JJJ)\
**Post date:** [October 5, 2018, 11:25am UTC](https://forums.swift.org/t/isnonempty/16684/9 "2018-10-05T11:25:05Z")

</div>

I did a quick search in part of our code and got 700 occurrences or `isNonEmpty` and 500 occurrences of `isEmpty`.

I wonder what stats others have, but I would assume that with `guard` etc. it could easily be the case in most projects that the check for non-emptiness is more common than the check for emptiness.

Considering the alternatives, I'm in favor of adding `isNonEmpty` 🙂

1. Keep it as is and make

```swift
if !interestingButRatherLongExpressionHere.isEmpty {

```

a fine example of proper usage of Swift...

1. Replace the `isEmpty` property with an `isNonEmpty` one, as that appears to be used more often 😉

2. Make it a method instead so the `!` and `isEmpty` stay close 😛

```swift
if !isEmpty(interestingButRatherLongExpressionHere) {

```

1. Add `isNonEmpty`

Allow

```swift
if interestingButRatherLongExpressionHere.isNonEmpty {

```

but face a torrent of incoming please-add-my-inversed-boolean-property-too requests...

---

<div class="post-metadata">

**Author:** ![JJJ](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jjj/32/12193_2.png) [@JJJ](https://forums.swift.org/u/JJJ)\
**Post date:** [October 5, 2018, 11:45am UTC](https://forums.swift.org/t/isnonempty/16684/10 "2018-10-05T11:45:13Z")

</div>

Searching a larger part of our codebase showed 1350 occurrences of `isNonEmpty` and 800 occurrences of `isEmpty`.

That's almost 1.7 times more.

---

<div class="post-metadata">

**Author:** ![sveinhal](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/sveinhal/32/3992_2.png) [@sveinhal](https://forums.swift.org/u/sveinhal)\
**Post date:** [October 5, 2018, 12:17pm UTC](https://forums.swift.org/t/isnonempty/16684/11 "2018-10-05T12:17:37Z")

</div>

> [@JJJ](#):
>
> If's not uncommon that you have a long expression ending in .isEmpty. You will then always have to go back to the start of the expression to check for a possible negation symbol (that does not stand-out visually). With isNonEmpty this reads fluently.

I'm not sure I'm advocating it, because I haven't given it much thought yet. But, if you want to solve the problem of the negation being visually far separated from the relevant expression, you could add some kind of extension on `Bool`:

```swift
extension Bool {
  var not: Bool { return !self } 
}

if viewModel.nested.collection.isEmpty.not { /* ... */ }
if model.names.contains(element).not { /* ... */ }
// ... etc

```

---

<div class="post-metadata">

**Author:** ![JJJ](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jjj/32/12193_2.png) [@JJJ](https://forums.swift.org/u/JJJ)\
**Post date:** [October 5, 2018, 12:19pm UTC](https://forums.swift.org/t/isnonempty/16684/12 "2018-10-05T12:19:38Z")

</div>

That does improves locality. But it doesn't read like English to me, sorry 🙂

---

<div class="post-metadata">

**Author:** ![sveinhal](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/sveinhal/32/3992_2.png) [@sveinhal](https://forums.swift.org/u/sveinhal)\
**Post date:** [October 5, 2018, 1:04pm UTC](https://forums.swift.org/t/isnonempty/16684/13 "2018-10-05T13:04:08Z")

</div>

> [@JJJ](#):
>
> That does improves locality. But it doesn't read like English to me, sorry 🙂

Sure, I agree. Maybe you can think of some better name?  
Because if you do, you have solved it for every bool property and function returning a bool.

---

<div class="post-metadata">

**Author:** ![JJJ](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jjj/32/12193_2.png) [@JJJ](https://forums.swift.org/u/JJJ)\
**Post date:** [October 5, 2018, 1:12pm UTC](https://forums.swift.org/t/isnonempty/16684/14 "2018-10-05T13:12:28Z")

</div>

That's kind of true. But even if I change it to ask whether `collection.isEmpty.isFalse`, or whether `collection.isEmpty.negated`, or whatever, it's still sort of a double negative and imposes additional cognitive load if I just want to know whether the collection is non-empty.

---

<div class="post-metadata">

**Author:** ![Max\_Howell1](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/max_howell1/32/10049_2.png) [@Max\_Howell1](https://forums.swift.org/u/Max_Howell1)\
**Post date:** [October 5, 2018, 8:30pm UTC](https://forums.swift.org/t/isnonempty/16684/15 "2018-10-05T20:30:20Z")

</div>

> But does a non-empty range have elements? Perhaps it does.

While you are right that we don’t think of them as elements for `Range`, the documentation does refer to them as such: [Range | Apple Developer Documentation](https://developer.apple.com/documentation/swift/range) (even if this is because it is lifted from the protocol, which I suspect is the case).

And the `associatedtype` of the underlying protocol is `Element`.

> But a non-empty geometry does not, I think.

I'm sure it won't fit as well with some potential `Sequence` conformances, but is there one in particular you mean?

---

<div class="post-metadata">

**Author:** ![Justin\_Jia](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/justin_jia/32/361_2.png) [@Justin\_Jia](https://forums.swift.org/u/Justin_Jia)\
**Post date:** [October 6, 2018, 12:01am UTC](https://forums.swift.org/t/isnonempty/16684/16 "2018-10-06T00:01:51Z")

</div>

Core team:

> [@\[Accepted with Modifications\] SE-0225: Adding isMultiple to BinaryInteger](https://forums.swift.org/t/accepted-with-modifications-se-0225-adding-ismultiple-to-binaryinteger/15689/1):
>
> In contrast, a property like `isNotEmpty` offers no substantial advantages over `!isEmpty` , and adding it would open the door to adding trivial negations of essentially every predicate.

You can also use `isEmpty == false`.

---

<div class="post-metadata">

**Author:** ![salutis](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/salutis/32/1156_2.png) [@salutis](https://forums.swift.org/u/salutis)\
**Post date:** [October 6, 2018, 12:22am UTC](https://forums.swift.org/t/isnonempty/16684/17 "2018-10-06T00:22:21Z")

</div>

Speaking of extending Bool, we could add Bool.toggled as a non-mutating variant the recently added Bool.toggle().

---

<div class="post-metadata">

**Author:** ![JJJ](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jjj/32/12193_2.png) [@JJJ](https://forums.swift.org/u/JJJ)\
**Post date:** [October 6, 2018, 2:04pm UTC](https://forums.swift.org/t/isnonempty/16684/18 "2018-10-06T14:04:34Z")

</div>

> [@Max\_Howell1](#):
>
> > But a non-empty geometry does not, I think.
> 
> I'm sure it won't fit as well with some potential `Sequence` conformances, but is there one in particular you mean?

`Envelope.isEmpty` / `Envelope.isNonEmpty`. Not a Sequence, but we still need to ensure it's non-empty before using it.

`if collection.isEmpty.toggled {` is still much worse than `if collection.isNonEmpty {`. But better than `if !collection.isEmpty {` I guess... 🙂

---

<div class="post-metadata">

**Author:** ![djtech42](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/djtech42/32/23960_2.png) [@djtech42](https://forums.swift.org/u/djtech42)\
**Post date:** [October 7, 2018, 3:10pm UTC](https://forums.swift.org/t/isnonempty/16684/19 "2018-10-07T15:10:31Z")

</div>

+1 for isNotEmpty, it seems to read clear in code

---

<div class="post-metadata">

**Author:** ![taylorswift](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/taylorswift/32/43413_2.png) [@taylorswift](https://forums.swift.org/u/taylorswift)\
**Post date:** [October 7, 2018, 7:16pm UTC](https://forums.swift.org/t/isnonempty/16684/20 "2018-10-07T19:16:03Z")

</div>

if we add `isNonEmpty` to the standard library what will we use as our strawman example of a property that should not be added to the standard library?

---

<div class="post-metadata">

**Author:** ![JJJ](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jjj/32/12193_2.png) [@JJJ](https://forums.swift.org/u/JJJ)\
**Post date:** [October 7, 2018, 8:18pm UTC](https://forums.swift.org/t/isnonempty/16684/21 "2018-10-07T20:18:57Z")

</div>

The thing is that `!isEmpty` is used **a lot** in common code, and apparently in many cases **more** than `isEmpty` is. On top of that it really helps readability. Together it really helps readability for a lot of Swift code...

It would be great with some corpus of Swift code that could be used to make statistics for arguing these sort of things 👍

[Next page](https://forums.swift.org/t/isnonempty/16684.md?page=2)
