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!
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.
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.
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.
A void method specifically does not return any data or object. Pragmatically, a constructor does not return anything.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With