June 22, 2010
Steven Schveighoffer wrote:
> On Mon, 21 Jun 2010 20:40:14 -0400, Adam Ruppe <destructionator@gmail.com> wrote:
> 
>> What's the point of a switch without implicit fallthrough?
> 
> Maintenance.  Using if statements instead of switch, you have to repeat the value to test for each of the cases.  If you want to change the value being tested, it's one change.  And your workaround using a delegate is not very appealing.
> 
> I'll also point out that popular languages have a switch statement and don't allow implicit fallthrough, meaning that 100% of switch statements do not have fallthrough.  And switch is used quite often in those languages too, so at least some people think it has use besides allowing implcit fallthrough.
> 
> I think mostly it's because the meaning of it is so easy to understand when reading/writing it.  When you see a switch, you know what it is and what it isn't.  An if statement has many possibilities and must be read more carefully.
> 
> -Steve

I believe that the switch statement originated as the equivalent of an asm jump table. You have a list of values, and a list of addresses to jump to. The existence of fallthough seems to be an implementation artifact.
June 22, 2010
Jeff Nowakowski wrote:
> On 06/22/2010 12:06 AM, Don wrote:
>>> Finally, goto is ugly and continue is strongly associated with looping.
>>
>> ? It's most commonly used in error handling.
>> But, fallthrough actually is a goto.
> 
> Do I really have to argue that goto is mostly deprecated, and has been for decades? 

I think you misunderstood. I was arguing that there is no association between 'goto' and loops. People who use goto, use it for error handling, not for looping. I was by no means advocating the use of goto for error handling!
June 22, 2010
Lars T. Kyllingstad, el 22 de junio a las 09:14 me escribiste:
> On Mon, 21 Jun 2010 20:27:32 -0700, Bill Baxter wrote:
> 
> > On Mon, Jun 21, 2010 at 6:31 PM, Leandro Lucarella <luca@llucax.com.ar> wrote:
> >> Bill Baxter, el 21 de junio a las 17:13 me escribiste:
> >>> On Mon, Jun 21, 2010 at 4:24 PM, Leandro Lucarella <luca@llucax.com.ar> wrote:
> >>> > goto next case; is a little more verbose but very clear to me :)
> >>> >
> >>> > Maybe just next case; is a shorter alternative...
> >>>
> >>> That would be great if "next" were a D keyword.  But I don't think you're going to get Walter to add a keyword just for this.
> >>
> >> Damn! Where did I get next from? I don't know...
> > 
> > I think it's a keyword in Perl maybe?
> > 
> > --bb
> 
> It is a keyword in BASIC, at least. :)
> 
>   FOR i = 1 TO 100
>     PRINT i
>   NEXT

I'm sure it's Perl, even when I written Basic (GW and Q) code, I remember it having the same meaning as C's "continue".

-- 
Leandro Lucarella (AKA luca)                     http://llucax.com.ar/
----------------------------------------------------------------------
GPG Key: 5F5A8D05 (F8CD F9A7 BF00 5431 4145  104C 949E BFB6 5F5A 8D05)
----------------------------------------------------------------------
According to several sources
Dr. Harvey Kellogg tried to make a cure for masturbation
When he made cornflakes
June 22, 2010
Steven Schveighoffer, el 22 de junio a las 07:26 me escribiste:
> On Mon, 21 Jun 2010 20:40:14 -0400, Adam Ruppe <destructionator@gmail.com> wrote:
> 
> >What's the point of a switch without implicit fallthrough?
> 
> Maintenance.  Using if statements instead of switch, you have to repeat the value to test for each of the cases.  If you want to change the value being tested, it's one change.  And your workaround using a delegate is not very appealing.

And it's a bit comfortable when used with expressions that should be evaluated just one:

switch (foo()) {
	case xxx:
	...
	case yyy:
	...
}

vs

auto tmp = foo();
if (tmp == xxx) {
	...
else if (tmp == yyy) {
	...
}

> I'll also point out that popular languages have a switch statement and don't allow implicit fallthrough, meaning that 100% of switch statements do not have fallthrough.  And switch is used quite often in those languages too, so at least some people think it has use besides allowing implcit fallthrough.
> 
> I think mostly it's because the meaning of it is so easy to understand when reading/writing it.  When you see a switch, you know what it is and what it isn't.  An if statement has many possibilities and must be read more carefully.

Like for/while vs goto and foreach vs while/for. They are specializations for common uses. It helps both the programmer to write/read more clear code and the compiler to generate more efficient code because they have a more defined semantics and it can make more assumptions.

-- 
Leandro Lucarella (AKA luca)                     http://llucax.com.ar/
----------------------------------------------------------------------
GPG Key: 5F5A8D05 (F8CD F9A7 BF00 5431 4145  104C 949E BFB6 5F5A 8D05)
----------------------------------------------------------------------
Creativity is great but plagiarism is faster
June 22, 2010
What you guys are saying makes enough sense. switch will always be a series of labels and jumps in my mind, but I can deal with this.

One note that I think is the main reason people find fallthrough confusing: they hit tab once too many times.

switch(a) {
    case 10:
    case 20: // obvious fallthrough, no break directly above it
    break; // obvious break - same indentation as the case, just like
braces would be
    case 30:
}

I notice a lot of other people indent the break further than the case, which is something I only do if the break is not the last statement (which means it is probably indented twice, being inside an if or something.)


Oh well.
June 22, 2010
On 06/22/2010 08:39 AM, Don wrote:
>
> I think you misunderstood. I was arguing that there is no association
> between 'goto' and loops. People who use goto, use it for error
> handling, not for looping. I was by no means advocating the use of goto
> for error handling!

Oh good :) The looping part wasn't about "goto", it was about the alternate proposal of reusing "continue" to mean fallthrough.
June 24, 2010
On Tue, 22 Jun 2010 09:17:57 -0400, Adam Ruppe <destructionator@gmail.com> wrote:
> 
> What you guys are saying makes enough sense. switch will always be a series of labels and jumps in my mind, but I can deal with this.
> 
> One note that I think is the main reason people find fallthrough confusing: they hit tab once too many times.
> 
> switch(a) {
>     case 10:
>     case 20: // obvious fallthrough, no break directly above it
>     break; // obvious break - same indentation as the case, just like
> braces would be
>     case 30:
> }
> 
> I notice a lot of other people indent the break further than the case, which is something I only do if the break is not the last statement (which means it is probably indented twice, being inside an if or something.)
> 
> 
> Oh well.

I would argue the exact opposite. I think it's not immediately clear what's associated with each case label in the example given. It's a matter of personal preference, though, in the end. I leave labels (both for switches and goto) at the left-most position of their enclosing block:

switch (a) {
case 5:
case 10:
    break;
default:
    ;
}

void main () {
    goto doSomething;

doSomething:
    writeln("foobar");
}

But I know there will probably be lots of people who dislike that as well. :P
1 2 3 4 5 6 7 8 9 10
Next ›   Last »