dart_style
dart_style copied to clipboard
switch statements can get much longer/harder to read
Hard to make a general rule, but I had
switch (pattern[0]) {
case 'a': return formatAmPm(date);
case 'c': return formatStandaloneDay(date);
case 'd': return formatDayOfMonth(date);
case 'D': return formatDayOfYear(date);
...
and it turns into
switch (pattern[0]) {
case 'a':
return formatAmPm(date);
case 'c':
return formatStandaloneDay(date);
case 'd':
return formatDayOfMonth(date);
case 'D':
return formatDayOfYear(date);
where it's both longer and harder to see at a glance what the cases are.
or similarly
void parseMonth(input, dateFields) {
var possibilities;
switch(width) {
case 5: possibilities = symbols.NARROWMONTHS; break;
case 4: possibilities = symbols.MONTHS; break;
case 3: possibilities = symbols.SHORTMONTHS; break;
default: return handleNumericField(input, dateFields.setMonth);
}
dateFields.month = parseEnumeratedString(input, possibilities) + 1;
}
becomes
void parseMonth(input, dateFields) {
var possibilities;
switch (width) {
case 5:
possibilities = symbols.NARROWMONTHS;
break;
case 4:
possibilities = symbols.MONTHS;
break;
case 3:
possibilities = symbols.SHORTMONTHS;
break;
default:
return handleNumericField(input, dateFields.setMonth);
}
dateFields.month = parseEnumeratedString(input, possibilities) + 1;
}
Yeah, this is a limitation. I don't know if I'll do something smarter here, but I'm not opposed to the idea.
Another:
switch (match.group(0)) {
case "\n" : return r"\n";
case "\"" : return r'\"';
case "\b" : return r"\b";
case "\t" : return r"\t";
case "\f" : return r"\f";
case "\v" : return r"\v";
}
switch (match.group(0)) {
case "\n":
return r"\n";
case "\"":
return r'\"';
case "\b":
return r"\b";
case "\t":
return r"\t";
case "\f":
return r"\f";
case "\v":
return r"\v";
}
Now that we got rid of break
, maybe this can be reconsidered?
switch (n) {
case 1: debugPrint('one');
case 2: debugPrint('two');
default: debugPrint(n.toString());
}
Yes, I think so! :)
In fact, I even had it implemented when I added support for the other new Dart 3.0 features. Unfortunately, it:
- Caused a performance regression when formatting some code.
- Caused enough changes existing code that it really needs to go through the full formatter change process.
Because of those, I ended up removing it. I am still interested in making this change, but right now I'm focused on experimenting with a unified Flutter-like style first.