2010-01-29 4 views
1

je tente de réécrire cette méthode en utilisant sténographie si:retour en sténographie si

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

Je ne peux pas faire comme cela parce que le retour me force à mettre une valeur après « : » 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"); 
    } 

Des idées?

+0

Dans le premier exemple, vous ne renvoyez rien si les deux instructions 'if' sont fausses. Cela donnera une erreur de compilation. Pourriez-vous corriger votre code? –

+0

Corrigé, merci. –

+2

quel est le point d'utiliser une main courte si? cela rend le code laid et illisible, surtout dans ce cas, alors que les deux «if» dans le code ci-dessus sont clairs au premier coup d'œil. –

Répondre

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

Une autre chose à noter est que vous pouvez remplacer

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

avec

String.IsNullOrEmpty(owner) 
+0

Merci pour le point! –

0

Il vous manque un retour par défaut si ce n'est pas l'un de ces deux cas.

Par exemple:

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

Est-ce que vous aviez en tête?

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

La méthode doit retourner quelque chose. Je doute que l'exemple le plus haut compile. (Je vois que vous l'avez changé maintenant.)

Combinez les deux autres réponses.

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; 


} 
0

Dans ce cas, une solution utilisant la première disposition d'exemples est beaucoup plus lisible. Je serais plus préoccupé par la lisibilité ici plutôt que d'essayer de code-golf votre code.

Zombies solution d'échange dans String.IsNullOrEmpty(owner) serait une belle amélioration.

0

Je n'aime personnellement en ligne si mais cela devrait faire l'affaire

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

À peu près la même chose que tout le monde dit, j'aime juste avoir la (implicite)() s 'clairement visible dans ifs inline

Questions connexes