Skip to content

Instantly share code, notes, and snippets.

@kitsuniru
Last active November 21, 2023 15:24
Show Gist options
  • Save kitsuniru/3ef333bb70c253c0bcd996ad8386d477 to your computer and use it in GitHub Desktop.
Save kitsuniru/3ef333bb70c253c0bcd996ad8386d477 to your computer and use it in GitHub Desktop.
dart3 instead of if else if else

dart3 instead of if else if else

Created with <3 with dartpad.dev.

void main() {
const x = 8;
const y = SomeLongEnum.veryVeryVeryLongEnumValue;
// instead of this unreadable bullshit
int runTest(int taxiStationId) {
if (y == SomeLongEnum.veryVeryVeryLongEnumValue &&
taxiStationId % 3 == 0 &&
taxiStationId % 3 == 0 &&
y == SomeLongEnum.veryVeryVeryLongEnumValue &&
taxiStationId % 3 == 0 &&
taxiStationId % 3 == 0) {
return 3;
} else if (taxiStationId % 2 == 0) {
return 2;
} else {
return 1;
}
}
// we can do like this
int runTestDart(int taxiStationId) => switch (()) {
_ //theres can be as many as you want conditions
when
y == SomeLongEnum.veryVeryVeryLongEnumValue &&
taxiStationId % 3 == 0 &&
taxiStationId % 3 == 0 &&
y == SomeLongEnum.veryVeryVeryLongEnumValue &&
taxiStationId % 3 == 0 &&
taxiStationId % 3 == 0 =>
// issue: a value will be moved at next line after format
3,
_ when taxiStationId % 2 == 0 => 2,
_ => 1,
};
print(runTest(x));
print(runTestDart(x));
}
enum SomeLongEnum { short, veryVeryVeryLongEnumValue }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment