3

From the documentation Custom Labels are primarily meant for multi-language translation use, but our company does not use them for this purpose, but uses them extensively for

  • text used in pdfs/VF pages
  • css styles
  • constant values

Is this good practice? Or is there a better way to do this? How many is too many?

2
  • I'm curious why you don't use a static resource for the CSS styles? Mar 5, 2021 at 18:10
  • We do for most CSS, just some seem to be in the labels.
    – Irene
    Mar 7, 2021 at 21:48

2 Answers 2

6

Is this good practice?

Generally no, they should not be used for this purpose. Write a constants class, or use constants in the class they are used in.

Or is there a better way to do this?

I prefer writing constants as static variables with getters. This is primarily a performance concern. The general pattern looks like this:

public static Decimal PI { get { return 3.14159265359; } }
public static String ASC { get { return 'Ascending'; } }

If you only have a few constants, though, you should prefer the more compact, Java-like syntax:

public static final Decimal PI = 3.14159265359;
public static final String ASC = 'Ascending';

The problem with this latter syntax is having hundreds of these in a single class can result in hundreds or even thousands of milliseconds in CPU overhead. Ancedotally, I once removed about 5 minutes off of our unit tests by converting from final static variables to getters (300 unit tests + 1 second saved per unit test).

If you do use constants constantly, try not to put them all in a shared global class if you prefer to use the final static method of writing constants.

How many is too many?

5,001. There's a limit of 5,000 labels per namespace in an org.

0

I was trying to test a huge Constant Class with the 2 options. The test basically try to get all the constants of the class (around 300), and these are my results:

Getters:

  • The Heap when the class is loaded in memory is 219
  • The Heap when the Test class is loaded in memory is 20229
  • Total time execution: 25.25ms

Constant:

  • The Heap when the class is loaded in memory is 761
  • The Heap when the Test class is loaded in memory is 10807
  • Total time execution: 8.13ms

So I think that regarding with the CPU use is better use Getter option, but you are losing time execution. What. do you think about it?

Thanks

1
  • Your timing numbers seem off. Are you really getting fractions of milliseconds in your measurements? Also, are you trying to answer the question which was posted or ask a new one?
    – Adrian Larson
    Mar 5, 2021 at 13:51

You must log in to answer this question.

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