0

I am trying to write less and less code and I am trying to find a way to prevent crashes.

An example what I have encountered is:

public class MyClass
{
   private User user;

   public MyClass()
   {
       // Get user from another class
       // Another thread, user can be null for couple of seconds or minutes
       // Asynchronous call
       user = AnotherClass.getUser();

       // start method
       go();
   }

   private void go()
   {
      // Method 1
      // Program it is crashing if user is null
      if (user.getId() == 155)
      {
         // TO DO
      }
      else
      {
         System.out.println("User is NOT 155 !");
      }

      // Method 2
      // Program still crashes if user is null
      if (user != null && user.getId() == 155)
      {
         // To do
      }
      else
      {
         System.out.println("user is not 155");
      }

      // Method 3
      // Program wont crash, but I write much more code !
      if (user != null)
      {
         if (user.getId() == 155)
         {
            // To do
         }
         else
         {
            System.out.println("User is not 155 !");
         }
      }
      else
      {
          System.out.println("User is not 155 !");
      }
   }
}

As you can see, method 3 it's working, but I am writing much more code... What should I do?

2
  • 1
    method 2 should work too. recheck it. in java if first part is false second part not evaluated. Commented Oct 23, 2013 at 14:04
  • The point is not that it doesn't work, but that it's relatively verbose Commented Oct 23, 2013 at 14:06

3 Answers 3

2

Prefer the way Short-circuit evaluation, That is method 2.

when the first argument of the AND function evaluates to false, the overall value must be false;

      if (user != null && user.getId() == 155)
      {
         // To do
      }
      else
      {
         System.out.println("user is not 155");
      }

That is the most preferable and readable code.

Your assumtions are wrong that method2 crash and method3 works. In the above code if user != null then only user.getId() == 155 executes.

Sign up to request clarification or add additional context in comments.

2 Comments

What if I swap the two conditions? if (user.getId() == 155 && user != null) ?
@ZbarceaChristian You end up with Null pointer exception. If you swap those conditions then its like if (user.getId() == 155) {if(user != null){}, which does'nt make sense.
1

Why not use a null object pattern here, so instead of setting user to be null, set it to a special 'null' case (implementation) of the User object ?

e.g.

user = AnotherClass.getUser();
if (user == null) {
   user = new NullUser();
}

(ideally AnotherClass.getUser() would do the null check internally)

In this case

user.getId()

could return a special value (-1 ?) which would never equate to a valid user id. Hence your code will always look like:

if (user.getId() == 155)

The same would apply to other methods on the User object.

2 Comments

What if I edit the User class and add a field id with a default value of -1 ? private int id = -1; public User(/* params */){}
Perhaps that would be suitable too
1

It's got to be something inside the block started by this statement:

if (user != null && user.getId() == 155)

That is logically identical to method 3. When the JVM sees that user is null, it should stop evaluating that.

I will say though that I encountered something like this with JVM 1.3, so if you are using a really old JVM that may be it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.