Coding best practices

In this section, we will learn some best practices to be followed during development in Dynamics AX.

Best practice check

Run your code through the X++ best practices process, evaluate all the best practices errors and warnings, and take the appropriate action.

Naming variables and objects

Begin the variable and method names with lowercase letters like custTable, validateFields, and so on. Begin the names of the AOT elements with capital letters such as CustTable, SalesFormLetters, and so on.

Prefix the parameters names with an underscore (Args _args). Use meaningful and self-explanatory variable names. For example: SalesTable salesTable and not SalesTable table1

Commenting the code

Code comments enhance the readability of the code and are very useful for those involved in modifying or maintaining the code. Comments should be used to describe the intent, algorithmic overview, and the logical flow.

Add XML documentation for class, class methods, and table methods. You can generate an XML file from this documentation.

Labels and text

Use labels for all text such as labels, form caption, infolog, and so on and provide code comments.

Database

The following list provides the best practices guidelines related to the database:

  • Avoid using direct SQL calls from the X++ code. Direct SQL statements do not respect application security.
  • Consider specifying a field list in select statements to increase the performance.
  • Avoid display methods whenever possible.
  • Run the code on AOS whenever possible.
  • Use the where clauses that align with the indexes in select statements and queries.
  • Use firstonly where applicable to increase the performance.
  • Use aggregates in the selection criteria instead of letting the code do the aggregation.
  • Use table joins instead of while loops.
  • Use Update_Recordset and insert_recordset wherever applicable.

Transactions

Keep the following best practices in mind when creating transactions:

  • Keep the database transactions as short as possible
  • Do not include user interaction inside the database transactions
  • Use throw instead of ttsAbort
  • On a server-side TTS block, do not call back to the client for displaying the dialog boxes

Exception handling

The following list provides the best practices guidelines related to exceptions:

  • The throw statement an exception to stop processing. It will stop the transaction execution and rollback operations if inside the transactions.
  • Use the info, warning, and error functions without a thrown exception in cases where the X++ call stack that is being executed should not be stopped.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset