DaveAKing / guava-libraries

Automatically exported from code.google.com/p/guava-libraries
Apache License 2.0
0 stars 0 forks source link

Wiki ApacheCommonCollectionsEquivalents #1609

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Comments were disabled on article, I'd like to offer an alternative for

boolean *isEmpty*(Collection)

Current listing:
collection == null || collection.isEmpty()

Suggestion:
Objects.firstNonNull(collection, Collections.EMPTY_LIST).isEmpty()

Original issue reported on code.google.com by tj.rothw...@gmail.com on 11 Dec 2013 at 7:12

GoogleCodeExporter commented 9 years ago
Why, though?  The first version seems more readable, shorter, more efficient...

Original comment by lowas...@google.com on 11 Dec 2013 at 7:14

GoogleCodeExporter commented 9 years ago
I agree it's shorter and more efficient. However the firstNonNull is a bit 
better readability-wise.

It also aligns with the concept of non-null collections, so if we were to 
example the snippet.

public foo(@Nullable Collection input) {
  Collection collection = Objects.firstNonNull(input, Collections.EMPTY_LIST);
  if (!collection.isEmpty()) {
    // some-logic
  }
}

Original comment by tj.rothw...@gmail.com on 11 Dec 2013 at 7:23

GoogleCodeExporter commented 9 years ago
I strongly disagree that the firstNonNull version is better readability-wise... 
it's much less clear about what it's actually doing. And I don't agree that it 
aligns with avoiding null collections because the whole point of that is to 
actually assign something as a non-null collection so you can avoid checking 
for null in future calls (as in your example above). When you just immediately 
call isEmpty(), that's not happening.

Original comment by cgdecker@google.com on 11 Dec 2013 at 7:27

GoogleCodeExporter commented 9 years ago
This issue has been migrated to GitHub.

It can be found at https://github.com/google/guava/issues/<issue id>

Original comment by cgdecker@google.com on 1 Nov 2014 at 4:10

GoogleCodeExporter commented 9 years ago

Original comment by cgdecker@google.com on 3 Nov 2014 at 9:08