mirror of
https://github.com/flutter/flutter.git
synced 2026-02-20 02:29:02 +08:00
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)
91 lines
2.7 KiB
Dart
91 lines
2.7 KiB
Dart
// Copyright 2014 The Flutter Authors. All rights reserved.
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
// found in the LICENSE file.
|
|
|
|
import 'dart:typed_data';
|
|
|
|
import 'package:flutter/services.dart';
|
|
import 'package:flutter_test/flutter_test.dart';
|
|
|
|
void checkEncoding<T>(MessageCodec<T> codec, T message, List<int> expectedBytes) {
|
|
final ByteData encoded = codec.encodeMessage(message)!;
|
|
expect(
|
|
encoded.buffer.asUint8List(0, encoded.lengthInBytes),
|
|
orderedEquals(expectedBytes),
|
|
);
|
|
}
|
|
|
|
void checkEncodeDecode<T>(MessageCodec<T> codec, T message) {
|
|
final ByteData? encoded = codec.encodeMessage(message);
|
|
final T? decoded = codec.decodeMessage(encoded);
|
|
if (message == null) {
|
|
expect(encoded, isNull);
|
|
expect(decoded, isNull);
|
|
} else {
|
|
expect(deepEquals(message, decoded), isTrue);
|
|
final ByteData? encodedAgain = codec.encodeMessage(decoded as T);
|
|
expect(
|
|
encodedAgain!.buffer.asUint8List(),
|
|
orderedEquals(encoded!.buffer.asUint8List()),
|
|
);
|
|
}
|
|
}
|
|
|
|
bool deepEquals(dynamic valueA, dynamic valueB) {
|
|
if (valueA is TypedData) {
|
|
return valueB is TypedData && deepEqualsTypedData(valueA, valueB);
|
|
}
|
|
if (valueA is List) {
|
|
return valueB is List && deepEqualsList(valueA, valueB);
|
|
}
|
|
if (valueA is Map) {
|
|
return valueB is Map && deepEqualsMap(valueA, valueB);
|
|
}
|
|
if (valueA is double && valueA.isNaN) {
|
|
return valueB is double && valueB.isNaN;
|
|
}
|
|
return valueA == valueB;
|
|
}
|
|
|
|
bool deepEqualsTypedData(TypedData valueA, TypedData valueB) {
|
|
if (valueA is ByteData) {
|
|
return valueB is ByteData && deepEqualsList(
|
|
valueA.buffer.asUint8List(),
|
|
valueB.buffer.asUint8List(),
|
|
);
|
|
}
|
|
|
|
return switch (valueA) {
|
|
Uint8List() => valueB is Uint8List && deepEqualsList(valueA, valueB),
|
|
Int32List() => valueB is Int32List && deepEqualsList(valueA, valueB),
|
|
Int64List() => valueB is Int64List && deepEqualsList(valueA, valueB),
|
|
Float32List() => valueB is Float32List && deepEqualsList(valueA, valueB),
|
|
Float64List() => valueB is Float64List && deepEqualsList(valueA, valueB),
|
|
_ => throw 'Unexpected typed data: $valueA',
|
|
};
|
|
}
|
|
|
|
bool deepEqualsList(List<dynamic> valueA, List<dynamic> valueB) {
|
|
if (valueA.length != valueB.length) {
|
|
return false;
|
|
}
|
|
for (int i = 0; i < valueA.length; i++) {
|
|
if (!deepEquals(valueA[i], valueB[i])) {
|
|
return false;
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
|
|
bool deepEqualsMap(Map<dynamic, dynamic> valueA, Map<dynamic, dynamic> valueB) {
|
|
if (valueA.length != valueB.length) {
|
|
return false;
|
|
}
|
|
for (final dynamic key in valueA.keys) {
|
|
if (!valueB.containsKey(key) || !deepEquals(valueA[key], valueB[key])) {
|
|
return false;
|
|
}
|
|
}
|
|
return true;
|
|
}
|