开发者

return in short-hand if

I try to rewrite this method using short-hand if:

 public string checkInputParamters(string baseUrl, string owner, string documentId, string user, string secret, string type)
    {
      if (String.IsNullOrEmpty(baseUrl)) 
          return ExceptionsCodes.BASE_URL_CANNOT_BE_NULL_OR_EMPTY.ToString("g");

      if (String.IsNullOrEmpty(owner))
          return ExceptionsCodes.OWNER_CANNOT_BE_NULL_OR_EMPTY.ToString开发者_如何学运维("g");

      return "";
    }

I cannot do like this because return forces me to put a value after ":" iso ";".

 public string checkInputParamters(string baseUrl, string owner, string documentId, string user, string secret, string type)
    {
       return ((null == baseUrl) || (string.Empty == baseUrl)) ? ExceptionsCodes.BASE_URL_CANNOT_BE_NULL_OR_EMPTY.ToString("g");
       return ((null == owner) || (string.Empty == owner)) ? ExceptionsCodes.OWNER_CANNOT_BE_NULL_OR_EMPTY.ToString("g");
    }

Any ideas?


return String.IsNullOrEmpty(baseUrl)
          ? YourBaseUrlException
          : String.IsNullOrEmpty(owner)
               ? YourOwnerException
               : "";


Another thing to note is that you can replace

((null == owner) || (string.Empty == owner))

with

String.IsNullOrEmpty(owner)


You are missing a default return if it isn't either one of these two cases.

For instance:

return String.IsNullOrEmpty(owner)?ExceptionsCodes.OWNER_BLAH.ToString("g"):(String.IsNullOrEmpty(baseUrl)?ExceptionsCodes.BASEURL_BLAH.ToString("g"):"");


Is this what you had in mind?

public string checkInputParamters(string baseUrl, string owner, string documentId, string user, string secret, string type)
    {
        return String.IsNullOrEmpty(baseUrl) ? 
            ExceptionsCodes.BASE_URL_CANNOT_BE_NULL_OR_EMPTY.ToString("g") :
            ( String.IsNullOrEmpty(owner) ? ExceptionsCodes.OWNER_CANNOT_BE_NULL_OR_EMPTY.ToString("g") : "" );
    }


The method has to return something. I doubt the top example compiles. (I see you changed it now.)

Combine the other two answers.

public string checkInputParamters(string baseUrl, string owner, string documentId, string user, string secret, string type)
{
  return String.IsNullOrEmpty(baseUrl) ?
      ExceptionsCodes.BASE_URL_CANNOT_BE_NULL_OR_EMPTY.ToString("g")
  : (String.IsNullOrEmpty(owner) ?
      ExceptionsCodes.OWNER_CANNOT_BE_NULL_OR_EMPTY.ToString("g")
  : String.Empty;


}


imo, in this case a solution using the first examples layout is far more readable. I would be more concerned about readability here rather than trying to code-golf your code.

Zombies solution of swapping in String.IsNullOrEmpty(owner) would be a nice enhancement.


I personally dislike inline if but this should do the trick

            return (
                String.IsNullOrEmpty(baseUrl)?
                ExceptionsCodes.BASE_URL_CANNOT_BE_NULL_OR_EMPTY.ToString("g"):(
                    String.IsNullOrEmpty(owner)?
                    ExceptionsCodes.OWNER_CANNOT_BE_NULL_OR_EMPTY.ToString("g"):
                    string.Empty
                )
            );

Pretty much the same as everyone else is saying, I just like to have the (implied) ()'s clearly visible in inline ifs

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