Header Ads Widget

Top Picks

6/recent/ticker-posts

AL coding guidelines in Business central - Application Language

AL coding guidelines in Business central


Introduction


The most effective procedures for creating AL code for Microsoft Dynamics 365 Business Central. It covers several topics, including:

  1. Naming conventions: The recommendations advise using camel case for local variable names and Pascal case for variable and function names. They also recommend using a specific naming convention for functions, such as "FunctionName(InputType1: Type1, InputType2: Type2): ReturnType".
  2. Formatting: The guidelines recommend using a consistent indentation and spacing style throughout your code, and placing the opening curly brace of a function on the same line as the function name.
  3. Comments: The guidelines recommend providing clear and concise comments throughout your code, explaining the purpose of each function, variable, and other elements.
  4. Error handling: The guidelines recommend using the "ERROR" function to handle errors and exceptions, rather than using the "raise" statement.
  5. Performance: The guidelines recommend being mindful of performance when designing and implementing your code. They recommend using efficient algorithms and avoiding unnecessary calculations, data retrieval, and data manipulation.
  6. Security: The guidelines recommend following best practices for data validation, user input validation, and error handling to ensure that your code is secure.
  7. Testability: The guidelines recommend designing your code in such a way that it is easy to test and debug. This includes keeping functions small and focused, with a single purpose and minimal dependencies.
  8. Reusability: The guidelines recommend designing your code in such a way that it can be easily reused in other parts of the system, or in other systems.
  9. Compatibility: The guidelines recommend testing your code in different environments, and with different versions of the Dynamics 365 Business Central platform, to ensure compatibility.
  10. Compliance: The guidelines recommend adhering to local laws, regulations, and industry standards when developing your code.

These guidelines are a great set of general direction to follow when developing code in AL, following them will help ensure your code is maintainable, readable, and easy to understand, and also that it performs well and comply with industry standards.


Examples


Here are some examples that demonstrate the best practices for AL code development outlined in the guidelines from the website you provided:


Naming conventions:


// Pascal casing for variable and function names

MyGlobalVariable: Integer;

CalculateTotal() : Decimal;

// camel casing for local variable names

localMyVariable : Boolean;


Formatting


// Consistent indentation and spacing
while (i < 10)
begin
    i := i + 1;
    // some code here
end;

// Begin the opening curly brace on the same line as the function name.
function DoSomething() : Boolean
begin
    // some code here
end;

Comments


// Clear and concise comments

// The cost of a list of things is calculated overall by this function.

function CalculateTotal(items : List of Item) : Decimal

begin

    // some code here

end;


Error handling


// Using the ERROR function to handle errors

if (Variable1 > Variable2) then

begin

    ERROR('Variable1 must be less than or equal to Variable2');

end;


Performance


// Efficient algorithm for finding the maximum value in a list
function FindMax(list : List of Integer) : Integer
var
    maxValue: Integer;
begin
    maxValue := list[1];
    for i in 2..list.Count do
    begin
        if (list[i] > maxValue) then
            maxValue := list[i];
    end;
    exit(maxValue);
end;


Security


// Validating user input
function DoSomething(input : Integer) : Boolean
begin
    if (input < 0) then
        ERROR('Input must be a positive integer');
    // some code here
end;


Testability


// Keeping functions small and focused
function CalculateTotalCost(items : List of Item) : Decimal
begin
    exit(CalculateCostPerItem(items) * CountItems(items));
end;

function CalculateCostPerItem(items : List of Item) : Decimal
begin
    // some code here
end;

function CountItems(items : List of Item) : Integer
begin
    // some code here
end;

Reusability


// Creating a function that can be reused in other parts of the system
function IsValidEmail(email : Text) : Boolean
begin
    // some code here
end;

Compatibility


// Testing code in different environments
if (ENVIRONMENT = 'Test') then
begin
    // some code here
end;
else if (ENVIRONMENT = 'Production') then
begin
    // some code here
end;

Compliance


// Adhering to industry standards
function DoSomething() : Boolean
begin
    // some code here
    if (IndustryStandard = 'FDA') then
    begin
        // some code here
    end;
end;


Here are some additional points for each of the best practices for AL code development outlined in the guidelines:


  1. Naming conventions:

  • Always choose meaningful and descriptive names for variables, functions, and other elements.
  • Abbreviations should be avoided unless they are commonly used and well-understood by others.
  • Avoid using single letters as variable names, except for indexing loops.

  1. Formatting:

  • Use consistent indentation throughout your code, to indicate the hierarchy of functions and other elements.
  • Use consistent spacing between elements, such as operators, commas, and curly braces.
  • Use a consistent style for punctuation and capitalization, such as always including semicolons at the end of statements.

  1. Comments:

  • Comments should provide a high-level explanation of the purpose of the code and its key elements.
  • Avoid using comments to simply repeat what the code is doing.
  • Commenting on "how" code works is less important than why it works.
  • Use the appropriate commenting style for the language you're using, such as "//" for AL

  1. Error handling:

  • Use the ERROR function to handle errors, rather than using the "raise" statement. This provides a consistent way of handling errors throughout your code.
  • Define a clear and consistent error-handling strategy for your code.
  • Always provide a clear error message that describes the problem and how to fix it.

  1. Performance:

  • Avoid unnecessary calculations and data retrieval.
  • Be mindful of the performance implications of algorithms and data structures.
  • Identify and optimize performance bottlenecks in your code using profiling tools.

  1. Security:

  • Validate all input data to ensure that it is of the expected type, format, and value.
  • Sanitize input data to prevent SQL injection, cross-site scripting (XSS), and other types of attacks.
  • Use secure cryptographic techniques to protect sensitive data, such as passwords and other authentication information.
  • Error messages should not disclose sensitive information


  1. Testability:

  • Design your code to be easy to test and debug.
  • Keep functions small and focused, with a single purpose and minimal dependencies.
  • Use a consistent testing strategy throughout your code.
  • Use automated testing frameworks to test your code.


  1. Reusability:

  • Design your code to be easily reusable in other parts of the system or other systems.
  • Use modular design principles, such as encapsulation and abstraction.
  • Keep functions and other elements as generic as possible, so that they can be reused in different contexts.
  • Provide clear and detailed documentation for reusable functions and other elements


  1. Compatibility:

  • Test your code in different environments and with different versions of the Dynamics 365 Business Central platform, to ensure compatibility.
  • Test your code with different configurations of the platform, such as different locales and currencies.
  • Keep track of changes to the platform and make sure your code is compatible with the latest version.


  1. Compliance:

  • Adhere to local laws, regulations, and industry standards when developing your code.
  • Use industry-standard libraries and frameworks whenever possible.
  • Keep track of changes in compliance regulations and ensure that your code is up to date.


These are just a few of the many considerations that should be taken into account when developing code in AL and Microsoft Dynamics 365 Business Central. Remember to always strive for readability, maintainability, and good performance when creating your code to allow future developers to quickly understand and maintain it.

 

Check here for more information




Will meet you soon in the next blog.


Regards,


Dr. S. Gomathi


MVP, MCT

Post a Comment

0 Comments

Youtube Channel Image
goms tech talks Subscribe To watch more Tech Tutorials
Subscribe