What's an acceptable way to write Java methods where the return value might not be valid?

I am coming from a C programming background. I want to understand what is the best way to write a Java API where a value is returned but might not be valid.

I have a class that implements a binary search tree. This method has a getRootValue () method that returns the root value.

C code -

boolean getRootValue(int *answer) {
    if (root != null) {
        *answer = value;
        return TRUE;
    } else {
        return FALSE;
    }
}

      

At the end of the user it will look like this:

if (getRootValue(&answer)) {
    //process valid answer
}

      

The user who calls this function checks the return and only considers answer

if the return value is TRUE

.

Java skeleton code -

package algorithm;
class bst {
    node root;
    class node {
        int value;
        node left;
        node right
    }

    //getRootValue()
}

      

Custom end -

package user;
import algorithm.bst;
...
//bst.getRootValue();

      

In Java, what would be the correct way (given that root is only known by the BST, not the user)? Should we send a custom class object that contains both boolean and response? If so, where should this class be defined?

Is there any other method? Should I think differently for my implementation of the bst class?

The previous questions ask the question of returning multiple values. This is not really my question. I want to know the ideal way to implement this particular case, which may not even require returning multiple objects.

+3


source to share


3 answers


I wouldn't even write a method. There is nothing special at the root, for which you need a specific method to get its value. The entire method is redundant. You need to get something to root, and you need to get something from any node (and its left and right children too).

I would write two methods:



  • Node Tree.getRoot()

  • int Node.getValue()

    ...

The exception you are talking about is done by returning null

from getRoot()

which the caller can easily detect.

+2


source


From Java 8, you can use Optional as return for unsure answer.

Optional<Integer> getRootValue() {
    return root != null ? Optional.of(value) : Optional.empty(); 
}

      



The client code can then use the lambda expression to process the value, if present, smoothly.

getRootValue().ifPresent(value -> {
    // process the value
    System.out.println(value);
})

      

+2


source


Not sure if this is the best way to do it, but in Java I would recommend creating / using an exception. The user can then check if the return value is valid based on whether the exception was thrown.

int getRootValue(int answer) throws Exception{
    if (root != null){
        return value;
    }else{
        throw new NullPointerException("Root is null");
    }
}

      

at the end of the user

try{
    myVal = getRootValue(answer);
}catch(Exception e){
    e.printStackTrace(); //There was an error here
}

      

+1


source







All Articles