Dynamics AX Development and Customization Best Practices-Code formatting recommendations

Some Dynamics AX customization best practices are applicable irrespective of AOT element. These standards include X++ standards includes:

  • Code formation
  • Naming conventions
  • Label standards
  • Help Text guidelines.

In this article we will cover some best practices related to the X++ language specially Code formatting recommendations.

Following are the Code formatting recommendations:

  • Place the opening brace at the beginning of the next line.

Example:

if (someExpression)

{

doSomething();

}

  • Align the closing brace with the corresponding opening brace.
    • Example

if (someExpression)

{

doSomething();

}

  • Place each opening and closing brace on its own line.
    • Example:

if (someExpression)

{

doSomething();

}

  • Do not omit braces. Braces are not optional, because they increase code readability and maintainability. They should be included even for single-statement blocks.

Example:

if (someExpression)

{

doSomething();

}

  • Omit braces for switch statements. These braces can be omitted, because the case and break statements clearly indicate the beginning and end.

Exampole:

case 0:

doSomething();

break;

  • Use a single space in the following cases:
    • On each side of an assignment operator

Example: cust.Name = “abc”;

    • After the comma between parameters

public void doSomething(int _x, int _y)

    • Between arguments

myAddress(myStr, 0, 1)

    • Before flow control statements

while (x == y)

    • Before and after binary operators

if (x == y)

    • After the semicolon between the parts of a for statement

for (i = 0; i < 10; i++)

  • Do not use any spaces in the following cases:
    • After an opening parenthesis or before a closing parenthesis

myAddress(myStr, 0, 1)

    • Between a member name and the opening parenthesis

myAddress()

    • After an opening bracket or before a closing bracket

x = dataArray[index];

    • Before or after unary operators

if (!y)

  • Use four spaces as the standard indent. In the code editor, the Tab key inserts four spaces. Indent in the following cases:
    • The contents of code blocks

if (someExpression)

{

doSomething();

}

    • Case blocks, even though they do not use braces

switch (someExpression)

{

case 0:

doSomething();

break;

}

    • A wrapped line one indent from the previous line

lastAccount = this.doSomething(

cust,

firstAccount,

startDate,

endDate);

    • Wrap lines that are too long to fit on a single line.
    • Wrap shorter lines to improve clarity.
    • Place each wrapped select and while select statement keyword at the beginning of a new line. The content associated with each keyword should be indented by one indent under the corresponding keyword.

select firstonly cust

where someExpression1

&& someExpression2

&& someExpression3;

select count(RecId)

from cust

where someExpression1

&& someExpression2

&& someExpression3;

while select firstonly cust

order by Name, AccountNum

where someExpression1

&& someExpression2

&& someExpression3

{

}

    • Do not use more or less than four spaces to force special alignment.

lastAccount = this.doSomething(

cust,

firstAccount,

startDate,

endDate);

Mostly developer use it as (This is wrong)

last = this.do(

cust,

firstAccount,

startDate,

endDate);

    • Put each indented parameter or argument on a separate line.
    • Use switch statements instead of consecutive if statements.
    • Do not use parentheses around the value of the cases of a switch statement.
    • Do not put the closing parenthesis for a method call on a new line.
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s