IF and CASE

If nested formatting of a single IF statement increases readability, this is considered good practice. But it should not be overdone.

IF THIS-OBJECT:DesignTime THEN RETURN FALSE . IF VALID-HANDLE(hDataset) AND NOT THIS-OBJECT:FindString > "":U THEN hDataset:EMPTY-DATASET () . IF VALID-OBJECT (THIS-OBJECT:SmartDataSource) AND THIS-OBJECT:ForeignFields > "":U THEN THIS-OBJECT:FetchQueryString = SUBSTITUTE("&1 &2":U, THIS-OBJECT:EvaluateParentQuery (), THIS-OBJECT:QuerySort). ELSE DO: IF THIS-OBJECT:QueryString = "":U AND THIS-OBJECT:QuerySort > "":U THEN THIS-OBJECT:FetchQueryString = SUBSTITUTE ("FOR EACH &1 &2":U, THIS-OBJECT:EntityTable, THIS-OBJECT:QuerySort) . ELSE THIS-OBJECT:FetchQueryString = SUBSTITUTE("&1 &2":U, THIS-OBJECT:QueryString, THIS-OBJECT:QuerySort). END.

A THEN DO or ELSE DO block is recommended when a THEN contains an IF statement and one of the two IF statements uses an ELSE option.

In case of a sequence of IF statements on related conditions it is recommended to use a CASE block.

When coding an IF statement with only one command following no DO block is required and the statement should be broken with indentation into the following line.

IF ... THEN Statement IF ... THEN DO: Statement. Statement. END.

When inserting more than one statements between the if statement the DO is kept in the line with the IF.

Inline statements are indented with 4 leading spaces and the END is on the same indentation level as the IF.

IF ... THEN IF ... THEN Statement. ELSE Statement. IF ... THEN DO: IF ... THEN Statement. ELSE Statement. END. ELSE Statement.

When there is more than one comparison for a single IF statement then the operator AND / OR is at the beginning of the line. Each boolean check should be in a separate line. Nested boolean operation can be placed in the same line if they are not too complex. The Code should be fluent to read to make the boolean operation easy to understand.

When the DO Block following the IF requires additional options, the following formatting should be considered: