代码优化 - 删除这个始终计算为 "true" 的表达式
Code Optimisation - remove this expression which always evaluates to "true"
我正在尝试使用 SonarQube 优化代码
List<string> itemList = serviceObject.GetItems();
我尝试使用以下代码验证列表
if(itemList != null && itemList.Any()
{
//Some operations
}
当上面的代码执行时,我收到 Sonarqube 错误 删除这个始终计算为“true”的表达式
所以我将代码重构为
if(itemList == null || !itemList.Any())
return;
//Some Operations
当上面的代码执行时,我收到 Sonarqube 错误 删除这个始终计算为“false”的表达式
谁能告诉我这里出了什么问题?
List<string> itemList = new List<string>();
itemList = serviceObject.GetItems();
if(itemList!=null && itemList.Count() > 0 )
{
//Some operations
}
计数足以验证列表
List 永远不会为 null,但不会有任何元素。但你必须实例化它,这样它就不会抛出异常。
我认为这是由于空比较造成的。
itemList != null
通过 [NotNull]
注释很有可能保证 serviceObject.GetItems();
不 return null。因此空检查是多余的。
How can I show that a method will never return null (Design by contract) in C#
您可以将其缩短为
if (itemList?.Count >0)
{
...
}
或
if (itemList?.Any() ==true)
{
...
}
?.
是 Null conditional operators 之一(另一个是 ?[]
),简称 Elvis operator
,它允许您在不抛出的情况下访问潜在的 null 变量。 Elvis 运算符后整个表达式的结果可以为空,如果变量为空,则 returns null
。
这意味着 itemList?.Count
return 是 Nullable<int>
,itemList?.Any()
是 Nullable<bool>
。 Nullable<T>
定义了自身与其基类型 T 之间的关系运算符,但不能在没有显式强制转换的情况下用作 T。这就是为什么需要 (itemList?.Any() ==true)
。
不过如果你使用Nullable Reference Types,itemList
就不能为空,所以一个简单的比较就足够了:
if (itemList.Count >0)
{
...
}
如果您通过在源文件中设置 #nullable enable
或在 csproj
文件中设置 <Nullable>enable</Nullable>
来启用可空引用类型,编译器会确保 return 引用类型 不是 null,这迫使您要么解决问题,要么明确指定变量可以为 null。
启用 NRT 后,此行:
List<string> itemList = serviceObject.GetItems();
只有在 GetItems
从不 return 编辑空值的情况下,才会在没有警告的情况下进行编译。如果编译器对此表示怀疑,它会发出警告,建议您解决问题或使用 :
显式声明 itemList
为可空
List<string>? itemList = serviceObject.GetItems();
if( itemList !=null ) {//做某事...}
我正在尝试使用 SonarQube 优化代码
List<string> itemList = serviceObject.GetItems();
我尝试使用以下代码验证列表
if(itemList != null && itemList.Any()
{
//Some operations
}
当上面的代码执行时,我收到 Sonarqube 错误 删除这个始终计算为“true”的表达式 所以我将代码重构为
if(itemList == null || !itemList.Any())
return;
//Some Operations
当上面的代码执行时,我收到 Sonarqube 错误 删除这个始终计算为“false”的表达式
谁能告诉我这里出了什么问题?
List<string> itemList = new List<string>();
itemList = serviceObject.GetItems();
if(itemList!=null && itemList.Count() > 0 )
{
//Some operations
}
计数足以验证列表
List 永远不会为 null,但不会有任何元素。但你必须实例化它,这样它就不会抛出异常。
我认为这是由于空比较造成的。
itemList != null
通过 [NotNull]
注释很有可能保证 serviceObject.GetItems();
不 return null。因此空检查是多余的。
How can I show that a method will never return null (Design by contract) in C#
您可以将其缩短为
if (itemList?.Count >0)
{
...
}
或
if (itemList?.Any() ==true)
{
...
}
?.
是 Null conditional operators 之一(另一个是 ?[]
),简称 Elvis operator
,它允许您在不抛出的情况下访问潜在的 null 变量。 Elvis 运算符后整个表达式的结果可以为空,如果变量为空,则 returns null
。
这意味着 itemList?.Count
return 是 Nullable<int>
,itemList?.Any()
是 Nullable<bool>
。 Nullable<T>
定义了自身与其基类型 T 之间的关系运算符,但不能在没有显式强制转换的情况下用作 T。这就是为什么需要 (itemList?.Any() ==true)
。
不过如果你使用Nullable Reference Types,itemList
就不能为空,所以一个简单的比较就足够了:
if (itemList.Count >0)
{
...
}
如果您通过在源文件中设置 #nullable enable
或在 csproj
文件中设置 <Nullable>enable</Nullable>
来启用可空引用类型,编译器会确保 return 引用类型 不是 null,这迫使您要么解决问题,要么明确指定变量可以为 null。
启用 NRT 后,此行:
List<string> itemList = serviceObject.GetItems();
只有在 GetItems
从不 return 编辑空值的情况下,才会在没有警告的情况下进行编译。如果编译器对此表示怀疑,它会发出警告,建议您解决问题或使用 :
itemList
为可空
List<string>? itemList = serviceObject.GetItems();
if( itemList !=null ) {//做某事...}