Jump to page: 1 24  
Page
Thread overview
removing default case requirement?
Mar 31, 2023
ryuukk_
Mar 31, 2023
Sebastiaan Koppe
Mar 31, 2023
Sebastiaan Koppe
Apr 01, 2023
Timon Gehr
Apr 01, 2023
claptrap
Apr 01, 2023
Salih Dincer
Apr 18, 2023
bauss
Apr 02, 2023
Jack Applegame
Apr 02, 2023
Dom Disc
Apr 02, 2023
Ali Çehreli
Mar 31, 2023
bachmeier
Mar 31, 2023
Ali Çehreli
Mar 31, 2023
Max Samukha
Mar 31, 2023
H. S. Teoh
Mar 31, 2023
Timon Gehr
Mar 31, 2023
Dom Disc
Apr 01, 2023
Salih Dincer
Apr 01, 2023
Paul Backus
Apr 01, 2023
Salih Dincer
Apr 01, 2023
Walter Bright
Apr 01, 2023
Dukc
Apr 02, 2023
Ivan Kazmenko
Apr 02, 2023
Vladimir Panteleev
Apr 04, 2023
Quirin Schroll
Apr 15, 2023
Salih Dincer
March 31, 2023

Imagine if every if statement required you to write an else clause, even if it was empty. The idea would be, that you have to affirmatively attest that you didn't want to have a special case for when it didn't match the condition.

How fast do you think that everyone would run screaming for the exits?

But we have such a thing, in the default: case requirement.

In my code, it happens quite a bit that I throw an exception in the default case, or return something, but there's still a lot of default: break; code in there.

But consider a few things here:

  1. I have final switch at my disposal whenever I need it, which allows me to avoid default: and is really meant for "I want to make sure I handle everything"
  2. If you forget to return something, or throw an exception, the compiler will still let you know (i.e. some code paths don't return a value). This counts for the majority of places where I have a default case that's not just break;.
  3. I know in my experience (yours may vary) that when I forget to include a default, it's usually because I forgot to include default: break;. So the rule hasn't really helped me avoid bad code.
  4. switch is already bloated enough with the requirement for break or goto between cases.

If we wanted to relax this requirement it's actually an easy change -- no currently-compiling code will break.

Why not? How much does this rule help you, vs. annoy you?

-Steve

March 31, 2023

On Friday, 31 March 2023 at 15:20:41 UTC, Steven Schveighoffer wrote:

>

Imagine if every if statement required you to write an else clause, even if it was empty. The idea would be, that you have to affirmatively attest that you didn't want to have a special case for when it didn't match the condition.

How fast do you think that everyone would run screaming for the exits?

But we have such a thing, in the default: case requirement.

In my code, it happens quite a bit that I throw an exception in the default case, or return something, but there's still a lot of default: break; code in there.

But consider a few things here:

  1. I have final switch at my disposal whenever I need it, which allows me to avoid default: and is really meant for "I want to make sure I handle everything"
  2. If you forget to return something, or throw an exception, the compiler will still let you know (i.e. some code paths don't return a value). This counts for the majority of places where I have a default case that's not just break;.
  3. I know in my experience (yours may vary) that when I forget to include a default, it's usually because I forgot to include default: break;. So the rule hasn't really helped me avoid bad code.
  4. switch is already bloated enough with the requirement for break or goto between cases.

If we wanted to relax this requirement it's actually an easy change -- no currently-compiling code will break.

Why not? How much does this rule help you, vs. annoy you?

-Steve

Indeed, there are many rules like this one that just doesn't make sense, and just makes you type more, removing clarity from your own code

Also requiring "break" is annoying, all modern languages moved away from this noisy syntax and went the pattern matching and enum as expression route, and it feels much better to use

D stuck with C's enums and C's switch model

April 01, 2023
I prefer having the current behavior, it gives good guarantees that all possible situations are handled, including no-operation.

Inaction is just as much an action, as any other action. Having compiler enforced is very reassuring that everything is handled and done so intentionally.
March 31, 2023

On Friday, 31 March 2023 at 15:20:41 UTC, Steven Schveighoffer wrote:

>

If we wanted to relax this requirement it's actually an easy change -- no currently-compiling code will break.

Why not? How much does this rule help you, vs. annoy you?

I would prefer to make the change, but it's not a major point for me, so I wouldn't fight too hard either way. Offsetting some of this is that I find the code more readable this way.

March 31, 2023
On 3/31/23 08:20, Steven Schveighoffer wrote:

> But we have such a thing, in the `default:` case requirement.

If we went with that change, I would probably feel the need to write comments like the following:

    // No default case

... which would defeat the purpose. Am I fooling myself here? Because I don't know why I don't feel the same need for the 'if' statement.

Ali

March 31, 2023

On 3/31/23 1:25 PM, Ali Çehreli wrote:

>

On 3/31/23 08:20, Steven Schveighoffer wrote:

>

But we have such a thing, in the default: case requirement.

If we went with that change, I would probably feel the need to write comments like the following:

    // No default case

You can still write default: break; if you want.

>

... which would defeat the purpose. Am I fooling myself here? Because I don't know why I don't feel the same need for the 'if' statement.

I honestly think it's just habit. And I think that habit has to do somewhat with problems that are mostly solved (from C and C++).

-Steve

March 31, 2023

On 3/31/23 12:28 PM, Richard (Rikki) Andrew Cattermole wrote:

>

I prefer having the current behavior, it gives good guarantees that all possible situations are handled, including no-operation.

Inaction is just as much an action, as any other action. Having compiler enforced is very reassuring that everything is handled and done so intentionally.

Do you feel the same about if statements without else? Why is it different?

-Steve

April 01, 2023
On 01/04/2023 7:30 AM, Steven Schveighoffer wrote:
> On 3/31/23 12:28 PM, Richard (Rikki) Andrew Cattermole wrote:
>> I prefer having the current behavior, it gives good guarantees that all possible situations are handled, including no-operation.
>>
>> Inaction is just as much an action, as any other action. Having compiler enforced is very reassuring that everything is handled and done so intentionally.
> 
> Do you feel the same about if statements without `else`? Why is it different?
> 
> -Steve

No.

Usually if statements is when you want to handle non-linear control flow. They require higher levels of scrutiny to ensure they are correct for the overall function flow to be correct.

Think about it in terms of flow chart. A switch statement is just a process step, whereas an if statement would be a conditional.
March 31, 2023

On Friday, 31 March 2023 at 18:30:27 UTC, Steven Schveighoffer wrote:

>

Do you feel the same about if statements without else? Why is it different?

-Steve

If statements are rather binary, and it is easy to understand the two paths.

Switches often have more than 2 cases, and its easier to forget one.

Also consider refactoring. With switches I might add or rename or replace one of the values, which doesn't happen with if statements.

March 31, 2023

On Friday, 31 March 2023 at 15:20:41 UTC, Steven Schveighoffer wrote:

>

Why not? How much does this rule help you, vs. annoy you?

Annoys.

« First   ‹ Prev
1 2 3 4