www.digitalmars.com         C & C++   DMDScript  

D - switch statement

reply Paul Conway <Paul_member pathlink.com> writes:
I just encountered D today for the first time. In some ways I am impressed. I
have been a C++ user for some years now. I always felt it should have made a
cleaner break from legacy C. The C struct tag nonsense goes away in D --
excellent. The C pre-processor and .H files go away -- also excellent. Etc.

But I am concerned that an opportunity has not been taken to improve on the C
and C++ switch statement, which appears to have survived intact. In my
experience the absence of a break statement within the body of a switch
statement, such that it is possible to fall through into the next case, is
typically a mistake. If I were to do this, I would always add a "// FALL THRU"
comment to assist in maintenance by making the intention clear. Since, in my
experience, a well-written switch statement tends to avoid falling through, it
would make a lot of sense to me if it were illegal to fall through, thus
rendering explicit break statements unnecessary, and allowing them instead to
cause break-out from an enclosing loop if there is one.

For several years now I have been using a language called Smallworld Magik which
was developed about the same time as Python and has considerable similarities
though done entirely independently. In Magik as in Python there is no such thing
as falling through and yet programmers survive.

In Magik there is no goto and yet the company I work for has a base of millions
of lines of code that has been happily written without it. Surely eliminating
the goto and tightening up the switch statement would be good to do in D?

"A mechanism of world inter-communication will be devised, embracing the whole
planet, freed from national hindrances and restrictions, and functioning with
marvellous swiftness...." -- Shoghi Effendi -- The Unfoldment of World
Civilisation -- 11 Mar 1936
Jan 19 2003
next sibling parent reply Ilya Minkov <midiclub 8ung.at> writes:
Paul Conway wrote:
 I just encountered D today for the first time. In some ways I am impressed. I
 have been a C++ user for some years now. I always felt it should have made a
 cleaner break from legacy C. The C struct tag nonsense goes away in D --
 excellent. The C pre-processor and .H files go away -- also excellent. Etc.
 
 But I am concerned that an opportunity has not been taken to improve on the C
 and C++ switch statement, which appears to have survived intact. In my
 experience the absence of a break statement within the body of a switch
 statement, such that it is possible to fall through into the next case, is
 typically a mistake. If I were to do this, I would always add a "// FALL THRU"
 comment to assist in maintenance by making the intention clear. Since, in my
 experience, a well-written switch statement tends to avoid falling through, it
 would make a lot of sense to me if it were illegal to fall through, thus
 rendering explicit break statements unnecessary, and allowing them instead to
 cause break-out from an enclosing loop if there is one.
I have seen a lot of wonderful uses for a fall-through switch statement, which don't hamper the legibility, but improve it. Anyway, it has to be possible to fall through on switch somehow. Maybe a warning at fallthrough, which can be shut off somehow?
 For several years now I have been using a language called Smallworld Magik
which
 was developed about the same time as Python and has considerable similarities
 though done entirely independently. In Magik as in Python there is no such
thing
 as falling through and yet programmers survive.
May i remind you that D is a successor to C. There are lots of good longuages, one exmple is Sather. But no good C ascendants other then D. I'm not a particular fan of C, i'm rather a Modula or O-Pascal guy. But there is no way to convince many programmers to learn anything else than C. C is, C has been, C will always be. Others light up and fade away, may they be thousands of times better. Only a C succesor has a chance.
 In Magik there is no goto and yet the company I work for has a base of millions
 of lines of code that has been happily written without it. Surely eliminating
 the goto and tightening up the switch statement would be good to do in D?
In Pascal ascendants, we don't have a fall-through switch either. But we have an often "undocumented" goto. In my early days, after having heard that "goto is evil", i wrote some code (i guess it was a chess knight problem), where i avoided using goto at any cost. Then i looked at the mess and realised: it is not necessary to follow these rumors blindly. I have rewritten it using goto and it has contracted by half in source and became more legible. The best thing to do for legibility are comments. As i grow older, there become more comments and less code in my code. And i do make justified use of anything. Why should I be prevented to do what is natural for a machine and make it another, non-natural way? This is basically what C is, it's always natural to a machine.
 "A mechanism of world inter-communication will be devised, embracing the whole
 planet, freed from national hindrances and restrictions, and functioning with
 marvellous swiftness...." -- Shoghi Effendi -- The Unfoldment of World
 Civilisation -- 11 Mar 1936
 
Jan 20 2003
next sibling parent reply "Daniel Yokomiso" <daniel_yokomiso yahoo.com.br> writes:
"Ilya Minkov" <midiclub 8ung.at> escreveu na mensagem
news:b0h3bi$16tc$1 digitaldaemon.com...

[snip]

 The best thing to do for legibility are comments.
 As i grow older, there become more comments and less code in my code.
