Skip to content
This repository was archived by the owner on Jul 16, 2023. It is now read-only.
This repository was archived by the owner on Jul 16, 2023. It is now read-only.

[New rule] Avoid forming and after that returning lists in loops #443

Open
@PlugFox

Description

@PlugFox

Avoid forming and after that returning lists in loops.
Because double memory consumption with more reduant code.

With iterable result you can transform it to Stream
For example, to avoid affecting the interface and event loops: https://dartpad.dev/6fad9242381bf89af4c1f6110c429e1c?null_safety=true

With iterable result you can much more effective with .take(n).toList() or .first or .isEmpty or .map<T>(...).toList() afterwards.


This is a very critical moment for collections displayed in lists, code generation, static analysis, parsing, reading files, converting collections.

Codegeneration, parsing, transforming must operate with Iterable not List, it's faster, lazy, less RAM consumption.


[X] Suggests an alternate way of doing something (suggestion)

Example:

BAD:

List<int> bad() {
  final list = <int>[];
  for (var i = 0; i < 1000; i++) {
    list.add(i);
  }
  return list;
}

GOOD:

Iterable<int> good() sync* {
  for (var i = 0; i < 1000; i++) {
    yield i;
  }
}

GOOD:

Iterable<int> good() => Iterable<int>.generate(
      1000,
      (i) => i,
    );

Metadata

Metadata

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions