pattern-matching refactor (#154753)

This pull request aims to improve code readability, based on feedback gathered in a recent design doc.

<br>

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.

<hr>

<br>

The following examples show how pattern-matching might affect these two factors:

<details> <summary><h3>Example 1 (GOOD)</h3> [click to expand]</summary>

```dart
if (ancestor case InheritedElement(:final InheritedTheme widget)) {
  themes.add(widget);
}
```

Without using patterns, this might expand to

```dart
if (ancestor is InheritedElement) {
  final InheritedWidget widget = ancestor.widget;
  if (widget is InheritedTheme) {
    themes.add(widget);
  }
}
```

Had `ancestor` been a non-local variable, it would need to be "converted" as well:

```dart
final Element ancestor = this.ancestor;
if (ancestor is InheritedElement) {
  final InheritedWidget inheritedWidget = ancestor.widget;
  if (widget is InheritedTheme) {
    themes.add(theme);
  }
}
```

</details>

<details> <summary><h3>Example 2 (BAD) </h3> [click to expand]</summary>

```dart
if (widget case PreferredSizeWidget(preferredSize: Size(:final double height))) {
  return height;
}
```

Assuming `widget` is a non-local variable, this would expand to:

```dart
final Widget widget = this.widget;
if (widget is PreferredSizeWidget) {
  return widget.preferredSize.height;
}
```

<br>

</details>

In both of the examples above, an `if-case` statement 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:**
- checks that `ancestor` is an `InheritedElement`
- checks that the inherited element's `widget` is an `InheritedTheme`

**Example 2:**
- checks that `widget` is a `PreferredSizeWidget`
(every `PreferredSizeWidget` has a `size` field, and every `Size` has a `height` field)

<br>

<hr>

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.

<br><br>

- resolves https://github.com/flutter/flutter/issues/152313
- Design Doc: [flutter.dev/go/dart-patterns](https://flutter.dev/go/dart-patterns)
This commit is contained in:
Nate Wilson
2024-10-03 12:21:04 -06:00
committed by GitHub
parent 91eb8d226d
commit 5ecf10052f
46 changed files with 378 additions and 671 deletions

View File

@@ -77,13 +77,11 @@ class _Visitor extends RecursiveAstVisitor<void> {
@override
void visitMethodInvocation(MethodInvocation node) {
if (node.methodName.name != 'onError' && node.methodName.name != 'catchError') {
return;
if (node case MethodInvocation(
methodName: SimpleIdentifier(name: 'onError' || 'catchError'),
realTarget: Expression(staticType: DartType(isDartAsyncFuture: true)),
)) {
_offendingNodes.add(node);
}
final DartType? targetType = node.realTarget?.staticType;
if (targetType == null || !targetType.isDartAsyncFuture) {
return;
}
_offendingNodes.add(node);
}
}

View File

@@ -169,16 +169,12 @@ abstract class TokenTemplate {
}
String? _numToString(Object? value, [int? digits]) {
if (value == null) {
return null;
}
if (value is num) {
if (value == double.infinity) {
return 'double.infinity';
}
return digits == null ? value.toString() : value.toStringAsFixed(digits);
}
return getToken(value as String).toString();
return switch (value) {
null => null,
double.infinity => 'double.infinity',
num() => digits == null ? value.toString() : value.toStringAsFixed(digits),
_ => getToken(value as String).toString(),
};
}
/// Generate an elevation value for the given component token.

View File

@@ -3,13 +3,11 @@
// found in the LICENSE file.
void main(List<String> args) {
String type = '';
if (args[0] == '--material') {
type = 'material';
}
if (args[0] == '--cupertino') {
type = 'cupertino';
}
final String type = switch (args.first) {
'--material' => 'material',
'--cupertino' => 'cupertino',
_ => '',
};
print('''
// Copyright 2014 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be

View File

@@ -85,14 +85,10 @@ Future<void> testWithNewIOSSimulator(
final String? iosKey = decodeResult.keys
.where((String key) => key.contains('iphoneos'))
.firstOrNull;
final Object? iosDetails = decodeResult[iosKey];
String? runtimeBuildForSelectedXcode;
if (iosDetails != null && iosDetails is Map<String, Object?>) {
final Object? preferredBuild = iosDetails['preferredBuild'];
if (preferredBuild is String) {
runtimeBuildForSelectedXcode = preferredBuild;
}
}
final String? runtimeBuildForSelectedXcode = switch (decodeResult[iosKey]) {
{'preferredBuild': final String build} => build,
_ => null,
};
String? iOSSimRuntime;

View File

@@ -12,14 +12,12 @@ import 'package:metrics_center/metrics_center.dart';
///
/// It supports both token and credential authentications.
Future<FlutterDestination> connectFlutterDestination() async {
const String kTokenPath = 'TOKEN_PATH';
const String kGcpProject = 'GCP_PROJECT';
final Map<String, String> env = Platform.environment;
final bool isTesting = env['IS_TESTING'] == 'true';
if (env.containsKey(kTokenPath) && env.containsKey(kGcpProject)) {
if (env case {'TOKEN_PATH': final String path, 'GCP_PROJECT': final String project}) {
return FlutterDestination.makeFromAccessToken(
File(env[kTokenPath]!).readAsStringSync(),
env[kGcpProject]!,
File(path).readAsStringSync(),
project,
isTesting: isTesting,
);
}

View File

@@ -70,16 +70,12 @@ class AppStateModel extends Model {
// Removes an item from the cart.
void removeItemFromCart(int productId) {
final int? value = _productsInCart[productId];
if (value != null) {
if (_productsInCart[productId] == 1) {
switch (_productsInCart[productId]) {
case 1:
_productsInCart.remove(productId);
} else {
case final int value:
_productsInCart[productId] = value - 1;
}
}
notifyListeners();
}

View File

@@ -92,15 +92,10 @@ Future<void> main() async {
}
final Finder backFinder = find.byElementPredicate(
(Element element) {
final Widget widget = element.widget;
if (widget is Tooltip) {
return widget.message == 'Back';
}
if (widget is CupertinoNavigationBarBackButton) {
return true;
}
return false;
(Element element) => switch (element.widget) {
Tooltip(message: 'Back') => true,
CupertinoNavigationBarBackButton() => true,
_ => false,
},
description: 'Material or Cupertino back button',
);

View File

@@ -574,16 +574,14 @@ class _MobileNavState extends State<_MobileNav> with TickerProviderStateMixin {
}
bool _handleScrollNotification(ScrollNotification notification) {
if (notification.depth == 0) {
if (notification is UserScrollNotification) {
switch (notification.direction) {
case ScrollDirection.forward:
_bottomAppBarController.forward();
case ScrollDirection.reverse:
_bottomAppBarController.reverse();
case ScrollDirection.idle:
break;
}
if (notification case UserScrollNotification(depth: 0)) {
switch (notification.direction) {
case ScrollDirection.forward:
_bottomAppBarController.forward();
case ScrollDirection.reverse:
_bottomAppBarController.reverse();
case ScrollDirection.idle:
break;
}
}
return false;

View File

@@ -108,20 +108,15 @@ class _FocusDemoState extends State<FocusDemo> {
return KeyEventResult.handled;
}
}
if (event.logicalKey == LogicalKeyboardKey.arrowLeft) {
node.focusInDirection(TraversalDirection.left);
return KeyEventResult.handled;
}
if (event.logicalKey == LogicalKeyboardKey.arrowRight) {
node.focusInDirection(TraversalDirection.right);
return KeyEventResult.handled;
}
if (event.logicalKey == LogicalKeyboardKey.arrowUp) {
node.focusInDirection(TraversalDirection.up);
return KeyEventResult.handled;
}
if (event.logicalKey == LogicalKeyboardKey.arrowDown) {
node.focusInDirection(TraversalDirection.down);
final TraversalDirection? direction = switch (event.logicalKey) {
LogicalKeyboardKey.arrowLeft => TraversalDirection.left,
LogicalKeyboardKey.arrowRight => TraversalDirection.right,
LogicalKeyboardKey.arrowUp => TraversalDirection.up,
LogicalKeyboardKey.arrowDown => TraversalDirection.down,
_ => null,
};
if (direction != null) {
node.focusInDirection(direction);
return KeyEventResult.handled;
}
}

View File

@@ -57,13 +57,12 @@ String getChannelName({
Platform platform = const LocalPlatform(),
ProcessManager processManager = const LocalProcessManager(),
}) {
final String? envReleaseChannel = platform.environment['LUCI_BRANCH']?.trim();
if (<String>['master', 'stable', 'main'].contains(envReleaseChannel)) {
switch (platform.environment['LUCI_BRANCH']?.trim()) {
// Backward compatibility: Still support running on "master", but pretend it is "main".
if (envReleaseChannel == 'master') {
case 'master' || 'main':
return 'main';
}
return envReleaseChannel!;
case 'stable':
return 'stable';
}
final RegExp gitBranchRegexp = RegExp(r'^## (?<branch>.*)');