Mentha
Mentha

Reputation: 73

Return empty instead of null

I have method search for exceptions

ValidationException(String operation) {
    super("Not valid for operation " + checkOperation(operation));
}

And method for checking operation

private static String checkOperation(String operation) {
    if (operation != null)
        return operation;
    else
        return null;
}

If first method start working and operation == null we have message "Not valid for operation null". But it must be "Not valid for operation". What need write instead of return null?

Upvotes: 2

Views: 816

Answers (1)

Andy Turner
Andy Turner

Reputation: 140318

Put the space into the return value of checkOperation:

if (operation != null)
    return " " + operation;
else
    return "";

Then invoke like:

super("Not valid for operation" + checkOperation(operation));
                           // ^ remove the space here

Although I would consider it better to provide two overloads of the constructor:

  • one which takes no operation (and constructs the message Not valid for operation);
  • the other takes an operation (and constructs the message Not valid for operation whatever).

Upvotes: 5

Related Questions