-
Notifications
You must be signed in to change notification settings - Fork 29.7k
Style Guide updates #152525
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
Style Guide updates #152525
Conversation
| if (_theProperty == value) | ||
| if (_theProperty == value) { | ||
| return; | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Now that we've enabled a linter rule to enforce if-statement curly braces, it makes sense for the examples here to reflect that.
| if (owner.debugDoingLayout) | ||
| return (RenderObject.debugActiveLayout == parent) && | ||
| parent.debugDoingThisLayout; | ||
| if (owner.debugDoingPaint) | ||
| return ((RenderObject.debugActivePaint == parent) && | ||
| parent.debugDoingThisPaint) || | ||
| ((RenderObject.debugActivePaint == this) && debugDoingThisPaint); | ||
| if (owner.debugDoingLayout) { | ||
| return (RenderObject.debugActiveLayout == parent) && parent.debugDoingThisLayout; | ||
| } | ||
| if (owner.debugDoingPaint) { | ||
| return ((RenderObject.debugActivePaint == parent) && parent.debugDoingThisPaint) | ||
| || ((RenderObject.debugActivePaint == this) && debugDoingThisPaint); | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This change is based on Align expressions; it also adds if-statement curly braces.
| However, where possible avoid global constants. Rather than `kDefaultButtonColor`, consider `Button.defaultColor`. If necessary, consider creating a class with a private constructor to hold relevant constants. | ||
| However, where possible avoid global constants. Rather than `kDefaultButtonColor`, consider `Button.defaultColor`. If necessary, consider creating an `abstract final class` to hold relevant constants. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| ```dart | ||
| setState(() { /* The animation ticked. We use the animation's value in the build method. */ }); | ||
| setState(() { | ||
| // The animation ticked. We use the animation's value in the build method. | ||
| }); | ||
| ``` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This change is based on the 80 characters guideline, along with the Dart documentation guide linked in the Documentation section:
DON'T use block comments for documentation
void greet(String name) { // Assume we have a valid name. print('Hi, $name!'); }void greet(String name) { /* Assume we have a valid name. */ print('Hi, $name!'); }
| // one-line constructor example | ||
| abstract class Foo extends StatelessWidget { | ||
| Foo(this.bar, { Key key, this.child }) : super(key: key); | ||
| final int bar; | ||
| final Widget child; | ||
| class ConstantTween<T> extends Tween<T> { | ||
| ConstantTween(T value) : super(begin: value, end: value); | ||
| // ... | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The old example would have triggered the use_super_parameters rule (I'm sure it was written before that feature existed).
| ### Prefer single quotes for strings | ||
|
|
||
| Use double quotes for nested strings or (optionally) for strings that contain single quotes. | ||
| For all other strings, use single quotes. | ||
|
|
||
| Example: | ||
|
|
||
| ```dart | ||
| print('Hello ${name.split(" ")[0]}'); | ||
| ``` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This section was likely written before the prefer_single_quotes and avoid_escaping_inner_quotes rules were introduced.
Since violations of this guideline result in analysis check failures, there isn't any additional utility that we gain from keeping this section. Removing it decreases the total length of this markdown file, making it more likely that the reader will pay attention to other sections.
| return <PopupMenuItem<String>>[ | ||
| PopupMenuItem<String>( | ||
| value: 'Friends', | ||
| child: MenuItemWithIcon(Icons.people, 'Friends', '5 new') | ||
| child: MenuItemWithIcon(Icons.people, 'Friends', '5 new'), | ||
| ), | ||
| PopupMenuItem<String>( | ||
| value: 'Events', | ||
| child: MenuItemWithIcon(Icons.event, 'Events', '12 upcoming') | ||
| child: MenuItemWithIcon(Icons.event, 'Events', '12 upcoming'), | ||
| ), | ||
| ]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated based on the Use a trailing comma guideline.
| ### Separate the 'if' expression from its statement | ||
|
|
||
| (This is enforced by the `always_put_control_body_on_new_line` and `curly_braces_in_flow_control_structures` lints.) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removing this section, for the same reason as 'Prefer single quotes' above.
180f5d3 to
e203e1a
Compare
| foo(bar, | ||
| baz); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the only change I'm on the fence about.
Personally, I think the other two examples are much easier to visually parse than this one.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why are you on the fence with this one ? This would only make sense if it breaks the max char per line rule, wouldn't it ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If foo() takes up multiple lines due to a callback/switch expression/list or map literal, then I think it's all right to format it this way.
foo(bar, () {
baz.quux();
return baz.toInt();
});
foo(bar, switch (baz) {
1 => 'a',
2 => 'b`,
_ => `c`,
});
foo(bar, [
'quux',
'qaax',
'qeex',
]);But in any other situation where the foo() arguments take up more than 1 line, I think it's much more readable with a trailing comma.
foo(
bar,
baz,
);There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it'd be nice to get another perspective on this, and then possibly add the above examples to this section.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree with removing this example and would be fine with adding the other examples you're mentioning in the comment above.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Awesome!
Examples have been added in the most recent commit. I'll plan on adding autosubmit tomorrow, to leave a window for any additional feedback 👍
goderbauer
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
| foo(bar, | ||
| baz); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree with removing this example and would be fine with adding the other examples you're mentioning in the comment above.
| foo(bar, | ||
| baz); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Awesome!
Examples have been added in the most recent commit. I'll plan on adding autosubmit tomorrow, to leave a window for any additional feedback 👍
| closing `]`, `}`, or `)` bracket will line up with the argument name, for | ||
| named arguments, or the `(` of the argument list, for positional arguments. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was confused by "argument name" and "argument list" here, since the code has a nested argument structure.
| you can instead use the `=>` form.\ | ||
| When doing this, the closing `]`, `}`, or `)` bracket will have the same | ||
| indentation as the line where the callback starts. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I picked up the \ from the resources linked in the semantic line breaks documentation :)
0f4bda6 to
29f409c
Compare
goderbauer
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
flutter/flutter@bced008...a0c0453 2024-08-16 [email protected] Style Guide updates (flutter/flutter#152525) 2024-08-16 [email protected] Roll Packages from 86d15a6 to 2c37fb0 (5 revisions) (flutter/flutter#153571) 2024-08-16 [email protected] Move Android API level 35 emulator tests to staging (flutter/flutter#153568) 2024-08-16 [email protected] Roll Flutter Engine from a8fefc81188e to d5bf3afc601f (2 revisions) (flutter/flutter#153565) 2024-08-16 [email protected] [Swift Package Manager] Test removing the last Flutter plugin (flutter/flutter#153519) If this roll has caused a breakage, revert this CL and stop the roller using the controls here: https://autoroll.skia.org/r/flutter-packages Please CC [email protected],[email protected],[email protected] on the revert to ensure that a human is aware of the problem. To file a bug in Packages: https://github.com/flutter/flutter/issues/new/choose To report a problem with the AutoRoller itself, please file a bug: https://issues.skia.org/issues/new?component=1389291&template=1850622 Documentation for the AutoRoller is here: https://skia.googlesource.com/buildbot/+doc/main/autoroll/README.md
This pull request updates the style guide for improved internal consistency and to match current linter rules. I'll make comments for each change here!
This pull request updates the style guide for improved internal consistency and to match current linter rules. I'll make comments for each change here!
This pull request contains various updates for the the style guide:
=>rule to include multi-line getters that return collection literals or switch expressions. (This is based on a recommendation from Bernardo Ferrari and Michael Goderbauer in a previous comment thread.)