It's strange. As I grow older there is less comments in my code and more contracts/tests/granular functions ;-) --- Outgoing mail is certified Virus Free. Checked by AVG anti-virus system (http://www.grisoft.com). Version: 6.0.443 / Virus Database: 248 - Release Date: 10/1/2003
Jan 20 2003
next sibling parent Ilya Minkov <midiclub 8ung.at> writes:
Daniel Yokomiso wrote:
 "Ilya Minkov" <midiclub 8ung.at> escreveu na mensagem
 news:b0h3bi$16tc$1 digitaldaemon.com...
 
 [snip]
 
 
The best thing to do for legibility are comments.
As i grow older, there become more comments and less code in my code.
It's strange. As I grow older there is less comments in my code and more contracts/tests/granular functions ;-)
I've got some strange kind of... I can't concentrate. I forget what something means as soon as it's not on the screen anymore. And when i scroll back, i have to figure it out again, all for new. And i'm only 20. Wonder what a wreck i'll be in five years. :> But well, i fully support contracts and such, because they'll test something for me. This doesn't mean i'd write less comment though.
 
 ---
 Outgoing mail is certified Virus Free.
 Checked by AVG anti-virus system (http://www.grisoft.com).
 Version: 6.0.443 / Virus Database: 248 - Release Date: 10/1/2003
 
 
Jan 20 2003
prev sibling parent "Walter" <walter digitalmars.com> writes:
"Daniel Yokomiso" <daniel_yokomiso yahoo.com.br> wrote in message
news:b0h3vm$1793$1 digitaldaemon.com...
 "Ilya Minkov" <midiclub 8ung.at> escreveu na mensagem
 news:b0h3bi$16tc$1 digitaldaemon.com...
 The best thing to do for legibility are comments.
 As i grow older, there become more comments and less code in my code.
It's strange. As I grow older there is less comments in my code and more contracts/tests/granular functions ;-)
The problem with comments are they are invariably incorrect, incomplete, out of date, and wrong. As you said, contracts and unit tests can replace many comments.
Feb 24 2003
prev sibling next sibling parent reply Antti Sykari <jsykari gamma.hut.fi> writes:
Ilya Minkov <midiclub 8ung.at> writes:

 Paul Conway wrote:
 In Magik there is no goto and yet the company I work for has a base
 of millions of lines of code that has been happily written without
 it. Surely eliminating the goto and tightening up the switch
 statement would be good to do in D?
In Pascal ascendants, we don't have a fall-through switch either. But we have an often "undocumented" goto. In my early days, after having heard that "goto is evil", i wrote some code (i guess it was a chess knight problem), where i avoided using goto at any cost. Then i looked at the mess and realised: it is not necessary to follow these rumors blindly. I have rewritten it using goto and it has contracted by half in source and became more legible. The best
Recently, I read a cheerful thread on the use of "goto" in the Linux kernel code. It's covered here: http://www.kerneltrap.org/node.php?id=553 A link to the original thread can be found at the bottom of the page. There are many legitimate comments on why goto might actually be useful, including the emulation of RAII idiom where it's not available. -Antti
Jan 21 2003
parent reply Ilya Minkov <midiclub tiscali.de> writes:
The discussion shows important points. Sometimes the conditions don't 
want to nest, and if you bend and force them the code will inevitedly 
become ugly. And the code becomes non-editable, that is if you are to 
add the third which wouldn't like to be nested... You have a long, long 
way in front of you. (unsigned long long :> )

I may say, that though i kinda respect what Wirth did for language 
design, i guess he was walking blindly. He's not proficient at 
structured proramming himself. I read a few excerpts from his book 
"numerical recepies in Pascal", including that of gauss-jordan 
elimination. That's something unstructured, illegible, unexplained. You 
have one main function, which is over a page long, and nests in about 6 
or 7 levels. After I saw that, 99% of my respect has vanished. I have 
split it into 3 tiny functions, each a few lines long, with a nesting 
level not higher than 2. Sane? Yes. And the compiler would do the inlining.


Antti Sykari wrote:
 Ilya Minkov <midiclub 8ung.at> writes:
 
 
Paul Conway wrote:

In Magik there is no goto and yet the company I work for has a base
of millions of lines of code that has been happily written without
it. Surely eliminating the goto and tightening up the switch
statement would be good to do in D?
In Pascal ascendants, we don't have a fall-through switch either. But we have an often "undocumented" goto. In my early days, after having heard that "goto is evil", i wrote some code (i guess it was a chess knight problem), where i avoided using goto at any cost. Then i looked at the mess and realised: it is not necessary to follow these rumors blindly. I have rewritten it using goto and it has contracted by half in source and became more legible. The best
Recently, I read a cheerful thread on the use of "goto" in the Linux kernel code. It's covered here: http://www.kerneltrap.org/node.php?id=553 A link to the original thread can be found at the bottom of the page. There are many legitimate comments on why goto might actually be useful, including the emulation of RAII idiom where it's not available. -Antti
Jan 22 2003
parent "Roberto Mariottini" <rmariottini lycosmail.com> writes:
"Ilya Minkov" <midiclub tiscali.de> ha scritto nel messaggio
news:b0m50i$1389$1 digitaldaemon.com...
[...]
 I may say, that though i kinda respect what Wirth did for language
 design, i guess he was walking blindly.
The old story that Pascal didn't have GOTO is a hoax. Professor Wirth was one of the first pioneer in an unexplored area, ant still today (after more than 30 years) much of his work is valid, though some parts are not. But the GOTO is another story. Wirth do said GOTO should be avoided, but it was included even in the first revision of Pascal (until today's pascal extensions), because it was inherently useful in some restricted areas. Ciao
Jan 24 2003
prev sibling parent reply Paul Conway <Paul_member pathlink.com> writes:
In article <b0h3bi$16tc$1 digitaldaemon.com>, Ilya Minkov says...

