-1

我一直在努力重构一些代码,然后我做了这样的事情。主要思想是我有几个包含实现的方法,在我的例子中它们更改了引用的字符串变量名称

我在这里不喜欢的情况是,在每个 if 语句中都返回相同的变量(但结果不同)。

所以我的问题是有人对最终重构这个有更好的想法吗?在 if 语句中执行此操作是否有任何问题(从逻辑、干净的代码方面等)。

我所有的方法都有ref关键字(用于字符串变量名)。很抱歉造成混乱!

 private string GenerateNameFrom(IRow row)
 {
    string name = string.Empty;

    if (Method1(name,row))
        return name;
    else if (Method2(name,row))
        return name;
    else if (Method3(name,row))
        return name;
    else return "Null";
}
4

5 回答 5

4

这是一种方法:

private string GenerateNameFrom(IRow row)
{
    var name = "";
    return (Method1(ref name) || Method2(ref name) || Method3(ref name)) ? name : "Null";
}
  • 用于var实例化空字符串,不需要写string两次。
  • using||意味着第一个返回的方法true将满足条件,并且不会执行其他方法。
  • 使用?:运算符进行条件返回。

注意:正如 Matthew Watson 评论的那样,您可能缺少ref(orout关键字-因为即使字符串是引用类型,它也是不可变的,因此如果您希望您的方法影响name参数的内容,您必须将其发送为ref或 as out,因为更改它的值的唯一方法是为其分配一个新字符串。

(也转换String.Empty"",但这只是个人喜好)

于 2018-05-21T09:43:57.737 回答
3

变量名总是有一个空字符串。当您在 if 语句之前声明和初始化时

任何方式都低于获得相同结果的捷径。结果将与以下代码和您的代码相同:

if (Method1(name) || Method2(name) || Method3(name))
{
    return name;
}
else
{ 
    return "Null";
}

或更具体地说,使用三元运算符

(Method1(name) || Method2(name) || Method3(name)) ? name : "Null";
于 2018-05-21T09:45:42.540 回答
2

看不到您的方法的实现,但我假设如下:

public bool Method1(ref string name)
{
    if (condition)
    {
        name = "SomeValue";
        return true;
    }

    return false;
}

您可以重构这些方法以返回更新后的名称:

public string Method1(name)
{
    if(condition)
    {
        return "SomeValue";
    }

    return null;
}

然后你可以空合并方法调用:

private string GenerateNameFrom(IRow row)
{
    string name = string.Empty;
    return Method1(name)
        ?? Method2(name)
        ?? Method3(name)
        ?? "Null";
}
于 2018-05-21T09:49:11.233 回答
1

所有这一切,Method1(name)并对Method2(name)输入字符串进行一些验证name并返回bool. 在这种情况下,建议您switch可能使用语句将所有这些验证逻辑组合在单个方法中,并改用该方法

于 2018-05-21T09:45:19.380 回答
1

我会尽量避免ref在这种情况下使用。相反,您可以使各种方法返回一个元组(bool success, string value),如下所示:

public static (bool success, string value) Method1(string name)
{
    if (name == "test")
        return (true, "changed");

    return (false, null);
}

public static (bool success, string value) Method2(string name)
{
    if (name == "test")
        return (true, "changed");

    return (false, null);
}

public static (bool success, string value) Method3(string name)
{
    if (name == "test")
        return (true, "changed");

    return (false, null);
}

然后你可以像这样编写调用代码(它不是更短,但它避免了ref)。你是否更喜欢这个可能是一个品味问题......

private string GenerateNameFrom(/*IRow row*/)
{
    string name = string.Empty;

    var result = Method1(name);

    if (result.success)
        return result.value;

    result = Method2(name);

    if (result.success)
        return result.value;

    result = Method3(name);

    if (result.success)
        return result.value;

    return null;
}

或者,如果null可以用来表示“没有结果”,那么只需做类似的事情,但检查返回值null

private string GenerateNameFrom(/*IRow row*/)
{
    string name = string.Empty;

    var result = Method1(name);

    if (result != null)
        return result;

    result = Method2(name);

    if (result != null)
        return result;

    return Method3(name);
}

public static string Method1(string name)
{
    if (name == "test")
        return "changed";

    return null;
}

public static string Method2(string name)
{
    if (name == "test")
        return "changed";

    return null;
}

public static string Method3(string name)
{
    if (name == "test")
        return "changed";

    return null;
}
于 2018-05-21T09:52:03.273 回答