Ternary Operators & Maintenance [closed]

元气小坏坏 提交于 2019-12-08 13:28:58

问题


Programmer A just loves some ternary operators, especially nested ones. Better yet he claims that they make the code easier to read and maintain because they can be fit on fewer lines. Programmer B argues that readability is lost when ternaries are nested, and in turn the code becomes harder to maintain.

Check out the block below:

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

What does everyone else think about the two schools of thought? What is the best approach to take?

EDIT: Is there a better way?


回答1:


I think the best solution is to keep the ternary operator but add parenthesis to it

return condA ? ( condB ? 20 : -10 ) : -20;

otherwise it might get confused easily.

Alternatively a hybrid approach might be best, where the ternary operators only occur on the last level of nesting.

if(condA)
{
    return condB ? 20 : -10;
} else {
    return -20;
}



回答2:


Clearly, Programmer A confuses "short" code with "maintainable and readable" code.




回答3:


I'll choose programmer B any day. Clever code just for the sake of being clever code is usually hard to understand and mantain, which makes up more than 70% of the time you spend looking at a screen at work.

Programmer's B way is straightforward and very hard to get wrong.




回答4:


Firstly, it's clearly subjective - and I'm tempted to vote to close the question on those grounds anyway. However, I think there can still be some productive points made which are less subjective.

I would write it differently, but still use the conditional operator. I'd use this:

return !condA ? -20
     : !condB ? -10
     : 20;

In other words, make the "simple" case the second operand in each case - so that we can see on each line, "If this condition holds, the return value will be that." That way it looks a bit more like the second code, but it's a bit terser. (And I don't tend to like braces on the same line like that - when you expand those if blocks out, it would be quite longwinded.)




回答5:


In my opinion, the number of lines is not an indicator of readability. You could put a lot of crazy stuff into one line, but that doesn't mean it's obvious for everyone what this line does.

I try to avoid ternary operators altogether (save for really trivial cases), since the expense to parse those line as a programmer is significantly higher than the space savings.

This is especially true for nested statements such as your example.

Example: For me, a trivial case would be something like

_foo = (bar != null) ? bar : String.Empty;



回答6:


I tend to agree that if you can fit code onto a single line without losing legibility its good practice.

I like ternary operators because they do tend to make code a bit shorter but they do often take a second longer to understand.

Nested ternary operators would never have a place in any project I supervise. Code must prioritise legibility and nested ternary operations are a step too far in my opinion.




回答7:


This is simple. Use parentheses.



来源:https://stackoverflow.com/questions/6153886/ternary-operators-maintenance

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!