Working on AL coding guidelines

When creating your AL project (and your .al files), remember to always follow these main guidelines.

Inside a .al code file, the structure for all your objects must follow this sequence:

  • Properties
  • Object-specific constructs:
    • Table fields
    • Page layout
    • Actions
  • Global variables:
    • Labels (old text constants)
    • Global variables
  • Methods

Remember to always reference the AL objects by their object name and not by their ID. So, for example, this is how you reference a Record variable or a Page variable:

Vendor: Record Vendor;
Page.RunModal(Page::"Customer Card", ...);

In an event subscriber object, this is how you should reference the publisher object:

[EventSubscriber(ObjectType::Codeunit, Codeunit::MyCodeunit, 'MyIntegrationEvent', '', false, false)]
local procedure MyIntegrationEventSubscriber()
begin
end;

So, let's sum this up:

  • Format your AL code: Take care of indentation and spacing (it keeps the code more readable). You can use Alt + Shift + F to auto-format your code.
  • Keep your .al files cleanWhen using snippets, they automatically create an object skeleton with methods, properties, variables, triggers, or sections that you might not be using. Please remove all the code that isn't being used. A typical example is triggers definitions on tables (which you can remove if you're not handling them) or global variables inside objects (if you don't remove them, your app will be full of myInt: integer variables).
  • Method declarations: Be as local as possible. Only use global methods if you need to expose them to other objects.
  • Use events to trigger business logic, but do not code in these triggersPutting a lot of code inside triggered events is just like putting a lot of code into field validation triggers. Identify your methods instead and call them from the triggered events.

For complex code, you can start using the Generic Method pattern:

  • Declare each method on its class (table).
  • Each method is a codeunit on its own (encapsulation).
  • Invoke a method only from its class (table/codeunit).
  • Each method's codeunit only has one global function.
  • Local functions include the following categories (in this order):
    1. Main (one function; a method header in the form of a readable flowchart)
    2. Main business process (multiple functions)
    3. UI wrapper (two functions)
    4. Business extension (one or more functions to provide extensibility)
    5. Event wrapper (two functions)

This is an example of some AL code that's been organized according to this pattern:

Respecting coding rules and guidelines is extremely important for increasing code readability, and many of these rules are mandatory for AppSource.

..................Content has been hidden....................

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