switch()

Nick Sabalausky SeeWebsiteToContactMe at semitwist.com
Mon Feb 17 22:00:23 PST 2014


On 2/17/2014 9:54 PM, Manu wrote:
> 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 use the formatting posted above, and it takes me very, very little time.

> 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.

Sounds like you need elastic tabstops:

http://nickgravgaard.com/elastictabstops/

I don't use them personally since PN2 doesn't support them yet, but 
they're a brilliant idea and they do solve that re-aligning issue. Word 
processors figured out how tabstops are supposed to work ages ago. 
Elastic tabstops really just amount to bringing code editors' ancient 
tab-handling up-to-date with the 90's.

> 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? :)

Erm, between all of that, plus your strong objection to "default: 
break;", I really do get the impression you're just simply being very 
OCD about this stuff. I don't mean that as an insult, I just think it's 
all a bit "Adrian Monk of source code", if you're familiar with the 
reference.



More information about the Digitalmars-d mailing list