February 20, 2014
On Thu, 20 Feb 2014 10:13:27 -0500, Daniel Murphy <yebbliesnospam@gmail.com> wrote:

> "Steven Schveighoffer"  wrote in message news:op.xbk44onleav7ka@stevens-macbook-pro.local...
>
>> What I really would be curious about is if in most D code, you see a lot more default: break; than default: assert(0);
>
> I just did a quick git-grep on the compiler source (not D, but all switches do have a default thanks to the d port)
>
> With 707 "default:"s 68 had a break on either the same or next line, and 249 had an assert(0).
>
> On phobos I get 22 assert(0)s vs 10 breaks with 147 defaults
>
> With druntime i get 24 assert(0)s + 5 error();s vs 11 breaks with 64 defaults.

Good data, but I was more thinking of people who use D, not the core language. The core language's developers have different behaviors than standard users. I'm not dismissing this data, but I would like to see more application statistics.

Another interesting data point would be whether any of those asserts were inserted after the language deprecated missing defaults, or if they existed beforehand.

One thing that is nice is how asserts are supported in D. It makes this a much easier decision.

-Steve
February 20, 2014
On Thu, 20 Feb 2014 10:08:48 -0500, bearophile <bearophileHUGS@lycos.com> wrote:

> Steven Schveighoffer:
>
>> What I really would be curious about is if in most D code, you see a lot more default: break; than default: assert(0);
>
> I have written tons of D2 code in the last years, and such "default: assert(0);" is quite uncommon in my code. Probably less than 8-10 usages in the whole code base.

But what about default: break;?

-Steve
February 20, 2014
"Steven Schveighoffer"  wrote in message news:op.xbk5cxeoeav7ka@stevens-macbook-pro.local...

> Putting default: assert(0); changes the meaning of the code. In other words:
>
> switch(x)
> {
>     case 1: ...
> }
>
> is the same as:
>
> switch(x)
> {
>     case 1: ...
>     default: break;
> }
>
> You are keeping the code the same. I don't think people would think of adding the assert(0), I wouldn't.

Are we talking about writing new code or porting C/C++ code?  If the latter, I agree there is a high chance of just copy-pasting in a default: break; everywhere the error pops up.

> And as Ary pointed out, it may be that you expect the default to occur, but don't want to do anything.

Sure, and you know this when you write the code and choose the correct one.

> 'if' and 'switch' are commonly used interchangeably. Frequently one uses 'switch' where they would normally use 'if' to avoid evaluating something multiple times.

I'm not sure this is true, at least not in my code. 

February 20, 2014
On 21 February 2014 01:20, Steven Schveighoffer <schveiguy@yahoo.com> wrote:

> On Thu, 20 Feb 2014 10:13:27 -0500, Daniel Murphy < yebbliesnospam@gmail.com> wrote:
>
>  "Steven Schveighoffer"  wrote in message news:op.xbk44onleav7ka@
>> stevens-macbook-pro.local...
>>
>>  What I really would be curious about is if in most D code, you see a lot
>>> more default: break; than default: assert(0);
>>>
>>
>> I just did a quick git-grep on the compiler source (not D, but all switches do have a default thanks to the d port)
>>
>> With 707 "default:"s 68 had a break on either the same or next line, and
>> 249 had an assert(0).
>>
>> On phobos I get 22 assert(0)s vs 10 breaks with 147 defaults
>>
>> With druntime i get 24 assert(0)s + 5 error();s vs 11 breaks with 64
>> defaults.
>>
>
> Good data, but I was more thinking of people who use D, not the core language. The core language's developers have different behaviors than standard users. I'm not dismissing this data, but I would like to see more application statistics.
>

In my little app:
 17 default: break;
 1 default: assert(0); ... and I just realised it should have been a final
switch() anyway... so now there's 0.

I haven't just been lazy, the default case just happens to be as if an unhandled else in many cases.

