Difference between Switch Statements and Expressions in Dart
Dart 3 brings switch expressions, which greatly simplify switch statements. They are shorter and easier to read because they remove the need for the case
and break
keywords. Switch expressions offer a more straightforward way to achieve the same functionality, making your code cleaner and more understandable. In this post, we will explore how to implement switch expressions and understand their differences.
Switch statements
Before we look into switch expressions let us first go over a regular switch statement.
In our main.dart
file we defined a function that will return a String
based on the number we provide in the parameter, see the code below:
void main() {
String getNumber(int number) {
String text;
switch (number) {
case 1:
text = '5';
break;
case 2:
text = '10';
break;
case 3:
text = '15';
break;
case 4:
text = '20';
break;
case 5:
text = '25';
break;
case 6:
text = '30';
break;
case 7:
text = '35';
break;
default:
text = 'Number too high!';
break;
}
return text;
}
print(getNumber(5));
}
In the given code snippet, we created a function called getNumber
that takes an integer
called number
as input. The function uses a switch statement to determine the corresponding string
based on the value of number
.
Finally, we call the function and print its result, which in this case will be a String
with the value of '25'
. However, as you can see this simple logic requires a lot of code.
Switch expressions
A switch expression generates a value based on the matching case within its expression body. In Dart, you can use switch expressions wherever expressions are allowed, except at the beginning of an expression statement.
void main() {
String getNumber(int number) {
String text;
text = switch (number) {
1 => '5',
2 => '10',
3 => '15',
4 => '20',
5 => '25',
6 => '30',
7 => '35',
_ => 'Number too high!'
};
return text;
}
print(getNumber(8));
}
In the above code snippet, we changed the switch statement into a switch expression. We removed the case
and break
keywords and are now using the =>
operator to return our value for every case we have. As you can see our switch expression will not set a value but directly return a value. That is why we can assign the switch expression directly to the text
variable. Furthermore, the default keyword is replaced with an underscore (_
) to represent the default
return value. When we will call the function with a parameter of 8
, it will still return "Number too high!".
Even though we shrunk the code by a lot, we can simplify it even more by directly returning our switch statement:
void main() {
String getNumber(int number) {
return switch (number) {
1 => '5',
2 => '10',
3 => '15',
4 => '20',
5 => '25',
6 => '30',
7 => '35',
_ => 'Number too high!'
};
}
print(getNumber(8));
}
In the above code instead of creating a variable we directly return our switch statement.
Advanced switch statements
More advanced switch statements can also be converted to switch expressions, see the following switch statement:
void main() {
String getNumber(int number) {
String text;
switch (number) {
case <= 3:
text = '5';
break;
case 4:
case 5:
text = '10';
break;
default:
text = 'Number too high!';
break;
}
return text;
}
print(getNumber(5));
}
In this "advanced" switch statement we changed the logic a bit. Now when the number
is less than or equal to 3
, it assigns the string '5'
to the text
variable. If the number
is either 4
or 5
, it assigns '10'
to the variable. Otherwise, if none of the previous cases match, it assigns the string "Number too high!".
If we want to do the same in a switch expression we end up with the following code:
void main() {
String getNumber(int number) {
return switch (number) {
<= 3 => '5',
4 || 5 => '10',
_ => 'Number too high!'
};
}
print(getNumber(5));
}
As you can see, it is way shorter, but it can be a little confusing.
Conclusion
In Dart, switch expressions introduced in Dart 3 offer a simpler and shorter way to handle multiple cases and return values based on conditions compared to traditional switch statements. By using the arrow (=>
) operator, switch expressions directly return the corresponding value for each case without the need for case
and break
keywords. The default case is represented by an underscore (_
) instead of the default
keyword. Switch expressions simplify the code, making it easier to read and reducing unnecessary lines, resulting in cleaner and more efficient code.