In Pascal ascendants, we don't have a fall-through switch either. But we 
have an often "undocumented" goto. In my early days, after having heard 
that "goto is evil", i wrote some code (i guess it was a chess knight 
problem), where i avoided using goto at any cost. Then i looked at the 
mess and realised: it is not necessary to follow these rumors blindly. I 
have rewritten it using goto and it has contracted by half in source and 
became more legible. The best thing to do for legibility are comments. 
As i grow older, there become more comments and less code in my code. 
And i do make justified use of anything. Why should I be prevented to do 
what is natural for a machine and make it another, non-natural way? This 
is basically what C is, it's always natural to a machine.
What I neglected to mention is that Pascal was invented before the 'structured goto' or 'structured escape'. When I got into Pascal, I was working at TI, and they had their own compilers, which implemented 'escape L' where L is a label (a name, not a number). All you had to do was prefix 'L:' to the block you wanted to get out of. So you could do the equivalent of 'break'. In Magik there is a _break statement which can optionally take a label associated with the block you want out of, just as in TI's Pascal. So eliminating the 'goto' is not the answer in itself, you have to provide an alternative, albeit one that is much tamer, and does not lend itself to spaghetti-style control-flow. In my opinion the human mind quite logically handles composing code using this restricted form of 'goto', and is well disposed to comprehending code written that way. What the human mind does not handle well is spaghetti control-flow.
Jan 24 2003
parent "Sean L. Palmer" <seanpalmer directvinternet.com> writes:
"Paul Conway" <Paul_member pathlink.com> wrote in message
news:b0sk8c$1le7$1 digitaldaemon.com...
 in TI's Pascal. So eliminating the 'goto' is not the answer in itself,
 you have to provide an alternative, albeit one that is much tamer, and
 does not lend itself to spaghetti-style control-flow. In my opinion the
 human mind quite logically handles composing code using this restricted
 form of 'goto', and is well disposed to comprehending code written that
 way. What the human mind does not handle well is spaghetti control-flow.
If you could draw little arrows in the source code, it wouldn't be so bad. ;) Sean
Jan 25 2003
prev sibling parent reply Farmer <itsFarmer. freenet.de> writes:

It is as powerful as the current switch statement, but more bug-safe.



switch(i)
{
case 1: // explicit break statement is generally required
     some_statement;
     break;
case 2: // compiler flags error, since break is missing here
    	some_statement;
case 3: // "fall through" is allowed, as there are no statements here
case 4:  
     some_statement;
     break:
case 5: // return or throw statements  can be used to "break" cases, too 
     some_statement;
    	return;
case 6: // "fall through" with statements is possible with an explicit goto
     some_statement;
    	goto 7;
case 7: // "fall through" with statements is possible with an explicit goto
     some_statement;
     goto default;
default:  
}





Farmer
Jan 25 2003
parent "Sean L. Palmer" <seanpalmer directvinternet.com> writes:

that annoys me about it is that, if it's smart enough to know that break has

Just get rid of it.

Damn C compatibility all to hell!

Sean

"Farmer" <itsFarmer. freenet.de> wrote in message
news:Xns930EAC5FDBCE5itsFarmer 63.105.9.61...

 It is as powerful as the current switch statement, but more bug-safe.



 switch(i)
 {
 case 1: // explicit break statement is generally required
      some_statement;
      break;
 case 2: // compiler flags error, since break is missing here
     some_statement;
 case 3: // "fall through" is allowed, as there are no statements here
 case 4:
      some_statement;
      break:
 case 5: // return or throw statements  can be used to "break" cases, too
      some_statement;
     return;
 case 6: // "fall through" with statements is possible with an explicit
goto
      some_statement;
     goto 7;
 case 7: // "fall through" with statements is possible with an explicit
goto
      some_statement;
      goto default;
 default:
 }





 Farmer
Jan 26 2003