Skip to content

Return empty collections in place of nulls #211

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 17, 2017

Conversation

TBuc
Copy link
Contributor

@TBuc TBuc commented Jul 31, 2017

When calling a getXyz() method on a Mail object, where xyz is some object implementing Collection, if xyz is null then an empty collection will be returned (currently returns null).

When calling some getXyz() method, where xyz is a collection, if xyz is null then an empty collection will be returned.
@thinkingserious thinkingserious added the status: code review request requesting a community code review or review from Twilio label Jul 31, 2017
@SendGridDX
Copy link
Collaborator

SendGridDX commented Jul 31, 2017

CLA assistant check
All committers have signed the CLA.

@thinkingserious
Copy link
Contributor

Hi @TBuc!

Thanks for the PR!

Could you please provide some explanation why we need to return empty collections rather than nulls?

With Best Regards,

Elmer

@TBuc
Copy link
Contributor Author

TBuc commented Jul 31, 2017

Hi @thinkingserious,

I kept it short at first, but I can do it for sure!

In general, nowadays avoiding returning nulls is generally a best practice, especially for collections. When fetching a collection, I expect to get a collection. If it has no objects in it, it will just be an empty collection. A potential NullPointerException should be avoided if not specifically needed.

There are many alternatives to achieve this goal, i.e.:

    • Eagerly initializing collections
    • Returning an empty collection when null (return new SomeCollection<T>();)
    • Initializing a collection when null (would need synchronization: I know now the addition methods aren't synchronized, but I would not make it worse)
    • Returning an Optional<T extends Collection<T>> object in place of the current <T extends Collection<T>>

In my PR I've opted for the solution n.2 since it has the lowest impact on the existing code.

Best regards,
Antonio

@vaskoz
Copy link

vaskoz commented Jul 31, 2017

@TBuc You're right about returning empty collections instead of nulls (Effective Java 2nd edition Item 43). Also, quoting directly from Item 43, "To eliminate the overhead from creating an empty collection or array, return the immutable empty collections in java.util.Collections".

Would you mind using the java.util.Collections immutable empty collections instead?

return Collections.emptyList();
and 
return Collections.emptyMap();

instead of:

return new ArrayList<Email>()
and 
return new HashMap<String, String>();

@TBuc
Copy link
Contributor Author

TBuc commented Aug 1, 2017

@vaskoz that's great! I've read Bloch quite a few years ago, and I did really not remember such immutable collections do exist. I will modify my code accordingly to your great suggestion. Thank you for pointing out!

I would only add the type parameterization to the code you propose, in order to return the exact type for the collection objects in order to maintain the current return types. So that, i.e., what you proposed:
public List<Email> getBccs() { [...] return Collections.emptyList(); [...] }
would become:
public List<Email> getBccs() { [...] return Collections.<Email>emptyList(); [...] }
Please let me know whether you agree or have a different opinion on it. I will wait for your feedback before enhancing the PR.

When returning an empty collections is needed, now makes use of the ready, immutable empty collections of java.util.Collections in place of instantiating a new collection every time.
@TBuc
Copy link
Contributor Author

TBuc commented Aug 2, 2017

Since one day has passed, I suppose it's OK and I've updated the PR.
@vaskoz please feel free to comment if you have any further suggestion to share.

@vaskoz
Copy link

vaskoz commented Aug 2, 2017

@TBuc Thanks so much! I have no comments.

@thinkingserious
Copy link
Contributor

Hello @TBuc,

Thanks again for the PR!

We want to show our appreciation by sending you some swag. Could you please fill out this form so we can send it to you? Thanks!

Team SendGrid DX

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: code review request requesting a community code review or review from Twilio
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants