-
Notifications
You must be signed in to change notification settings - Fork 29.8k
pattern-matching refactor #154753
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
pattern-matching refactor #154753
Conversation
1f3053b to
7470d6f
Compare
|
I took a cursory glance (it's EOD Friday here so I'll have to look again next week) and in general this seems like an improvement, and in the couple of places where code owners might prefer the legacy style I suspect you could make 1-off adjustments. That being said, I'm having a hard time understanding the state of the proposal; the original design has a combination of positive and negative comments, and the only other (public?) comment I see from @goderbauer implies not a strong amount of support. If there were other outcomes or conversations you can share to explain this change, that would be great. |
|
Sure! I'd be happy to give an overview of what I've learned. Hixie engaged with the doc the most, and also was the most strongly opposed to pattern-matching out of the people who responded. His negative responses (e.g. here, here, here, and here) are summarized really well by a comment he made at the top: simplicity is more important than concision. There were other spots where he gave a more neutral response (here, here, here, and here) along the lines of "neither one is very readable." As far as his supportive feedback, one comment in particular stuck out:
Mylo Fawcett also engaged quite a bit and showed more support overall for implementing patterns. I was also interested to hear from Bob Nystrom that Dart pattern-matching has a lot in common with Swift syntax. It's definitely not cut-and-dry whether pattern-matching is overall "better" or "worse" than the alternative (there are some situations where whether the pattern is more readable depends on how long the variable names are). There's a lot of variety, both with regards to how patterns would apply to a particular code snippet, and also pertaining to the individual reactions and overall opinions that different people have. That being said, there was one huge takeaway that hadn't occurred to me until I started talking with people and gathering feedback: map patterns are far more readable than object patterns. if (someMap case {'key': final Object value}) {
// ...
}It's distinct, concise, and expressive: the By comparison, object patterns are often not as great, especially with the leading colon:
To decide what went into this PR, I basically just included all the map patterns, and then some other things that I thought looked nice.
Sounds like a great plan! |
7470d6f to
54ad329
Compare
54ad329 to
e2cbb9c
Compare
victorsanni
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.
I love how this reduces the if foo is null/T checks. Also love the switch statements, especially when they're aligned nicely. The if..case..when statements also make more sense to me now.
dev/bots/test/analyze-gen-defaults/dev/tools/gen_defaults/bin/template.dart
Outdated
Show resolved
Hide resolved
ebcf9c7 to
a66890e
Compare
Piinks
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.
So, some of the case statements gave me the heebie-jeebies initially but once I cam across something I knew really well, they made sense.
Regarding anonymous parameters, maybe we need to revisit that in the style guide, as there are a lot of them here. I wonder if they're already pervasive in the codebase and the style guide is just not reflecting reality...
I'll circulate this with the team once more so we can finally move this forward.
I always love your creative titles, but can you update it to reflect the change now? Thanks!
This pull request aims to improve code readability, based on feedback gathered in a recent design doc.
There are two factors that hugely impact how easy it is to understand a piece of code: verbosity and complexity.
Reducing verbosity is important, because boilerplate makes a project more difficult to navigate. It also has a tendency to make one's eyes gloss over, and subtle typos/bugs become more likely to slip through.
Reducing complexity makes the code more accessible to more people. This is especially important for open-source projects like Flutter, where the code is read by those who make contributions, as well as others who read through source code as they debug their own projects.
The following examples show how pattern-matching might affect these two factors:
Example 1 (GOOD)
[click to expand]Without using patterns, this might expand to
Had
ancestorbeen a non-local variable, it would need to be "converted" as well:Example 2 (BAD)
[click to expand]Assuming
widgetis a non-local variable, this would expand to:In both of the examples above, an
if-casestatement simultaneously verifies that an object meets the specified criteria and performs a variable assignment accordingly.But there are some differences: Example 2 uses a more deeply-nested pattern than Example 1 but makes fewer useful checks.
Example 1:
ancestoris anInheritedElementwidgetis anInheritedThemeExample 2:
widgetis aPreferredSizeWidget(every
PreferredSizeWidgethas asizefield, and everySizehas aheightfield)I feel hesitant to try presenting a set of cut-and-dry rules as to which scenarios should/shouldn't use pattern-matching, since there are an abundance of different types of patterns, and an abundance of different places where they might be used.
But hopefully the conversations we've had recently will help us converge toward a common intuition of how pattern-matching can best be utilized for improved readability.