May 19, 2009 Re: Switch | ||||
|---|---|---|---|---|
| ||||
Georg Wrede Wrote: > bearophile wrote: > > > > void classify(char c) { > > write("You passed "); > > switch (c) { > > case '#': > > writeln("a hash sign."); > > break; > > case '0' ..> '9': > > writeln("a digit."); > > break; > > case 'A' ..> 'Z', 'a' ..> 'z': > > writeln("an ASCII character."); > > break; > > case '.', ',', ':', ';', '!', '?': > > writeln("a punctuation mark."); > > break; > > default: > > writeln("quite a character!"); > > break; > > } > > } > (A bunch of other versions omitted.)... > > > void classify(char c) { > write("You passed "); > switch (c) { > case '#': > writeln("a hash sign."); > break; > case '0' .. case '9': > writeln("a digit."); > break; > case 'A' .. case 'Z': > case 'a' .. case 'z': > writeln("an ASCII character."); > break; > case '.', ',', ':', ';', '!', '?': > writeln("a punctuation mark."); > break; > default: > writeln("quite a character!"); > break; > } > } > > This is usable, easy to read -- and the programmer has no problem to remember that .. works differently in case statements than in ranges. > > There are other places in D that place an undue burden on the programmer, but this is not IMHO one of them. > > -------------------- > > My pet peeve with case is that, since we don't seem to be getting rid of break (and use some word for fall-through instead, "which would save a good deal of ink"), we should at least try to make break more conspicuous. I can name a hundred times I've forgotten a break from somewhere. So the canonical indentation should be like this: > > > void classify(char c) { > write("You passed "); > switch (c) { > case '#': > writeln("a hash sign."); > break; > case '0' .. case '9': > writeln("a digit."); > break; > case 'A' .. case 'Z': > case 'a' .. case 'z': > writeln("an ASCII character."); > break; > case '.', ',', ':', ';', '!', '?': > writeln("a punctuation mark."); > break; > default: > writeln("quite a character!"); > break; > } > } > > (It'd look better when the cases have more lines, but still.) Currently in D, break is at least as important as case, therefore it deserves a conspicuous place, hence my suggestion. Full Circle... http://www.digitalmars.com/webnews/newsgroups.php?art_group=digitalmars.D&article_id=81519 | ||||
May 19, 2009 Re: Switch | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Mike James | Mike James wrote:
> Full Circle...
>
If you're trying to emphasize the significance of this proposal, then good.
But if you're trying to say that one should post more original posts, then bad.
| |||
Copyright © 1999-2021 by the D Language Foundation
Permalink
Reply