jeudi 11 avril 2019

Is there a better way to do a empty check in Java

This might look like a primitive question or a this could be done by a simple util library method that I don't know about.

The goal if to check the value of a boolean field that is nested under two objects.

 private boolean sourceWebsite(Registration registration) {
      Application application = registration.getApplication();
  if (application == null) {
     return true;
  }

  Metadata metadata = application.getMetadata();
  if (metadata == null) {
     return true;
  }

  Boolean source = metadata.getSource();
  if (source == null) {
     return true;
  }

  return !source;

}

I know this could be done in a single if(). I have added multiple ifs here for the sake of readability.

Is there a way that we could simplify the above if statements and have a simple util class that returns the value of Boolean source if the parent objects or not null

Aucun commentaire:

Enregistrer un commentaire