Skip to content

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

Merged
merged 3 commits into from
Oct 15, 2024
Merged

Conversation

JesusValeraDev
Copy link

@JesusValeraDev JesusValeraDev commented Aug 12, 2024

📚 Description

Some people write ternary operators as follows:

$foo = true ?
    'foo' :
    'bar';

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:

$foo = true
    ? 'foo'
    : 'bar';

@Crell
Copy link
Collaborator

Crell commented Aug 12, 2024

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?

@JesusValeraDev
Copy link
Author

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 😄

@Crell
Copy link
Collaborator

Crell commented Aug 13, 2024

Let's make that a separate PR so it can have its own discussion.

@Jean85
Copy link
Member

Jean85 commented Aug 20, 2024

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?

@JesusValeraDev
Copy link
Author

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?
The proposal is to have the operators at the beginning 😄

@rhertogh
Copy link

@Jean85 @JesusValera I initially had the same misunderstanding based on the opening post (before looking at the file changes.)
Perhaps it would be good to add the proposed format to the opening post?

@Jean85
Copy link
Member

Jean85 commented Aug 20, 2024

👍 on what @rhertogh said 😄 I got confused too!

@JesusValeraDev
Copy link
Author

I just pushed a new commit.

Initially, I was unsure if the content of this PR belonged to the 2.3 Lines, the 6.3. Ternary operators or a new 6.4. Operator's placement section.
I decided to create the new section as the proposal involves not only the lines related section but also the operator behaviours when multi-lines, because of that, probably a new section is the best choice.

Let me know what you think, otherwise, I could change the code again :octocat:

@kenguest
Copy link
Contributor

kenguest commented Oct 9, 2024

I have seen instances of

$foo = $bar ? $qux
           : $quux

and

return $foo 
       ? $qux
       : $quux

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 :-)

@Crell Crell merged commit f8ced3a into php-fig:master Oct 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants