程序员 A 只是喜欢一些三元运算符,尤其是嵌套运算符。更好的是,他声称它们使代码更易于阅读和维护,因为它们可以放在更少的行上。程序员 B 认为,当三元组嵌套时,可读性会丢失,进而代码变得更难维护。
查看下面的块:
private int MyFunction(bool condA, bool condB)
{
// Programmer A says:
// Nested ternary --> This is easier to read and maintain because it is on one line. Ternaries can be nested 4 or 5 deep and it is no problem.
return condA ? condB ? 20 : -10 : -20;
// Programmer B says:
// Unwrapped --> This is more readable as it better describes the step by step evaluation of the conditions and their results.
if (!condA) { return -20; }
if (!condB) { return -10; }
return 20;
}
其他人如何看待这两种思想流派?最好的方法是什么?
编辑:有更好的方法吗?