Skip to content

Commit 536fa0e

Browse files
authored
Reorder "For loops" examples of the Language Tour (#3102)
The "For loops" section of the Language Tour described `Iterable.forEach` before `for-in` and suggested `Iterable.forEach` as a "good option". This currently is not the recommendation from Effective Dart, which discourages using `Iterable.forEach` with anonymous functions. * Reorder the examples in the "For loops" section to put greater emphasis on `for-in`. * Describe `Iterable.forEach` as "an option" rather than as a "good option". * Adjust the code snippets so that the `Iterable.forEach` example follows the Effective Dart guideline by using a tear-off instead of an anonymous function. * Update with review feedback from parlough
1 parent 1f9934c commit 536fa0e

File tree

3 files changed

+19
-20
lines changed

3 files changed

+19
-20
lines changed

null_safety_examples/misc/lib/language_tour/control_flow.dart

+5-3
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,11 @@ void miscDeclAnalyzedButNotTested() {
2626
}
2727

2828
{
29-
// #docregion forEach
30-
candidates.forEach((candidate) => candidate.interview());
31-
// #enddocregion forEach
29+
// #docregion collection
30+
for (var candidate in candidates) {
31+
candidate.interview();
32+
}
33+
// #enddocregion collection
3234
}
3335

3436
{

null_safety_examples/misc/test/language_tour/control_flow_test.dart

+4-6
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,12 @@ void main() {
2626
expect(_test, m.prints(['0', '1']));
2727
});
2828

29-
test('collection', () {
29+
test('forEach', () {
3030
_test() {
31-
// #docregion collection
31+
// #docregion forEach
3232
var collection = [1, 2, 3];
33-
for (var x in collection) {
34-
print(x); // 1 2 3
35-
}
36-
// #enddocregion collection
33+
collection.forEach(print); // 1 2 3
34+
// #enddocregion forEach
3735
}
3836

3937
expect(_test, m.prints([1, 2, 3]));

src/_guides/language/language-tour.md

+10-11
Original file line numberDiff line numberDiff line change
@@ -2237,24 +2237,23 @@ callbacks.forEach((c) => c());
22372237
The output is `0` and then `1`, as expected. In contrast, the example
22382238
would print `2` and then `2` in JavaScript.
22392239

2240-
If the object that you are iterating over is an Iterable, you can use the
2241-
[forEach()][] method. Using `forEach()` is a good option if you dont need to
2242-
know the current iteration counter:
2240+
If the object that you are iterating over is an Iterable (such as List or Set)
2241+
and if you don't need to know the current iteration counter,
2242+
you can use the `for-in` form of [iteration](/guides/libraries/library-tour#iteration):
22432243

2244-
<?code-excerpt "../null_safety_examples/misc/lib/language_tour/control_flow.dart (forEach)"?>
2244+
<?code-excerpt "../null_safety_examples/misc/lib/language_tour/control_flow.dart (collection)"?>
22452245
```dart
2246-
candidates.forEach((candidate) => candidate.interview());
2246+
for (var candidate in candidates) {
2247+
candidate.interview();
2248+
}
22472249
```
22482250

2249-
Iterable classes such as List and Set also support the `for-in` form of
2250-
[iteration](/guides/libraries/library-tour#iteration):
2251+
Iterable classes also have a [forEach()][] method as another option:
22512252

2252-
<?code-excerpt "../null_safety_examples/misc/test/language_tour/control_flow_test.dart (collection)"?>
2253+
<?code-excerpt "../null_safety_examples/misc/test/language_tour/control_flow_test.dart (forEach)"?>
22532254
```dart
22542255
var collection = [1, 2, 3];
2255-
for (var x in collection) {
2256-
print(x); // 1 2 3
2257-
}
2256+
collection.forEach(print); // 1 2 3
22582257
```
22592258

22602259

0 commit comments

Comments
 (0)