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:
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.
It reminds me of an old Internet meme (maybe? This might have been before memes) which goes something like:
delete myfiles
are you sure?
yes
This will remove 'myfiles', are you really sure about this? you can't get it back.
YES GODDAMMIT, just delete it already!!!
'myfiles' has been deleted
Ah crap...
> Having the default be open-endedness means I don't get reminded about that ugly detail anymore.
Of the two evils, I prefer the explicit one.
The open-endededness is not that you forgot a case, it's that you didn't care about that case, or it's not something that requires a case in that particular switch.
I really am puzzled by the idea that the compiler requiring one to put default:
is helping them remember things.
To be honest, I'm OK with the requirement. I really am, I can write default: break;
and I actually do with regularity without forgetting! But it's a hard one to explain to newcomers. It's just one of those things that I explain with "D makes you do this, get over it". I can't really explain that it's helpful with a straight face.
-Steve