简体   繁体   English

如何消除“否则-如果”陈述

[英]How to eliminate “else-if” statements

My function get QueryString from some Web page as a string . 我的函数从某个网页获取QueryString作为string I need to parce it, to check, what strategy i must use. 我需要对其进行检查,以检查我必须使用哪种策略。

Now my code looks ugly (i think so): 现在我的代码看起来很丑陋(我认为是这样):

public QueryStringParser(string QueryString)
        {
            if (string.IsNullOrEmpty(QueryString))
            {
                this._mode = Mode.First;
            }
            else if (QueryString.Contains(_FristFieldName) && !QueryString.Contains(_SecondFieldName))
            {
                this._mode = Mode.Second;
            }
            else if (!QueryString.Contains(_FristFieldName) && QueryString.Contains(_SecondFieldName))
            {
                this._mode = Mode.Third;
            }
            else
            {
                throw new ArgumentException("QueryString has wrong format");
            }
        }

There must'n't be both FieldNames in one QueryString. 一个QueryString中不能同时有两个FieldName。

How to change this code to be mo readable. 如何将此代码更改为可读性。

I would at least remove some duplication in checking the existing fields: 我至少会在检查现有字段时删除一些重复项:

public QueryStringParser(string QueryString) {
    if (string.IsNullOrEmpty(QueryString))
        this._mode = Mode.First;
    else {
        bool has_1st = QueryString.Contains(_FristFieldName);
        bool has_2nd = QueryString.Contains(_SecondFieldName);
        if      ( has_1st && !has_2nd) this._mode = Mode.Second;
        else if (!has_1st &&  has_2nd) this._mode = Mode.Third;
        else
            throw new ArgumentException("QueryString has wrong format");
    }
}

You could put the determination of the Mode into a separate method and return the Mode value; 您可以将Mode的确定放到单独的方法中,然后返回Mode值; that way you can eliminate the if-else statements 这样就可以消除if-else语句

public QueryStringParser(string QueryString)
{
    this._mode = DetermineMode(QueryString);
}

private Mode DetermineMode(string QueryString)
{
    // Input is NULL STRING
    if (string.IsNullOrEmpty(QueryString))
        return Mode.First;

    bool hasFirst = QueryString.Contains(_FristFieldName);
    bool hasSecond = !QueryString.Contains(_SecondFieldName);

    // Query using FirstName
    if (hasFirst && !hasSecond)
        return Mode.Second;

    //Query using SecondName
    if (!hasFirst && hasSecond)
        return Mode.Third;

    //Insufficient info to Query data
    throw new ArgumentException("QueryString has wrong format");
}

[EDIT] Removed the double checking on the existance of field names and use variables instead. [编辑]删除了对字段名称是否存在的双重检查,改为使用变量。

The code is readable, you could use some nesting on common conditions, to make parts clearer. 代码可读性强,可以在常见条件下使用一些嵌套,以使零件更清晰。

You cant use Switch because the comparison has to be to a static value. 您不能使用Switch,因为比较必须为静态值。

Comments and Indentation 注释和缩进


It is already good-looking. 已经好看了。

public QueryStringParser(string QueryString)
        {
            // Input is NULL STRING
            if (string.IsNullOrEmpty(QueryString))
            {
                this._mode = Mode.First;
            }

            // Query using FirstName
            else if (QueryString.Contains(_FristFieldName) &&
                    !QueryString.Contains(_SecondFieldName))
            {
                this._mode = Mode.Second;
            }

            //Query using SecondName
            else if (!QueryString.Contains(_FristFieldName) &&
                      QueryString.Contains(_SecondFieldName))
            {
                this._mode = Mode.Third;
            }

            //Insufficient info to Query data
            else
            {
                throw new ArgumentException("QueryString has wrong format");
            }
        }

I hope she looks Beautiful to you now... 我希望她现在对你看起来很美...

GoodLUCK!! 祝好运!!

The code is readable. 该代码是可读的。 You could make it shorter by removing the brackets as the statements are only one-liners: 您可以通过删除方括号来使其更短,因为这些语句只是单行代码:

public QueryStringParser(string QueryString)
        {
            //check if string is empty
            if (string.IsNullOrEmpty(QueryString))
                this._mode = Mode.First;
            else
            {
               bool hasFirst = QueryString.Contains(_FristFieldName);
               bool hasSecond= QueryString.Contains(_SecondFieldName);

               //check if string contains first field name but not second field name
               if (hasFirst  && !hasSecond)
                   this._mode = Mode.Second;
               //check if string contains second field name but not first field name
               else if (!hasFirst && hasSecond)
                   this._mode = Mode.Third;
               //default - error
               else
                   throw new ArgumentException("QueryString has wrong format");
            }
        }

Also - removed duplicate calls to the Contains method 还-删除了对Contains方法的重复调用

I wouldn't suggest using any shorthand operators as these would make the code far less readable. 我不建议使用任何速记运算符,因为它们会使代码的可读性大大降低。

Lastly - in line comments! 最后-在线评论!

Use something like ReSharper or Refactor Pro! 使用诸如ReSharper或Refactor Pro之类的东西! to help tidy up your code and encourage good practice. 帮助整理代码并鼓励良好实践。

使用开关盒。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM