✏️ Explanatory Question

What are the layout and indentation rules for writing X++ in Dynamics 365 Finance & Operations? Cover indentation settings, one statement per line, parameter formatting, spacing around keywords, and the use of curly braces.

👁 3 Views
📘 Detailed Answer
🟢 Easy
💡

Answer with Explanation

D365 F&O • X++ INTERVIEW

Layout Rules & Indentation in Dynamics 365 Finance & Operations

Question 38 — Formatting X++ for readability: indentation, statements per line, spacing and braces.

Interview Question

What are the layout and indentation rules for writing X++ in Dynamics 365 Finance & Operations? Cover indentation settings, one statement per line, parameter formatting, spacing around keywords, and the use of curly braces.

Model Answer (Short)

A good layout uses formatting to emphasise the structure of the code and make it easy to read. Use the default Code Editor settings (Smart Indent, 4-character indent, save tabs as spaces). Write one statement per line and one declaration per line. Methods start with a single tab and their body with two tabs. There is no space between a method name and its parentheses; one or two parameters can go on the same line, but more than two should each go on a new indented line. Put one space after if, switch, for and while, and use curly braces around all code blocks — even single-statement blocks — except around the case labels of a switch.

Indentation & Editor Settings

Indentation rules

  • Always indent code correctly — proper indentation reflects structure.
  • Use the default Code Editor settings: Smart Indent, 4-character indent, save tabs as spaces.
  • Methods start with a single tab; everything inside a method starts with two tabs.
  • If subsequent lines aren't auto-indented, indent them by one tab stop (four spaces).

Statements & parameters

  • Write only one statement per line.
  • Write only one declaration per line.
  • No space between the method name and the opening parenthesis.
  • One or two parameters may share a line; more than two go each on a new line, indented four spaces.
  • Put a blank line between processing units.

Spacing & braces

  • Put one space after the opening keyword of if, switch, for, while.
  • Use curly braces around all code blocks — except around the case labels of a switch.
  • Use curly braces even if the block has only one statement.
GOLDEN RULE
4-space indent  •  One statement per line  •  Always use braces

Prerequisites (Rule 5)

  • Visual Studio with the Dynamics 365 developer tools.
  • A custom model / package for your objects.
  • Editor configured with Smart Indent, 4-char indent, tabs as spaces.

Code Example — Correct layout & braces

public void processOrders()
{
    SalesTable salesTable;   // one declaration per line

    while select salesTable
    {
        // Always use braces, even for one statement
        if (salesTable.SalesStatus == SalesStatus::Backorder)
        {
            this.process(salesTable);
        }
    }
}

Parameter formatting (more than two)

// One or two params can share a line
public Amount calc(Qty _qty, Price _price)
{
    return _qty * _price;
}

// More than two params -> each on its own indented line
public Amount calcDiscounted(
    Qty        _qty,
    Price      _price,
    Percent    _discount,
    CustAccount _custAccount)
{
    return (_qty * _price) * (1 - _discount / 100);
}

Avoid: poor indentation & missing braces

// BAD: no braces, poor indentation, multiple statements per line
if (myCondition)
   doSomething(); doSomethingElse();

// GOOD
if (myCondition)
{
    doSomething();
    doSomethingElse();
}

Points the interviewer wants to hear

  • Use default editor settings: Smart Indent, 4-char, tabs as spaces.
  • One statement and one declaration per line.
  • No space before method parentheses; split >2 params onto new lines.
  • One space after if/switch/for/while.
  • Braces around all blocks, even single statements (except switch cases).

Likely Follow-up Questions

  • Why use braces even for a single-statement block?
  • How should you format a method with more than two parameters?
  • What are the recommended default editor indentation settings?
  • Where are curly braces intentionally omitted?

Key Takeaway

Consistent layout makes X++ readable: use 4-space Smart Indent, one statement per line, no space before method parentheses, split long parameter lists, add a space after control keywords, and always use curly braces (except around switch cases).