プログラマー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;
}
他の誰もが2つの考え方についてどう思いますか?取るべき最善のアプローチは何ですか?
編集:もっと良い方法はありますか?