Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Null checks in constructor vs in service method?

Tags:

java

object

oop

soa

This is a topic in one of our code reviews, I'd like more opinions.

Let's say I am writing a service that will allow me to insert a simple Person object into a database.

public class Person
{
   private String name;
   ....
}

We have a simple VerifyNotNull method throws IllegalArgumentException.

Which route would you take and why.

Option 1:

Verify not null in constructor of Person object.

public Person(String name)
{
     VerifyNotNull(name);//throws illegal arg exception if name is null
     this.name = name;
}


Option 2:

Allow Person to be constructed with null, verify not null on addPerson call.

public class PersonService
{
  public void addPerson(Person personToAdd)
  {
     VerifyNotNull(personToAdd.getName());//throws illegal arg exception
     //add code
  }
}

I don't like the idea of throwing Exceptions in constructors. To me option 2 feels right, I don't know how to explain or justify it though.

Is it acceptable to throws Exceptions in constructors?

Thanks for your help!

like image 407
Andy Avatar asked Nov 29 '11 17:11

Andy


People also ask

What is the best way to check null?

In order to check whether a Java object is Null or not, we can either use the isNull() method of the Objects class or comparison operator. Let's take an example to understand how we can use the isNull() method or comparison operator for null check of Java object.

Is it good practice to check for null?

Avoiding Null Checks Through Coding PracticesIt's usually a good practice to write code that fails early. So, if an API accepts multiple parameters that aren't allowed to be null, it's better to check for every non-null parameter as a precondition of the API.

Can we return null from constructor?

It's not possible for a constructor to return null , and even if an exception would be thrown from the constructor, the next line won't be called.

What's the difference between constructors and void methods?

A void method specifically does not return any data or object. Pragmatically, a constructor does not return anything.


1 Answers

The first approach is more fail-fast, which will increase the likelihood that you'll find the source of your bug more quickly. Think of it like this: if your error log starts telling you that a number of errors have been cropping up because you're trying to add people that have null names, you're going to want to know where those people's null names came from, right? Depending on how your system is structured, it's possible that the person was created miles away from the place where the person is getting added to the service. So you have no idea which of the four thousand places in your code is creating people without names.

So if I had to choose, I'd go with the first option.

Of course, it depends on your business model. If a person will a null name is a perfectly legal thing to create when you're in the data-entry phase, and it's not until you're getting ready to persist that person's information that you want to make sure it's passed validation, then that's a different story. In that case, you might even want to come up with a ValidatedPerson class that wraps Person, but indicates in a type-safe way that the addPerson method can only be called if someone has gone through the validation process, because the only way to create a ValidationPerson is through a specific validate method that checks the person's name.

like image 169
StriplingWarrior Avatar answered Nov 14 '22 20:11

StriplingWarrior