0

I think is a bad idea to return null values so I am looking to an alternative to this.

Here is my code:

 public Flight getFlight(String id)    {
      for (Flight f : this.flightList ) {
         if ( f.getId().equals(id))
            return f;
      }
      return null;
   }

I am looking for a certain Flight with a certain id and I want to return it.

If I return null I have to check every time if the object I returned is null or not. And I would like to avoid this if possible.

So any suggestions are welcome.

dres
  • 499
  • 6
  • 18
  • You could return an `Optional` but you would still have to check that the optional contains a value... – assylias Sep 03 '15 at 12:49
  • 4
    Well what do you want to happen when a flight doesn't exist for a given `id`? What's your business logic in that case? At some point you need to check whether or not the flight was retrieved successfully. – Kayaman Sep 03 '15 at 12:49
  • I think you're too focused on the "not checking null" part and too little on what you actually imagine your code would look like. You'll always have to check for `null`, whether it has a value (`Optional`) or whether an exception is thrown. – Jeroen Vannevel Sep 03 '15 at 12:51
  • This question is too broad without context of your app - we have no idea how `getFlight` is called, like... Is in your business domain? Maybe `throw new Exception("flight doesnt exists"); //this will be very slow, but fail-fast` or `return new Flight(null);`? Or `Flight f = new Flight(); flightList.Add(f); return f;`? Or `public bool tryGetFlight(String id, Flight out){return ((out = getFlight()) == null);}` or... So many possibilities, but we dont know how you expect it to work. – Jan 'splite' K. Sep 03 '15 at 12:59
  • Related question: http://stackoverflow.com/q/271526 – Tunaki Sep 03 '15 at 13:08
  • dres, @Kayaman is correct: you need to first decide what behavior you desire in a business context. What does it **mean** that `getFlight()` was called for a non-existent flight? – CPerkins Sep 03 '15 at 13:13

3 Answers3

3

This really depends if the event "a flight was not found" is considered exceptional or not.

When you call this method, if the calling code expects a valid flight all the time then not finding it is exceptional. There should be a flight but there isn't one. In this scenario, it is best to throw a custom Exception like FlightNotFoundException, that contains the id of the searched flight. It would typically be a RuntimeException: the flight should have been there, there is nothing you can do to recover from this.

However, if this requirement is not met, I don't see anything wrong with returning null, as long as this is correctly documented.

Tunaki
  • 132,869
  • 46
  • 340
  • 423
1

You can take a look at Null object pattern (in C# is implemented as string.Empty), since it

replaces check of NULL object instance. Instead of putting if check for a null value, Null Object reflects a do nothing relationship. Such Null object can also be used to provide default behaviour in case data is not available.

In your case it could be something like:

public class NullFlight extends AbstractFlight {

 @Override
   public String getId() {
      return "Not Available for null Flight!";
   }

   @Override
   public boolean isNil() {
      return true;
   }
}

Also this is a valid case when dealing with collections, you should return 'new' empty ones instead of Null.

Community
  • 1
  • 1
ekostadinov
  • 6,880
  • 3
  • 29
  • 47
0

Generally returning null instead of an object is a bad practice. There are two alternatives to returning null. Fistly I would recommend to check out Null Object Pattern with defined neutral ("null") behavior. The second idea is to thrown your own Exception when you can't return an object.

if ( f.getId().equals(id)){ return f; }else throw new FlightNotFoundException();

Thamiar
  • 600
  • 7
  • 22
  • Maybe, I was writing by heart, just to show the case. I narrowed my example, hoping It won't confuse. – Thamiar Sep 03 '15 at 13:17