About switch case statements...

Andrei Alexandrescu SeeWebsiteForEmail at erdani.org
Sun Nov 15 12:58:18 PST 2009


Walter Bright wrote:
> Michel Fortin wrote:
>> Hum two suggestions. Sometime I want fall-through when I write a 
>> switch, but I always make it clear that it isn't a bug by writing the 
>> intent in a short comment:
>>
>>     switch (c) {
>>         case 1:
>>             blah();
>>             break;
>>         case 2:
>>             blah();
>>             // fallthrough
>>         case 3:
>>             blah();
>>             break;
>>     }
> 
> I just use whitespace:
> 
>      switch (c) {
>          case 1:
>              blah();
>              break;
> 
>          case 2:
>              blah();
>          case 3:
>              blah();
>              break;
>      }
> 
> Works fine.

No whitespace works fine, too. I strongly advise requiring control flow 
at the end of the break. Your style notwithstanding, fall through *is* 
rare. It is also more brittle than goto case xxx; because it is not 
invariant to manual code motion. It does make sense to write more to 
achieve the more dangerous and less used result, than the other way around.

Andrei



More information about the Digitalmars-d mailing list