Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Conversation

@Hixie
Copy link
Contributor

@Hixie Hixie commented Jul 21, 2023

No description provided.

@github-actions github-actions bot added the platform-web Code specifically for the web engine label Jul 21, 2023

@override
String toString() {
switch (type) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could be switch expression!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it could be an if chain too :-P

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

...but a switch expression is better.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's tighter. It makes it trivial to parse the intent. You KNOW every branch yields a value. Greatly increases scannability of the code IMHO

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think there's a meaningful difference in readability between these two:

String toString() {
  switch (type) {
    case ColorFilterType.mode:
      return 'ColorFilter.mode($color, $blendMode)';
    case ColorFilterType.matrix:
      return 'ColorFilter.matrix($matrix)';
    case ColorFilterType.linearToSrgbGamma:
      return 'ColorFilter.linearToSrgbGamma()';
    case ColorFilterType.srgbToLinearGamma:
      return 'ColorFilter.srgbToLinearGamma()';
  }
}
String toString() {
  return switch (type) {
    ColorFilterType.mode => 'ColorFilter.mode($color, $blendMode)',
    ColorFilterType.matrix => 'ColorFilter.matrix($matrix)',
    ColorFilterType.linearToSrgbGamma => 'ColorFilter.linearToSrgbGamma()',
    ColorFilterType.srgbToLinearGamma => 'ColorFilter.srgbToLinearGamma()',
  };
}

In both cases the language enforces that there must be a return. In both cases it scans relatively easily (maybe the first is a little easier, in the second the return is not as obvious because of being on the same line as the switch, but maybe the second is easier because of the avoidance of line wrapping).

The former in general is better IMHO in this case just because it's more consistent with other code in the same package (it's literally just copied and pasted from the existing code elsewhere), and because it's easier to maintain if we ever have to add a branch that's more complicated than just a single string return.

@Hixie Hixie added the autosubmit Merge PR when tree becomes green via auto submit App label Jul 24, 2023
@auto-submit auto-submit bot merged commit 1e737db into flutter:main Jul 24, 2023
engine-flutter-autoroll added a commit to engine-flutter-autoroll/flutter that referenced this pull request Jul 24, 2023
auto-submit bot pushed a commit to flutter/flutter that referenced this pull request Jul 24, 2023
LouiseHsu pushed a commit to LouiseHsu/flutter that referenced this pull request Jul 31, 2023
vashworth pushed a commit to vashworth/flutter that referenced this pull request Aug 2, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

autosubmit Merge PR when tree becomes green via auto submit App platform-web Code specifically for the web engine

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants