2011-06-16 81 views
0

请找到以下代码,并帮助我编写更好的if...else代码。我觉得这是一个非常低于平均水平的方式来写其他如果。需要帮助来重构嵌套的if else语句

{ 
     Retrieve the number in focus. 
     string number= getnumber(); 
     string zipcode; 
     int corporate; 
     bool bCoverageInBet = false; 
     try 
     { 
      //Get the address and zipcode of the number 
      GetAddress(number, out address); 

      if (adress!= null) 
      { 
       zipcode = adress.zipcode 
       //if the following are null means this is first time call 
       if (!(string.IsNullOrEmpty(_loadedZipcode)) && _address != null) 
       { 
        if (zipcode.Equals(_loadedZipcode)) 
        { 
         if (adress.Equals(_address)) 
         { 
          if (focusChanged) 
          { 
           return result; 
          } 
         } 
         else 
         { 
          if (bCoverageInBet) 
          { 
           // case 2: Different address and different coverage which is in between, make a call anf get new valus for result          
           //return the new result 
          } 
          else 
          { 
           return //current result value; 
          } 
         } 
        } 
       }    
       else 
        { 
         _loadedZipcode = zipcode; 
         _address = adress; 
         GetResponse(out resp) 
         { 
          if ((resp != null)) 
          { 
           bool isCorporate = false; 
           corporate = getValues(); 
           if (corporate .Equals(100)) 
           { 
            result = true; 
            return result; 
           } 
           else if (corporate > 0 && corporate < 100) 
           { 
            //Make a call to get corporate 
            bCoverageInBet = true; 
            LocationResponse objResults; 
            if (GetAddressbycorporate(out objResults, out errMsg)) 
            { 
             if (objResults != null) 
             { 
              isCorporate = objResults.located; 
              if (isCorporate) 
              { 
               result = true; 
              } 
             } 

            } 
           } 
           return result; 
          } 
          return result; 
         } 
         else 
         { 
          DisplayError("No response "); 
          return result; 
         } 
        } 
       } 

      else 
      { 
       //To do: What is address comes null 
      } 
     } 
     catch (System.Exception ex) 
     { 
      //some ccode 
     } 
     return result; 
    } 

由于 ķ

+0

是什么结果呢?这甚至编译? – goalie7960 2011-06-16 15:37:09

+0

理想情况下,如果可以的话,你应该把它分成单独的方法。而你错过了;关闭第一个邮政编码= adress.zipcode – w69rdy 2011-06-16 15:38:34

回答

4

重构方法为更小的单位作为适合的。另外,尽早从方法返回而不是使用else子句。

E.g.而不是

if (adress!= null) 
{ 
    zipcode = adress.zipcode 

    //if the following are null means this is first time call 
    if (!(string.IsNullOrEmpty(_loadedZipcode)) && _address != null) 
    { 
    } 
    else 
    { 
     return false; 
    } 
} 
else 
{ 
    return false; 
} 

务必:

if (adress == null) 
{ 
    return false; 
} 

if (string.IsNullOrEmpty(_loadedZipcode) || _address == null) 
{ 
    return false; 
} 

有不少其他的问题,但应该让代码更清洁开始。

+0

即使这与单一的回报原则,可能是一个很好的折衷与真正巨大的嵌套块 – sll 2015-03-03 15:04:53

3

我不认为有人会“帮助”你重写这段代码。不过,我可以提供一些建议。

我已经发现,如果我试图重写和向后工作(链上),我会更容易追踪到最内层。根据IF块,有时候在适当的时候将它们分解成单独的方法更容易。

另外,不要忘记条件运算符。有时可以更清楚地使用它,而不是整个if else块。

例如,property = (boolean expression) ? (true value) : (false value);

这里是MSDN的链接就可以了:conditonal operator documentation