In our flutter project, with use a lot of enum
s and extension
s. Some of us write switch
statements, some others use map
s. I was wondering what practice is considered the best one in terms of time, memory etc ?
enum Animal { cat, dog, bird }
Switch statement:
extension AnimalExtension on Animal {
String get name {
switch (this) {
case Animal.cat:
return 'Cat';
case Animal.dog:
return 'Dog';
case Animal.bird:
return 'Bird';
}
}
}
Map:
extension AnimalExtension on Animal {
String get name => {
Animal.cat: 'Cat',
Animal.dog: 'Dog',
Animal.bird: 'Bird',
}[this];
}
Also, for the map
method, is it better to create a static const Map
instead of instantiating it on the fly at every call?