Hay dos cuestiones a abordar aquí: legibilidad y comprensibilidad
La solución "legibilidad" es una cuestión de estilo y como tal es abierto a la interpretación . Mi preferencia es la siguiente:
if (var1 == true && // Explanation of the check
var2 == true && // Explanation of the check
var3 == true && // Explanation of the check
var4 == true && // Explanation of the check
var5 == true && // Explanation of the check
var6 == true) // Explanation of the check
{ }
o esto:
if (var1 && // Explanation of the check
var2 && // Explanation of the check
var3 && // Explanation of the check
var4 && // Explanation of the check
var5 && // Explanation of the check
var6) // Explanation of the check
{ }
Dicho esto, este tipo de cheque complejo puede ser muy difícil de analizar mentalmente mientras escanea el código (especialmente si usted no es el autor original) Considerar la creación de un método de ayuda para abstraer parte de la complejidad de distancia:
/// <Summary>
/// Tests whether all the conditions are appropriately met
/// </Summary>
private bool AreAllConditionsMet (
bool var1,
bool var2,
bool var3,
bool var4,
bool var5,
bool var6)
{
return (
var1 && // Explanation of the check
var2 && // Explanation of the check
var3 && // Explanation of the check
var4 && // Explanation of the check
var5 && // Explanation of the check
var6); // Explanation of the check
}
private void SomeMethod()
{
// Do some stuff (including declare the required variables)
if (AreAllConditionsMet (var1, var2, var3, var4, var5, var6))
{
// Do something
}
}
Ahora al escanear visualmente el método "SomeMethod", la complejidad real de la lógica prueba está oculto, pero el significado semántico se conserva para los seres humanos para entender en un alto nivel. Si el desarrollador realmente necesita comprender los detalles, se puede examinar el método AreAllConditionsMet.
Esto se conoce formalmente como el patrón de refactorización "Decompose Conditional", creo. ¡Herramientas como Resharper o Refactor Pro! puede hacer este tipo de refactorización fácil!
En todos los casos, la clave para tener un código legible y comprensible es usar nombres de variables realistas. Si bien entiendo que este es un ejemplo artificial, "var1", "var2", etc. son no nombres de variables aceptables. Deben tener un nombre que refleje la naturaleza subyacente de los datos que representan.
simple, fácil de hacer, y eficaz. –