2010-05-18 15 views
14

Tengo el siguiente método que encontré en una revisión del código. Dentro del circuito, Resharper me dice que if (narrativefound == false) es incorrecto porque narrativeFound es siempre verdadero. No creo que este sea el caso, porque para establecer narrativeFound en true, primero tiene que pasar la cadena condicional comparar, entonces, ¿cómo puede ser siempre cierto? ¿Me estoy perdiendo de algo? ¿Es esto un error en Resharper o en nuestro código?¿Hay código inalcanzable en este fragmento? No lo creo, pero Resharper me dice lo contrario

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; 
} 
+0

Parece alcanzable para mí. Podría ser una buena idea para escribir un caso de prueba y arrojar su arnés de prueba para ver si puede obtener cobertura de código de todos modos. ¿** Tienes ** pruebas, ¿verdad? :) –

+2

No he encontrado nunca un "código inaccesible" de la resharper como incorrecto. Aunque a veces me ha tomado algunas reflexiones serias para descubrir por qué. No estoy diciendo que siempre debas confiar en resharper y borrar lo que sea que te diga que puedes borrar de manera segura, pero no llegues a la conclusión de que es incorrecto. un paso de depuración de este código le habría mostrado lo que estaba pasando. –

+0

Parece que el error aquí es al interpretar el código de error, parece que el error "Código extraño" hubiera sido más útil que "Código inalcanzable". – SwDevMan81

Respuesta

31

La variable narrativefound nunca será verdadera cuando el control llega a esa declaración:

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

creo ReSharper está tratando de decir que la condición narrativefound == false siempre será cierto.

+5

Exactamente: la * condición * siempre será cierta; 'narrativeFound' siempre será falso. –

+1

Buen ojo. Perdí eso en el primer pase yo mismo. –

+0

Interpreté mal lo que R # me decía. ¡Gracias por la gran respuesta! –

1

R # es correcto porque si activas el argumento narrativo, estarás saliendo del foreach justo después de configurarlo.

4

No necesita la variable narrativeFound en absoluto. En el ámbito donde lo establece verdadero, se rompe desde el foreach loop. Si no lo configura en verdadero, no se rompe y agrega el newNarrative al newNarrativeList.

Por lo tanto, esto podría ser reescrita como

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);     
    } 
} 
4

Es un error en el código.

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); 
      } 
    } 
} 
0

creo que le está diciendo que becuase SI narriativefound se establece en true, entonces el bucle se sale (break;). Entonces, si se evalúa if (narriativefound == false), siempre tendrá un valor de false.

Cuestiones relacionadas