If it’s worth loading, it’s worth overloading

In this video, I’m showing the, (for AL vs C/AL) new capability of procedure overloading. Inspired by a recent Twitter thread.

https://youtu.be/YBBRS35nZwo

In this video, Erik explores the concept of procedure overloading in AL for Business Central — a feature made possible by the modern AL compiler. He demonstrates how overloading works, why it leads to cleaner and more understandable code, and how Microsoft themselves use this pattern in the base application with examples from the Base64 Convert codeunit.

What Is Overloading?

Overloading is the ability to define multiple procedures with the same name but with different signatures (different parameter lists). This concept has been available in languages like C# for decades, and it’s now available in AL for Business Central.

Instead of having one procedure with many parameters — some of which the caller may not care about — you can provide multiple versions of the same procedure, each tailored to a specific use case. The caller simply picks the version that matches the data they have available.

Building an Overloaded Example

Erik starts by creating a simple codeunit that demonstrates overloading with a CreateCustomer procedure. The idea is to provide three different ways to create a customer:

  1. Basic version — just creates a blank customer record
  2. With a name — creates a customer and sets the name
  3. Full version — creates a customer with name, posting group, and phone number

Here’s the complete codeunit:

codeunit 50214 "Overloading"
{
    procedure CreateCustomer(var Customer: Record Customer)
    begin
        Customer.Init();
        Customer.Insert(true);
    end;

    procedure CreateCustomer(Name: Text; var Customer: Record Customer)
    begin
        CreateCustomer(Name, '', '', Customer);
    end;

    procedure CreateCustomer(Name: Text; PostingGrp: Code[20]; PhoneNo: Text; var Customer: Record Customer)
    begin
        Customer.Init();
        Customer.Insert(true);
        Customer.Validate(Name, Name);
        Customer.Validate("Phone No.", PhoneNo);
        if PostingGrp <> '' then
            Customer.Validate("Customer Posting Group", PostingGrp);
        Customer.Modify(true);
    end;
}

The Key Pattern: Simpler Versions Call the Complex One

Notice how the second procedure (taking just a Name) doesn’t duplicate any logic. Instead, it calls the most complex version, passing empty strings for the parameters it doesn’t need:

procedure CreateCustomer(Name: Text; var Customer: Record Customer)
begin
    CreateCustomer(Name, '', '', Customer);
end;

This is a powerful pattern. The most complex overload contains all the real logic, and the simpler overloads act as convenient shortcuts that default the missing parameters. This approach:

  • Avoids code duplication
  • Keeps all the real business logic in one place
  • Simulates the concept of optional parameters that languages like C# support natively

IntelliSense Makes It Shine

One of the great benefits of overloading shows up in Visual Studio Code’s IntelliSense. When you type CreateCustomer. and trigger the autocomplete, you see something like “1 of 3” — meaning there are three versions of this procedure. You can arrow through them and see each signature, making it immediately clear what options are available to you as a consumer of this code.

To test this, Erik wires up a simple page extension on the Customer List:

pageextension 50214 "Cust List" extends "Customer List"
{
    actions
    {
        addfirst(processing)
        {
            action(TestOverload)
            {
                Caption = 'Create Customer';
                ApplicationArea = all;
                trigger OnAction()
                var
                    CreateCustomer: Codeunit Overloading;
                    base64: Codeunit "Base64 Convert";
                begin
                end;
            }
        }
    }
}

Real-World Example: Base64 Convert

Erik then shows that Microsoft uses this exact pattern in the standard Base64 Convert codeunit. Despite supporting many different conversion scenarios, the codeunit exposes only two function names:

  • ToBase64 — with seven overloads
  • FromBase64 — with four overloads

Without overloading, that would be eleven separate function names to discover and choose between. With overloading, you see just two, and IntelliSense guides you through the available signatures.

The ToBase64 overloads include versions that accept:

  • A string, returning a string (simplest)
  • A string with text encoding
  • A string with text encoding and code page
  • A string with a line breaks boolean
  • A string with line breaks, text encoding, and code page
  • A stream (encoding is already handled)
  • A stream with line breaks

Under the hood, the simpler versions call down to the most complex version, defaulting parameters along the way — exactly the same pattern Erik demonstrated in his CreateCustomer example. The simplest ToBase64(Text) sets line breaks to false and calls the next version, which in turn defaults encoding and code page, and so on until the final version that does all the actual work.

Why This Matters: The Old NAV Problem

In the old NAV days, without overloading, you’d typically end up with one function that had many parameters. As a caller, you were left guessing:

  • Which parameters are required?
  • Which ones can I leave blank?
  • What’s the correct default value for parameters I don’t care about?

This created confusion and led to bugs. With overloading, the consumer of your code sees exactly the signatures they can use, with no ambiguity about what’s optional and what’s not.

Summary

Overloading in AL is a pattern that brings clarity and simplicity to your Business Central code. The key takeaways are:

  • Overloading lets you define multiple procedures with the same name but different parameter lists
  • The recommended pattern is to have the simplest overloads call the most complex one, defaulting missing parameters
  • This effectively gives AL a form of optional parameters
  • It dramatically reduces the number of function names consumers of your code need to learn
  • IntelliSense in VS Code fully supports overloading, showing all available signatures
  • Microsoft uses this pattern extensively in the base application (e.g., the Base64 Convert codeunit)

If you’re building codeunits or libraries that others will consume — whether that’s your team, your customers, or the broader community — overloading is a tool that will make your APIs significantly more pleasant to work with.