需要帮助重构嵌套的if语句

问题描述

| 请找到以下代码,并帮助我编写更好的
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;
   }
谢谢 ķ     

解决方法

        将方法重构为适当的较小单位。同样,从方法中早返回而不是使用else子句。 例如。代替
 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;
 }
还有很多其他问题,但这应该使代码更清晰。     ,        我认为没有人会去“帮助”您重写此代码。但是,我可以提供一些建议。 我发现,如果可能的话,更容易追踪到最深处的内部,并尝试重新编写并向后(沿着链条)进行工作。根据IF块,有时在适当的情况下将它们分解为单独的方法会更容易。 另外,不要忘记条件运算符。有时使用它比使用else块整体要清晰得多。 例如
property = (boolean expression) ? (true value) : (false value);
这是指向MSDN的链接:条件操作员文档     

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...