Sunday 16 November 2014

PL/I - Select - When Coding

Use the SELECT statement when there are a large number of different conditions to test for. It's much easier to read than nested IF statements, and much safer to use. Too many nested IF statements get maintenance programmers in trouble, and are very difficult to understand and change. Never have the nesting be over one printed page in length. Too many WHEN…DO…ENDS make the code difficult to read:


[label:] SELECT(BOOZE);
WHEN('BEER') CALL USE_BIG_GLASS;
WHEN('GIN')DO; MARTINI = GIN||OLIVE; CALL STIR; END;
WHEN(BOURBON,RYE,NOT_TAXED) ITS_AMERICAN = YES;
WHEN(WINE) IF RED
THEN CALL SELECT_GLASS('RED');
ELSE CALL SELECT_GLASS('WHITE');
WHEN(WATER); /* IGNORE */
OTHERWISE DO; /*SOMETHING ELSE */ END;

END [label]; /* SELECT */
Using a label on a SELECT or a DO statement is a nice way to document the scope of the logic. This also helps the compiler report the location of missing END statements accurately.


A1: SELECT(SOME) WHEN… OTHERWISE… END A1; 

No comments:

Post a Comment