switch()

Manu turkeyman at gmail.com
Mon Feb 17 18:54:43 PST 2014


On 18 February 2014 06:00, Walter Bright <newshound2 at digitalmars.com> wrote:
>
>
> I tend to format such like this:
>
>
>          int difficulty;
>          switch(e.note.note)
>          {
>            case 60: .. case 71:  difficulty = 0;  break;
>            case 72: .. case 83:  difficulty = 1;  break;
>            case 84: .. case 95:  difficulty = 2;  break;
>            case 96: .. case 107: difficulty = 3;  break;
>            default:              difficulty = -1; break;
>          }
>
> By lining things up, it takes on a tabular appearance. People are good at
> inferring patterns, and such tabular arrangements make it easy to spot
> squeaky wheels.


Me too, but you don't feel this is basically a hack?
About half of that text is repeated cruft, and there's no precedent for
formatting well-structured code like that anywhere else in the language.
How long would you say you spend on average fiddling with the tabulation?
I am known to spend minutes pressing the space bar, trying to make it line
up and look nice. And then invariably, some other case comes along, with a
slightly longer identifier name, and you have to work your way up shifting
everything around against with a bunch more spaces.. pollutes source
control history, etc.
And then that case with the REALLY long identifier name comes along, and
you end out with so many spaces surrounding all the other cases, that it
becomes difficult to associate which line matches which case, so then you
think to yourself, "this one long case is ruining my code, maybe I should
break the pattern and fall this long one onto it's own line below...", and
then you're just wasting time, pushing the spacebar key, trying to work
around something that shouldn't have been an issue in the first place.
Often enough the break statements end up far off the right hand side of the
screen due to that one long statement in the sequence; do you line them all
up religiously far to the right? Or do you make an exception for that one
long line, allowing it to span beyond the 'break' line, and keep the rest
lined up nearer to the code they terminate?
Tell me this doesn't happen to you? Surely I'm not the only one that faces
this sort of conundrum frequently when I try and use switch? :)

I can't see that practise as anything other than a formatting hack to deal
with something that was structurally unsound in the first place.


> Finally, you didn't address the suggestion to allow assignment of the
> switch > condition to a properly scoped variable: switch([x =] expression) ?
>
> That's probably a good idea.
>

Cool, I'll bugzilla it. At least something useful may comes from my ranting
:P
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.puremagic.com/pipermail/digitalmars-d/attachments/20140218/d8a1edda/attachment-0001.html>


More information about the Digitalmars-d mailing list