【发布时间】:2011-02-21 00:07:54
【问题描述】:
我在代码审查中遇到了以下方法。在循环内部,Resharper 告诉我 if (narrativefound == false) 不正确,因为 narrativeFound 总是正确的。我不认为是这种情况,因为为了将narrativeFound 设置为true,它必须首先通过条件字符串比较,那么它怎么可能总是正确的呢?我错过了什么吗?这是 Resharper 中的错误还是我们的代码中的错误?
public Chassis GetChassisForElcomp(SPPA.Domain.ChassisData.Chassis asMaintained, SPPA.Domain.ChassisData.Chassis newChassis)
{
Chassis c = asMaintained;
List<Narrative> newNarrativeList = new List<Narrative>();
foreach (Narrative newNarrative in newChassis.Narratives)
{
bool narrativefound = false;
foreach (Narrative orig in asMaintained.Narratives)
{
if (string.Compare(orig.PCode, newNarrative.PCode) ==0 )
{
narrativefound = true;
if (newNarrative.NarrativeValue.Trim().Length != 0)
{
orig.NarrativeValue = newNarrative.NarrativeValue;
newNarrativeList.Add(orig);
}
break;
}
if (narrativefound == false)
{
newNarrativeList.Add(newNarrative);
}
}
}
c.SalesCodes = newChassis.SalesCodes;
c.Narratives = newNarrativeList;
return c;
}
【问题讨论】:
-
对我来说似乎触手可及。为它编写一个测试用例并放入您的测试工具中以查看您是否可以将其用于代码覆盖可能是一件好事。你确实有测试,对吧? :)
-
我从来没有发现一个 resharper “代码无法访问”是不正确的。尽管有时我花了一些认真的思考才能找出原因。我并不是说您应该始终只信任 resharper 并删除它告诉您可以安全删除的任何内容,但不要贸然下结论它不正确。这段代码的调试步骤会告诉你发生了什么。
-
似乎这里的错误在于解释错误代码,似乎“Extraneous code”错误会比“Unreachable code”更有帮助。
标签: c# .net visual-studio .net-3.5 resharper