Cristian Boariu
Cristian Boariu

Reputation: 9621

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?

Upvotes: 1

Views: 677

Answers (7)

LaustN
LaustN

Reputation: 720

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

Upvotes: 0

Mike
Mike

Reputation: 6028

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.

Upvotes: 0

UncleO
UncleO

Reputation: 8459

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;


}

Upvotes: 0

Wedge
Wedge

Reputation: 19875

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") : "" );
    }

Upvotes: 0

Wim
Wim

Reputation: 12092

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"):"");

Upvotes: 0

David Hedlund
David Hedlund

Reputation: 129832

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

Upvotes: 8

ZombieSheep
ZombieSheep

Reputation: 29963

Another thing to note is that you can replace

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

with

String.IsNullOrEmpty(owner)

Upvotes: 8

Related Questions