Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8259827
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T02:57:09+00:00 2026-06-08T02:57:09+00:00

I have read this topic: Avoiding != null statements but I don’t see benefits.

  • 0

I have read this topic: Avoiding != null statements but I don’t see benefits.

  1. If you return empty array, you need to examine where the “real” array is, and where the empty array is.

  2. Instead of using code like getName() != null, you must use getName().length > 0. As an example, I’m working on project, where I need to compress some data with few archivers and decided to choose compressed data with the smallest size. If I return null from the archiver “compress” method, I need to check that the returned value is not null. If I decided to return an empty array, then I also need to check it for empty.

Am I right?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T02:57:10+00:00Added an answer on June 8, 2026 at 2:57 am

    The primary advantage of using empty collections or “blank” actions instead of null is that most of the time, such objects will still work in code without further modification. The null value, at its core, is simply far more prone to errors due to its nature.

    Take the following code, for example:

    String[] names = data.getNames();
    if (names != null) {
        for (String name : names) {
            // Do stuff
        }
    }
    

    The check for null is required or you’ll get an NPE. Using a standard for loop does not resolve the problem. On the other hand, if you know you’ll always get an array of some kind, your code will work fine with no additional checks. The loop won’t run at all if the array is empty. Problem solved.

    The same is true for code that implements some form of action. Another example:

    Action myAction = data.getActionToRun();
    if (myAction != null) {
        myAction.run();
    }
    

    Once again, you need a null check. If the action is guaranteed to exist, then you can always call action.run() with no side effects, yet the blank actions just won’t do anything. It’s that simple.

    In many cases, null checks can simply be discarded if you modify how methods return, leading to much simpler and understandable code. In some cases, returning null is the correct choice (for example, getting an object from a collection of keys and values), since there is no default “actionless” value. But null indicates the lack of a value at all, and it requires additional handling be the receiver. Using blank, actionless, non-null objects allows the error to be handled by the data object. That’s good encapsulation. It’s good programming. It just works.™

    Finally, returning null is certainly not a good way to handle errors. If something goes wrong in your code that should never go wrong unless you as the programmer made a programming mistake, use asserts or exceptions. Those are failures. Don’t use null as a failure case, use it as a simple lack of a value.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have read a couple of posts on this topic, but neither addresses my
i have read some topics on this, but i don't quite think it answers
I have read a few bits and bobs online about this topic but found
I have read many Forums on this topic but I still haven't found an
I have read questions and answers about this topic but I still have some
I have read other questions on this topic but none that actually answers my
I have read several posts on this topic, but have yet to find a
I have read through some articles on this topic but I am still cautious
I have read some posts about this topic and the answers are comet, reverse
I have read a lot of related topics here regarding this problem but I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.