What's the better way to verify null parameters from objects relationships

104 Views Asked by At

well i've a good question about verification of relationships.

Follows below the three classes to suppose the question:

public class Son {

    private Integer idSon;
    private String name;
    private Father father;
}

public class Father {

    private Integer idFather;
    private String name;
    private GrandFather grandFather;
}

public class GrandFather {

    private Integer idGrandFather;
    private String name;
}

Following these principies, let's suppose that we've to do relationships verifications, instead every time we do something like this "in differents parts of our code":

Son son = new SonBusiness().getById(idSon); //Get a Son from database
if(son.getFather == null){
    throw new Throwable("Father's Son does not exist");
}

wouldn't be better create some method like:

public void verifySonsDatas(Son son, Bollean verifyFather, Boolean verifyGrandFather) throws Throwable{
    if(son == null){
        throw new Throwable("Son does not exist");
    }
    if(verifyFather){
        if(son.getFather == null){
            throw new Throwable("Father's Son does not exist");
        }
    }

and so on for any son's ralationships..

Somebody else agree that with this approach we avoid many redundant codes and we can concentrate the verification inside an unique method...?

If we think about 10 relations, imagine writting if(object == null) it does not appear be a good thing for me.

Thanks!

5

There are 5 best solutions below

0
On

If null is part of the contract then I suppose yes creating a helper method is better than repeating the same code all over the place. However the much better alternative is to just avoid this in some way. For example consider the following:

public Son(int idSon, String name, Father father) {
    if (father == null) {
        this.father = new Father(0, "Unknown", null);
    }
}

Or if nulls are illegal then make the constructor the delegate for the exception:

public Son(int idSon, String name, Father father) {
    if (father == null) {
        throw new IllegalArgumentException(
            "Children must have parents to be born."
        );
    }
}

If an object is invalid within its own specifications it makes more sense not to construct it at all.

Finally, you are checking for a null and throwing an exception:

if(son.getFather == null){
    throw new Throwable("Father's Son does not exist");
}

So why bother? If nulls are illegal, why not get a NullPointerException?

0
On

I would rather suggest to find the corresponding part of your code, where you have to check this only once or twice. Why should you call this check more than twice? Shouldnt it be enough to check this if 1. you save a son or 2. load the son from database?

0
On

I think if you have to place the above checks at so many places, with throwing some Exception with predefined messages then this approach at least provide a reason (i.e. all the exception will be managed at a central place) and thus it is valid.
I will suggest you should have different function each to check individually Son, Father and GrandFather and so on and frame a method with those boolean conditions.

But if this exceptions, are just thrown to break without any meanings then you should consider placing the obj == null checks wher you need to check them.

You can also read Avoiding != null statements for information.

0
On

How about implementing some sort of interface to specify the family's inheritance (heh heh).

interface IChild {
    abstract int GetParent();
}

In Son:

Son implements IChild 

@override 
int GetParent() { return this.father.idFather; }

In Father:

Father implements IChild 

@override 
int GetParent() { return this.grandFather.idGrandFather; }
0
On

Indeed exists many ways to implement it. But in my case i can't create a rule like "can't exist any relations == null" or any other implementation because in differents actions, i've differents approaches, sometimes i've to verify the son's father for example. Another i've to verify the father's and grandfather's son, My method need top be flexible and generic. I've analised what kjhf said me, implementing it by another way, without be mandatory the parameters of the implementations... Some cases i've three relationships, another i've five, another only one. Anyway, i'm trying to discover some way to do it only one time and all of the objects use the same method. nowadays i'm creating a new method inside each business class that references one entity. Then if i've 10 entities, logically i've 10 methods of verification. If someone else have some ideia, please, i'm waiting.

Thanks everybody.