No breaks
Sometimes Google Dart mindlessly derives from Java and C, without taking a step back and improving obvious flaws.
Here’s how Dart implements switch construct:
var command = 'OPEN';
switch (command) {
case 'CLOSED':
executeClosed();
break;
case 'PENDING':
executePending();
break;
case 'APPROVED':
executeApproved();
break;
case 'DENIED':
executeDenied();
break;
case 'OPEN':
executeOpen();
break;
default:
executeUnknown();
}
In 1960s Dennis Ritchie, by error of judgment decided to implement the switch statement a confusing error-prone way. You have to manually call “break” after every case otherwise several branches get executed together. Now, 50 years later we still using this ridiculous syntax. Why do we need breaks?
Apple deleted breaks in Swift and made the code much cleaner:
let command = 'OPEN'
switch (command) {
case 'CLOSED':
executeClosed()
case 'PENDING':
executePending()
case 'APPROVED':
executeApproved()
case 'DENIED':
executeDenied()
case 'OPEN':
executeOpen()
default:
executeUnknown()
}
Rust, another C-based language cleans up switch in another way:
let my_number = 1i;
match my_number {
0 => println!(“zero”),
1 | 2 => println!(“one or two”),
3..10 => println!(“three to ten”),
_ => println!(“something else”)
}
Go, one more C-based language created inside Google (same as Dart) also used clean switch statements:
switch c {
case ‘&’:
esc = “&”
case ‘\’’:
esc = “'”
case ‘<’:
esc = “<”
case ‘>’:
esc = “>”
case ‘”’:
esc = “"”
default:
panic(“unrecognized escape character”)
}This makes me wonder: why does the Dart team decided to keep the breaks?