Skip to content
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

Operator placement in long if statements #1149

Closed
tim-altitudegames opened this issue Jan 25, 2024 · 3 comments
Closed

Operator placement in long if statements #1149

tim-altitudegames opened this issue Jan 25, 2024 · 3 comments

Comments

@tim-altitudegames
Copy link

Using 0.27.1, I feel there could be a minor improvement with how long statements are separated with regards to the placement of the operators. Having all the variables vertically aligned with each other to me seems a little easier to read with the && placed at the end of the previous line rather than the start of the next one. This would be similar behaviour to what prettier does too.

Input:

var theNumberOne = 1;
var theNumberTwo = 2;
var theNumberThree = 3;
var theNumberFour = 4;
var theNumberFive = 5;
var theNumberSix = 6;

if (theNumberOne == 1 && theNumberTwo == 2 && theNumberThree == 3 && theNumberFour == 4 && theNumberFive == 5 && theNumberSix == 6)
{
    // They are who they say they are
}

Output:

var theNumberOne = 1;
var theNumberTwo = 2;
var theNumberThree = 3;
var theNumberFour = 4;
var theNumberFive = 5;
var theNumberSix = 6;

if (
    theNumberOne == 1
    && theNumberTwo == 2
    && theNumberThree == 3
    && theNumberFour == 4
    && theNumberFive == 5
    && theNumberSix == 6
)
{
    // They are who they say they are
}

Expected behavior:

var theNumberOne = 1;
var theNumberTwo = 2;
var theNumberThree = 3;
var theNumberFour = 4;
var theNumberFive = 5;
var theNumberSix = 6;

if (
    theNumberOne == 1 &&
    theNumberTwo == 2 &&
    theNumberThree == 3 &&
    theNumberFour == 4 &&
    theNumberFive == 5 &&
    theNumberSix == 6
)
{
    // They are who they say they are
}
@Rudomitori
Copy link
Contributor

I prefer how CSharpier formats it now. It makes easier to understand the logic of the condition when you have not trivial combination of && and ||

@belav
Copy link
Owner

belav commented Jan 25, 2024

Operators at the beginning of long statements is the widely accepted best practice. If I recall correctly from this super long prettier issue about it, someone just picked putting them at the end and they have been reluctant to finally move them to the beginning. prettier/prettier#3806

There is a lot of discussion in that issue as well about the pros of operators at the beginning.

@tim-altitudegames
Copy link
Author

Fair enough, reading through that issue I can see why, also I see you referenced this issue #442 as well in case anyone else sees this

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

No branches or pull requests

3 participants