removing default case requirement?
claptrap
clap at trap.com
Sat Apr 1 07:32:28 UTC 2023
On Friday, 31 March 2023 at 23:52:29 UTC, Steven Schveighoffer
wrote:
> On 3/31/23 6:14 PM, Sebastiaan Koppe wrote:
>> On Friday, 31 March 2023 at 21:24:25 UTC, Steven Schveighoffer
>> wrote:
>>> If you "forget a case statement", or refactor it, forcing you
>>> to write `default: break;` doesn't fix that.
>>>
>>
>> Whenever I do write then, I do it reluctantly and mostly as a
>> last resort.
>
> So I do it when it makes sense that I only want to do something
> for the cases I specify.
>
> But that's not my point. If your code is:
>
> ```d
> switch(x)
> {
> case 0:
> doCase0;
> break;
> case 1:
> doCase1;
> break;
> }
> ```
>
> And you forgot case 42, it doesn't say "you forgot case 42". It
> says "you forgot the default case". So then you add `default:
> break;` and case 42 is still not there -- because you forgot it.
>
> If you refactor, and `default: break;` is already there, it
> doesn't complain.
How often does anyone use "default: break"?
i pretty much exclusively use "default: assert(0)"
If i have cases that do nothing I still put them in, default is
there to catch stuff ive forgoten.
More information about the Digitalmars-d
mailing list