有什么办法可以进一步减少代码行数?

时间:2018-11-01 07:08:04

标签: c# if-statement

我正在寻找减少此功能代码行数的方法。任何帮助表示赞赏!

private bool CanSubmitPackage ( object obj )
    {
        // ---- Checks if the package contains any files to be submitted ----
        if ( _selectedWorkspace.Package.Files != null )
        {
            if ( _selectedWorkspace.Package.Files.Count > 0 )
                if ( _selectedWorkspace.Package.Platform != null || _selectedWorkspace.Package.Platform != "" )
                    if ( _selectedWorkspace.Package.PackagePath != null || _selectedWorkspace.Package.PackagePath != "" )
                        if ( _selectedWorkspace.Package.PackageSize != null || _selectedWorkspace.Package.PackageSize != "" )
                            if ( _selectedWorkspace.Package.SubmittedBy != null || _selectedWorkspace.Package.SubmittedBy != "" )
                                return true;
                            else
                                return false;
                        else
                            return false;
                    else
                        return false;
                else
                    return false;
            else
                return false;
        }
        else
            return false;
    }

7 个答案:

答案 0 :(得分:8)

return ( _selectedWorkspace.Package.Files?.Count > 0
    && !string.IsNullOrEmpty(_selectedWorkspace.Package.Platform)
    && !string.IsNullOrEmpty(_selectedWorkspace.Package.PackagePath)
    && !string.IsNullOrEmpty(_selectedWorkspace.Package.PackageSize) 
    && !string.IsNullOrEmpty(_selectedWorkspace.Package.SubmittedBy));

答案 1 :(得分:4)

您可以使用&&运算符和string.IsNullOrEmpty方法来使其更短,更易读:

return ( _selectedWorkspace.Package.Files != null 
&& _selectedWorkspace.Package.Files.Count > 0 
&& !string.IsNullOrEmpty(_selectedWorkspace.Package.Platform)
&& !string.IsNullOrEmpty(_selectedWorkspace.Package.PackagePath)
&& !string.IsNullOrEmpty(_selectedWorkspace.Package.PackageSize)
&& !string.IsNullOrEmpty(_selectedWorkspace.Package.SubmittedBy));

如果您使用的是c#6+,则可以走得更远(一点点)并使用 Expression Bodied method

private bool CanSubmitPackage () => //I'm not sure about object obj argument
    ( _selectedWorkspace.Package.Files != null 
    && _selectedWorkspace.Package.Files.Count > 0 
    && !string.IsNullOrEmpty(_selectedWorkspace.Package.Platform)
    && !string.IsNullOrEmpty(_selectedWorkspace.Package.PackagePath)
    && !string.IsNullOrEmpty(_selectedWorkspace.Package.PackageSize)
    && !string.IsNullOrEmpty(_selectedWorkspace.Package.SubmittedBy));

如果要缩短代码文本的长度,可以声明一个简短的命名变量,并使用它代替_selectedWorkspace.Package

var p = _selectedWorkspace.Package;
return (p.Files?.Count > 0
       && !string.IsNullOrEmpty(p.Platform)
       && !string.IsNullOrEmpty(p.PackagePath)
       && !string.IsNullOrEmpty(p.PackageSize)
       && !string.IsNullOrEmpty(p.SubmittedBy));;

答案 2 :(得分:3)

这是一种实现方式

更新

return ( _selectedWorkspace != null
    && _selectedWorkspace.Package != null
    && _selectedWorkspace.Package.Files != null 
    && _selectedWorkspace.Package.Files.Count > 0 
    && !string.IsNullOrEmpty(_selectedWorkspace.Package.Platform)
    && !string.IsNullOrEmpty(_selectedWorkspace.Package.PackagePath)
    && !string.IsNullOrEmpty(_selectedWorkspace.Package.PackageSize)
    && !string.IsNullOrEmpty(_selectedWorkspace.Package.SubmittedBy));

