4

Suppose I have a Constants class with thousands of variables. Which code do you think would be better in terms of performance and reliability?

Option A:

public static final String URL_HTTPS = 'https://';

Option B:

public static final String URL_HTTPS {
    get {
        return 'https://';
    }
}
1
  • Option B. If you ever have to look at an Apex debug log, it will be flooded with the option A initialisations. (And incur the costs the answerer's below have described.)
    – Keith C
    Commented Apr 25, 2023 at 21:28

3 Answers 3

7

There is a potentially huge impact if you choose to keep all your constants in one class: Apex Heap Size.

As soon as you use one of the constants, all of them are initialized and consume memory.

So, one good practice is to keep constants in the classes they are used.

If you inherited a class and need to perform the least amount of changes, then using a getter allows you to potentially shave of some Apex Heap allocation.

This old answer gives good advice.

Last but not least: you should not aim for performance alone, first, aim to readability.

1
  • 2
    Heap size probably won't be a primary concern for most code, but that is a good point. No point in wasting heap needlessly.
    – sfdcfox
    Commented Apr 25, 2023 at 12:00
5

Unlike some languages, Apex does not store static final constants within the class code directly, but instead has to execute every assignment in the class the first time you access any of the class's properties. In an organization I previously worked at, our constants class had enough lines of code to add an entire second of CPU time to every transaction that used a constant. And every class and trigger used at least one constant. By switching from final constant to using a getter for every property, we saved about one second of CPU time from every transaction, including every unit test. Refactoring to use getters saved us something like 5 minutes every time we had to deploy or "run all tests."

Personally, I'd recommend that you keep constants as local as possible, but if you have to use a shared constants class, using getters will dramatically improve your code's performance as you get into the hundreds of properties territory. It does require more overall code, and there are some cases where you shouldn't use it (e.g. if you have properties that use a query, it's probably better for a static final constant), so the overall cost for each design has to be considered. As a general rule of thumb, however, one should first consider using getters over constants, as getters perform better as the number of constants increase.

1
  • 1
    +1 I'd recommend that you keep constants as local as possible; 100% agree, in a galaxy long long ago I too had a giant CONSTANTS.cls. Now my constants are distributed across (mostly) domain classes
    – cropredy
    Commented Apr 25, 2023 at 21:42
0

As far as apex character limit is concerned. I feel writing all constants alphabetically without getter will occupy less apex characters in the org.

And as the code size increases it would eventually have an performance impact too.

Let see example.

public static final String URL_HTTPS1 = 'https://';
public static final String URL_HTTPS2 = 'https://';
public static final String URL_HTTPS3 = 'https://';
public static final String URL_HTTPS4 = 'https://';
public static final String URL_HTTPS5 = 'https://';
public static final String URL_HTTPS6 = 'https://';
public static final String URL_HTTPS7 = 'https://';
public static final String URL_HTTPS8 = 'https://';

public static final String URL_HTTPS { get {return 'https://';}}
public static final String URL_HTTPS1 { get {return 'https://';}}
public static final String URL_HTTPS2 { get {return 'https://';}}
public static final String URL_HTTPS3 { get {return 'https://';}}
public static final String URL_HTTPS4 { get {return 'https://';}}
public static final String URL_HTTPS5 { get {return 'https://';}}
public static final String URL_HTTPS6 { get {return 'https://';}}
public static final String URL_HTTPS7 { get {return 'https://';}}
public static final String URL_HTTPS8 { get {return 'https://';}}
2
  • 3
    You would be incorrect, actually. There is a performance issue as code size increases.
    – sfdcfox
    Commented Apr 25, 2023 at 11:45
  • technically yes, as the code size increases the performance would get impacted. I have edited and updated the answer. Thanks @sfdcfox Commented Apr 25, 2023 at 11:46

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .