简体   繁体   中英

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 ?

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 ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM