Skip to content

Compound expressions in return statements format poorly (WONTFIX) #525

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

Closed
Hixie opened this issue Aug 24, 2016 · 5 comments
Closed

Compound expressions in return statements format poorly (WONTFIX) #525

Hixie opened this issue Aug 24, 2016 · 5 comments

Comments

@Hixie
Copy link

Hixie commented Aug 24, 2016

This flutter code:

    return config.direction == DismissDirection.horizontal
        || config.direction == DismissDirection.endToStart
        || config.direction == DismissDirection.startToEnd;

...becomes less readable when processed by dartfmt:

    return config.direction == DismissDirection.horizontal ||
        config.direction == DismissDirection.endToStart ||
        config.direction == DismissDirection.startToEnd;

It's not as readable as the original but I would also have accepted:

    return config.direction == DismissDirection.horizontal ||
           config.direction == DismissDirection.endToStart ||
           config.direction == DismissDirection.startToEnd;
@Hixie
Copy link
Author

Hixie commented Aug 24, 2016

Here's another example:
Input (and ideal output, IMHO):

    return a.storage[0] == 1.0 // col 1
        && a.storage[1] == 0.0
        && a.storage[2] == 0.0
        && a.storage[3] == 0.0
        && a.storage[4] == 0.0 // col 2
        && a.storage[5] == 1.0
        && a.storage[6] == 0.0
        && a.storage[7] == 0.0
        && a.storage[8] == 0.0 // col 3
        && a.storage[9] == 0.0
        && a.storage[10] == 1.0
        && a.storage[11] == 0.0
        && a.storage[12] == 0.0 // col 4
        && a.storage[13] == 0.0
        && a.storage[14] == 0.0
        && a.storage[15] == 1.0;

Output:

    return a.storage[0] == 1.0 // col 1
        &&
        a.storage[1] == 0.0 &&
        a.storage[2] == 0.0 &&
        a.storage[3] == 0.0 &&
        a.storage[4] == 0.0 // col 2
        &&
        a.storage[5] == 1.0 &&
        a.storage[6] == 0.0 &&
        a.storage[7] == 0.0 &&
        a.storage[8] == 0.0 // col 3
        &&
        a.storage[9] == 0.0 &&
        a.storage[10] == 1.0 &&
        a.storage[11] == 0.0 &&
        a.storage[12] == 0.0 // col 4
        &&
        a.storage[13] == 0.0 &&
        a.storage[14] == 0.0 &&
        a.storage[15] == 1.0;

@Hixie
Copy link
Author

Hixie commented Aug 24, 2016

Another example, this from the Flutter rendering library:

Input:

    return (minWidth <= size.width) && (size.width <= maxWidth) &&
           (minHeight <= size.height) && (size.height <= maxHeight);

Output:

    return (minWidth <= size.width) &&
        (size.width <= maxWidth) &&
        (minHeight <= size.height) &&
        (size.height <= maxHeight);

The original code here is carefully written so that it forms a two-by-two matrix with the vertical columns being minimum and maximum, and the horizontal rows being width and height, with each column having the form "a < b < c". However, the formatter removes all of this and the code ends up much harder to understand at a glance.

@Hixie
Copy link
Author

Hixie commented Aug 24, 2016

To clarify: I care a little about the first here, the second I think we could work around my moving the comments. The third is the most important IMHO. @sethladd suggests that for the third we could hint to the formatter that things should be kept on one line by putting parens around the first two and the last two expressions. That does seem to work, the output is then:

  return ((minWidth <= size.width) && (size.width <= maxWidth)) &&
      ((minHeight <= size.height) && (size.height <= maxHeight));

It would definitely be better if the second line of the return expression was aligned with the first though.

@Hixie
Copy link
Author

Hixie commented Aug 24, 2016

I've filed the indent thing separately in #532. This means that this issue is really only about the first two examples, which basically boil down to noticing that you can put the && or || operators in a chain on the left hand side. @abarth likes that style. I think it's cute but could live without it if it's too hard to implement.

@munificent
Copy link
Member

It's not hard to implement, but it goes against the style guide and the millions of lines of existing Dart code that are already formatted with binary operators on the previous line.

We did discuss changing this a while back, and the consensus on the team at the time was that keeping the operators on the right was better.

@Hixie Hixie changed the title Compound expressions in return statements format poorly Compound expressions in return statements format poorly (WONTFIX) Sep 1, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants