代码之家  ›  专栏  ›  技术社区  ›  The Matt

此代码段中是否有无法访问的代码?我不这么认为,但雷斯哈珀不是这样告诉我的

  •  14
  • The Matt  · 技术社区  · 15 年前

    我在代码评审中遇到了以下方法。在循环中,Resharper告诉我 if (narrativefound == false) 不正确,因为 narrativeFound 总是正确的。我不认为是这样,因为 叙事发现 如果为真,则必须首先传递条件字符串比较,那么如何始终为真呢?我错过什么了吗?这是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; 
    }
    
    5 回复  |  直到 15 年前
        1
  •  31
  •   Mark Byers    15 年前

    变量 narrativefound 当控件到达该语句时永远不会为真:

    narrativefound = true;
    // ...
    break;  // This causes control to break out of the loop.
    

    我想Resharper是想告诉你 narrativefound == false 永远都是真的。

        2
  •  4
  •   Mark Rushakoff    15 年前

    你不需要 narrativeFound 变量。在你设定它为真的范围内,你打破了 foreach 循环。如果你不把它设为真,你就不会打破,然后你加上 newNarrative newNarrativeList .

    所以,这个可以重写为

    foreach (Narrative newNarrative in newChassis.Narratives)
    {
         foreach (Narrative orig in asMaintained.Narratives)
         {
                if (string.Compare(orig.PCode, newNarrative.PCode) == 0)
                {
                          if (newNarrative.NarrativeValue.Trim().Length != 0)
                          {
                             orig.NarrativeValue = newNarrative.NarrativeValue;
                             newNarrativeList.Add(orig);                            
                          }
                          break;
                }
    
                newNarrativeList.Add(newNarrative);                 
         }
    }
    
        3
  •  4
  •   Cheeso    15 年前

    这是你代码中的一个错误。

    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);                             
                          } 
    // narrativeFound == true, but now we exit the for loop
                          break; 
                } 
    // narrativeFound is always false here.  The test is redundant
                if (narrativefound == false) 
                { 
                     newNarrativeList.Add(newNarrative);  
                } 
         } 
    } 
    
        4
  •  1
  •   Otávio Décio    15 年前

    R是正确的,因为如果你把叙述变为真实,你就在你设定好它之后,从前臂中挣脱出来。

        5
  •  0
  •   Greg Bogumil    15 年前

    我相信它告诉你,因为如果narrativefound设置为true,那么for循环将退出(break;)。因此,如果对if(narrativeFound==false)进行评估,则其值始终为false。

    推荐文章