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

发布于 2024-11-15 21:55:58 字数 3824 浏览 1 评论 0原文

请找到以下代码并帮助我编写更好的 if...else 代码。我觉得这是一种非常低于平均水平的 else if 编写方式。

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

谢谢 K

Please find the following code and help me write a better if...else code. I feel this is a very below average way to write else if.

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

Thanks
K

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

開玄 2024-11-22 21:55:58

根据需要将该方法重构为更小的单元。另外,尽早从方法返回而不是使用 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;
 }

Do:

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

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

还有很多其他问题,但这应该会使代码一开始就更干净。

Refactor the method into smaller units as appropriate. Also, return early from the method rather than using else clauses.

E.g. instead of

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

Do:

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

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

There are quite a few other problems, but that should make the code cleaner to start with.

浮生未歇 2024-11-22 21:55:58

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

我发现更容易追踪到最内在的 if 并尝试重写并向后(沿着链向上)工作。根据 IF 块的不同,有时在适当的情况下将它们分解为单独的方法会更容易。

另外,不要忘记条件运算符。有时使用它比使用整个 if else 块更清晰。

例如,property = (布尔表达式) ? (真值) : (假值);

这是 MSDN 的链接: 条件操作员文档

I don't think anyone is going to "help" you rewrite this code. However, I can offer some suggestions.

I have found it easier to trace my way down to the inner most if and try to rewrite and work my way backwards (up the chain). Depending on the IF block, it is sometimes easier to break them out into separate methods where appropriate.

Also, don't forget about the conditional operator. It can sometimes be clearer to use that than a whole if else block.

For example, property = (boolean expression) ? (true value) : (false value);

Here is a link to MSDN on it: conditonal operator documentation

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文