switch()

Walter Bright newshound2 at digitalmars.com
Mon Feb 17 12:00:05 PST 2014


On 2/17/2014 5:33 AM, Manu wrote:
>         We can compact it a bit 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;
>         }
>
>         But that's horrible too.

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.

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



More information about the Digitalmars-d mailing list