Created with <3 with dartpad.dev.
Last active
November 21, 2023 15:24
-
-
Save kitsuniru/3ef333bb70c253c0bcd996ad8386d477 to your computer and use it in GitHub Desktop.
dart3 instead of if else if else
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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