Old Ans

return ( _selectedWorkspace.Package.Files != null 
    && _selectedWorkspace.Package.Files.Count > 0 
    && ( _selectedWorkspace.Package.Platform != null || _selectedWorkspace.Package.Platform != "" )
    && ( _selectedWorkspace.Package.PackagePath != null || _selectedWorkspace.Package.PackagePath != "" )
    && ( _selectedWorkspace.Package.PackageSize != null || _selectedWorkspace.Package.PackageSize != "" )
    && ( _selectedWorkspace.Package.SubmittedBy != null || _selectedWorkspace.Package.SubmittedBy != "" ));

答案 3 :(得分:2)

这是另一种方式

private bool CanSubmitPackage(object obj)
        {
            // ---- Checks if the package contains any files to be submitted ----
            if (_selectedWorkspace.Package.Files == null) return false; 
            if (_selectedWorkspace.Package.Files.Count <= 0) return false;
            if (_selectedWorkspace.Package.Platform == null && _selectedWorkspace.Package.Platform == "") return false; 
            if (_selectedWorkspace.Package.PackagePath == null && _selectedWorkspace.Package.PackagePath == "") return false;
            if (_selectedWorkspace.Package.PackageSize != _selectedWorkspace.Package.PackageSize != "")
                return _selectedWorkspace.Package.SubmittedBy != null || _selectedWorkspace.Package.SubmittedBy != "";

            return false;
        }

答案 4 :(得分:2)

我希望使用@xneg的答案(https://stackoverflow.com/a/53096740/10588170),但是,您也可以删除所有'else'语句,并在'if'块之后用单个return语句替换它们。 请确保将来使用string.IsNullOrEmpty(_str),否则必须使用&&而不是||,否则会出现异常,因为您正在检查_str是否为空,如果它为null,是否不为null,则您将根本不检查_str的内容。我很确定那不是您想要的。

答案 5 :(得分:1)

我可能会拆分条件,例如:

public bool HasFile
{
   get { return _selectedWorkspace.Package.Files != null && _selectedWorkspace.Package.Files.Count > 0; }
}

public bool HasPlatform
{
   get { return !string.IsNullOrEmpty(_selectedWorkspace.Package.Platform); }
}

// Do that for all conditions

然后您可以像这样写最后的if

bool result = HasFile && HasPlatform && … 

答案 6 :(得分:1)

如果您希望使其基本保持一致,则所有失败的IF语句的返回均为false,因此只需包含一次即可。如果您当前的任何检查失败,它将无法执行“ return true”,因此您只能有一个最终的return语句,如下所示:

private bool CanSubmitPackage ( object obj )
{
    // ---- Checks if the package contains any files to be submitted ----
    if ( _selectedWorkspace.Package.Files != null )
        if ( _selectedWorkspace.Package.Files.Count > 0 )
            if ( _selectedWorkspace.Package.Platform != null || _selectedWorkspace.Package.Platform != "" )
                if ( _selectedWorkspace.Package.PackagePath != null || _selectedWorkspace.Package.PackagePath != "" )
                    if ( _selectedWorkspace.Package.PackageSize != null || _selectedWorkspace.Package.PackageSize != "" )
                        if ( _selectedWorkspace.Package.SubmittedBy != null || _selectedWorkspace.Package.SubmittedBy != "" )
                            return true;

    // One of the above checks failed, that's why we're running this line
    return false;
}

我也更喜欢@ xneg,@ SeM或@Mihir Dave的方法(因为它们基本上是相同的答案),但是它的确归结于您觉得最舒适/可读的。我肯定会使用方法String.IsNullOrEmpty()来简化您的if语句。

此外,?在xneg的答案:_selectedWorkspace.Package.Files?.Count中的作用是说“如果Files不为null,则检索Files.Count,否则返回null。然后在C#中使用如下语句,null > 0始终返回false

相关问题