dart_style icon indicating copy to clipboard operation
dart_style copied to clipboard

switch statements can get much longer/harder to read

Open alan-knight opened this issue 10 years ago • 5 comments

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.

alan-knight avatar Nov 10 '14 23:11 alan-knight

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;
  }

alan-knight avatar Nov 10 '14 23:11 alan-knight

Yeah, this is a limitation. I don't know if I'll do something smarter here, but I'm not opposed to the idea.

munificent avatar Nov 11 '14 00:11 munificent

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";
      }

munificent avatar Jan 14 '15 18:01 munificent

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());
  }

sufftea avatar May 25 '23 19:05 sufftea

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:

  1. Caused a performance regression when formatting some code.
  2. 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.

munificent avatar Jun 08 '23 22:06 munificent