Which Coding Style You Use for Ternary Operator

Which coding style you use for ternary operator?

The ternary operator is generally to be avoided, but this form can be quite readable:

  result = (foo == bar)  ? result1 :
(foo == baz) ? result2 :
(foo == qux) ? result3 :
(foo == quux) ? result4 :
fail_result;

This way, the condition and the result are kept together on the same line, and it's fairly easy to skim down and understand what's going on.

Conditional (Ternary) Operator Code Style

Oracles Code Conventions states the following

return (condition ? x : y);

.. and further

if (a == b && c == d)     // AVOID!
if ((a == b) && (c == d)) // RIGHT

...which is freely translated to

return (someBoolean ? x : y);
return ((x > y) ? x : y);

.. although on a personal note I wouldn't mind relaxing a parenthesis or two.
In the end it's still a subjective matter. If you feel adding/removing a parenthesis offers better readability, then by all means feel free to do so.

Ternary operator should not be used on a single line in Node.js. Why?

With all coding standards, they are generally for readability and maintainability. My guess is the author finds it more readable on separate lines. The compiler / interpreter for your language will handle it all the same. As long as you / your project have a set standard and stick to it, you'll be fine. I recommend that the standards be worked on or at least reviewed by everyone on the project before casting them in stone. I think that if you're breaking it up on separate lines like that, you may as well define an if/else conditional block and use that.

Be wary of coding standards rules that do not have a justification.

Personally, I do not like the ternary operator as it feels unnatural to me and I always have to read the line a few times to understand what it's doing. I find separate if/else blocks easier for me to read. Personal preference of course.

Ternary (Conditional) Operator and Style

The conditional operator should normally be used in expressions - value producing expressions - and is best not used as a replacement for the 'if/then/else' statement. Used occasionally, there'd be no particular problem; used systematically, I think it would be hiding the intent of the code from the readers.

What is recommended coding style for conditional operator in Python?

The layout of your code does indeed make it pretty unclear what's going on.

At a minimum, I would be inclined to line break after the binary operators and and or, per the style guide, rather than in the middle of a single condition.

I would also try to keep a ternary on a single line, where possible; in this case that ends up being quite long, though, so probably not an option. I think the boolean operators in the ternary make more sense at the start of their line, although I can't find a reference for this (beyond "Martijn likes it too").

One more readable example:

while (id1 != "cancel" and 
((id1 not in _idName.id.values) if isinstance(_idName, _pd.DataFrame)
else (_idName(id1) is None))):

or perhaps:

while (id1 != "cancel" and 
((id1 not in _idName.id.values)
if isinstance(_idName,_pd.DataFrame)
else (_idName(id1) is None)):

How to indent a long ternary condition in Php?

One option is to put each part on new line so it is clearly visible what you do in the "if" part and "else" part:

$variable = (condition) 
? function()
: $variable;

Python Ternary Style: Is this good or bad style?

That style would not be appropriate at my workplace. Consider this snippet from PEP8:

Compound statements (multiple statements on the same line) are
generally discouraged.

Yes:

if foo == 'blah':
do_blah_thing()
do_one()
do_two()
do_three()

Rather not:

if foo == 'blah': do_blah_thing()
do_one(); do_two(); do_three()

So, in your case:

Bad:

for state in states: score += penalty if state == bad else bonus

Better:

for state in states:
score += penalty if state == bad else bonus

Best:

for state in states:
if state == bad:
score += penalty
else:
score += bonus

As matter of design style, not necessarily coding style, I might rather see state-specific score increments stored in a mapping object, like so:

for state in states:
score += scores_per_state[state]


Related Topics



Leave a reply



Submit