Friday, April 4, 2014

X++ Code Layout

  • Only one statement per line.
  • Break up complex expressions that are more than one line - make it visually clear.
  • Use a single blank line to separate entities.
  • Do not use parentheses around the case constants. Warning icon
  • Do not use parentheses around where expressions.
  • Add one space between ifswitchforwhile and the expressions starting parentheses. For example:
    if (creditNote)
  • Use braces around all code blocks, except for around case clauses in a switch statement. Use braces even if there is only one statement in the block.
An indentation is equivalent to four (4) characters, which corresponds to one tab in the X++ editor. You must not start a new line in columns 2, 3 or 4. Error icon
  • Put opening and closing braces, { and }, on the same level, on separate lines, and aligned with the command creating the block. They must be at the start of a line, and in a tab column position (1, 5, 9 etc.). Error iconBraces must be on a dedicated line Error icon unless a opening brace is followed by a semicolon ( {; ) or a closing brace is followed by a while ( }while ).
  • The following reserved words should be placed at the beginning of a line: casecatchchangeCompanycontinuedefaultelseforifretryreturnswitchtryttsAbortttsBeginttsCommit,whileWarning icon
    The exceptions to this rule are:
    case: … (reserved words in a case statement)
    default: … (reserved words in a default statement)
    else if
    }while
  • If a line of code starts with any other reserved word, or with an alphabetical character, the line should start in a tab column position (1, 5, 9 etc). Warning icon The following reserved words must be placed in a tab column position: casecatchchangeCompanycontinuedefaultelseforifretryreturnswitchtryttsAbortttsBeginttsCommitwhileError icon
    The exceptions to these rules are:
    case: … (reserved words in a case statement)
    default: … (reserved words in a default statement)
    else if
    }while
  • The reserved word else must have a dedicated line unless you write else if Error icon.
  • switch-case statements: indent case and default statements by 1 level (with any code within these indented a further level) and indent break statements by two levels.
  • Indent where and other qualifiers to the select statement by one level.
  • If Booleans are used in a long expression, put them at the start of an indented new line.

Example switch-case Statement

switch (myEnum)
{
    case ABC::A:
        ...
        break;
    case ABC::B
        ...
        break;
    default:
        ...
        break;
}

Example select Statement

select myTable
    index hint myIndex
    where myTable.field1 == 1
        && myTable.field2 == 2;

Example Layout of Booleans in a Long Expression

select firstOnly utilElements
    where utilElements.recordType == recordType
        && utilElements.parentId == parentID
        && utilElements.name == elementName;
Column layout should be used where more than one line has the same structure; for example, in declarations or assignments.
Do not use column layout when there is only one row, or if consecutive rows do not have the same structure.
Column format is defined using extra blanks.

Example Column Layout

tmpABC.refRecId     = inventTable.recId;
tmpABC.itemGroupId  = inventTable.itemGroupId;
tmpABC.itemId       = inventTable.itemId;
tmpABC.amount       = amount;
tmpABC.oldValue     = this.getCategory(inventTable);
tmpABC write();
The starting parenthesis on method declarations and calls should be the character just after the method name (no space).
If there are one or two parameters, the parameters can be listed on the same line. If there are more than two parameters, move each parameter onto a new line, and indent by 4 spaces.

Example Layout for Method with One or Two Parameters

myMethod(parameter1, parameter2);

Example Layout for Method with Many Parameters

myMethod(
    parameter1,
    parameter2,
    parameter3);

No comments:

Post a Comment