Another interesting data point would be whether any of those asserts were
> inserted after the language deprecated missing defaults, or if they existed beforehand.
>
> One thing that is nice is how asserts are supported in D. It makes this a much easier decision.
>
> -Steve
>


February 20, 2014
On 2/20/14, 1:53 PM, Manu wrote:
> On 21 February 2014 01:20, Steven Schveighoffer <schveiguy@yahoo.com
> <mailto:schveiguy@yahoo.com>> wrote:
>
>     On Thu, 20 Feb 2014 10:13:27 -0500, Daniel Murphy
>     <yebbliesnospam@gmail.com <mailto:yebbliesnospam@gmail.com>> wrote:
>
>         "Steven Schveighoffer"  wrote in message
>         news:op.xbk44onleav7ka@__stevens-macbook-pro.local...
>
>             What I really would be curious about is if in most D code,
>             you see a lot more default: break; than default: assert(0);
>
>
>         I just did a quick git-grep on the compiler source (not D, but
>         all switches do have a default thanks to the d port)
>
>         With 707 "default:"s 68 had a break on either the same or next
>         line, and 249 had an assert(0).
>
>         On phobos I get 22 assert(0)s vs 10 breaks with 147 defaults
>
>         With druntime i get 24 assert(0)s + 5 error();s vs 11 breaks
>         with 64 defaults.
>
>
>     Good data, but I was more thinking of people who use D, not the core
>     language. The core language's developers have different behaviors
>     than standard users. I'm not dismissing this data, but I would like
>     to see more application statistics.
>
>
> In my little app:
>   17 default: break;
>   1 default: assert(0); ... and I just realised it should have been a
> final switch() anyway... so now there's 0.

Did you put those "default: break;" because:

1. The compiler told it to do so.
2. You already know the compiler will tell you, so you put it before that happens.

In the cases where it was "1.", did it make you think whether you needed to handle the default case?

February 21, 2014
On 21 February 2014 03:27, Ary Borenszweig <ary@esperanto.org.ar> wrote:

> On 2/20/14, 1:53 PM, Manu wrote:
>
>> On 21 February 2014 01:20, Steven Schveighoffer <schveiguy@yahoo.com <mailto:schveiguy@yahoo.com>> wrote:
>>
>>     On Thu, 20 Feb 2014 10:13:27 -0500, Daniel Murphy
>>     <yebbliesnospam@gmail.com <mailto:yebbliesnospam@gmail.com>> wrote:
>>
>>         "Steven Schveighoffer"  wrote in message
>>         news:op.xbk44onleav7ka@__stevens-macbook-pro.local...
>>
>>
>>             What I really would be curious about is if in most D code,
>>             you see a lot more default: break; than default: assert(0);
>>
>>
>>         I just did a quick git-grep on the compiler source (not D, but
>>         all switches do have a default thanks to the d port)
>>
>>         With 707 "default:"s 68 had a break on either the same or next
>>         line, and 249 had an assert(0).
>>
>>         On phobos I get 22 assert(0)s vs 10 breaks with 147 defaults
>>
>>         With druntime i get 24 assert(0)s + 5 error();s vs 11 breaks
>>         with 64 defaults.
>>
>>
>>     Good data, but I was more thinking of people who use D, not the core
>>     language. The core language's developers have different behaviors
>>     than standard users. I'm not dismissing this data, but I would like
>>     to see more application statistics.
>>
>>
>> In my little app:
>>   17 default: break;
>>   1 default: assert(0); ... and I just realised it should have been a
>> final switch() anyway... so now there's 0.
>>
>
> Did you put those "default: break;" because:
>
> 1. The compiler told it to do so.
> 2. You already know the compiler will tell you, so you put it before that
> happens.
>

Because the compiler told me. It's not a habit of mine to type it.


In the cases where it was "1.", did it make you think whether you needed to
> handle the default case?
>

Never. If I meant to handle the case, I would have already handled the case. I've never forgotten to handle the case when I was supposed to. In the same way I've never forgotten to write else after an if when I intend to.


1 2 3 4 5 6 7 8 9
Next ›   Last »