-
Notifications
You must be signed in to change notification settings - Fork 27
Add ternary operator on multi-line #93
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
We probably need some more explanatory text on there, but as a fan of multi-line ternaries, I support this addition. Anyone else from the WG want to weigh in? |
I was thinking, maybe it makes sense to also add this rule for the Elvis/null coalescing operators? // Not this
$foo = 'foo' ??
'bar';
$foo = 'foo' ?:
'bar';
// But this
$foo = 'foo'
?? 'bar';
$foo = 'foo'
?: 'bar'; I could add the modifications in this (or another PR), let me know what you think and if you have another suggestion 😄 |
Let's make that a separate PR so it can have its own discussion. |
Can I say that I totally prefer the opposite style, with the operators at the start of the line? $foo = $bar
? 'bar'
: 'foo'; Wouldn't it be more consistent with how binary operators are considered? |
Sorry, I do not understand your statement - do you mean you prefer them at the beginning or the end? |
@Jean85 @JesusValera I initially had the same misunderstanding based on the opening post (before looking at the file changes.) |
👍 on what @rhertogh said 😄 I got confused too! |
I just pushed a new commit. Initially, I was unsure if the content of this PR belonged to the Let me know what you think, otherwise, I could change the code again |
I have seen instances of
and
I prefer the bottom one as that's more consistent, and more true to "multi-line" formatting - operators at the start of the lines is preferable :-) |
📚 Description
Some people write ternary operators as follows:
In the previous snippet is kind of easy, but when instead of
'foo'
, it is a long line calling method, etc, it becomes complex.My proposal is to add the following rule into the PER file to avoid people doing the previous snippet, making the code easier to track when ternary operators are used to improve the legibility; eg: