5

As you know, we can use apex property or member variable to manage the data. Of course, the apex property can do more additional things, now I just use the property to get/set value instead of using the variable with geting/seting method, does that affect apex performance?

1 Answer 1

5

Personally, if some data is going to be exposed from an object I'll use a property. Otherwise I'll use a private member variable (field) to store the internal data about an object that isn't directly exposed. It's worth noting that properties in Apex also come with their own pre-baked backing variables, so often there isn't even a need for a field (similar to C# autoproperties except it's available even if you define a get/set).

With properties I don't need to worry about the underlying fields changing, just that I can still expose the required properties.

Properties also give you the opportunity to:

  • do validation for both setting and getting (as required)
  • do lazy loading / initialization. This could turn into a philosophical debate around modifying state in a property.
  • do purely calculated values. E.g. derived from other properties.
  • provide a nice short hand for Visualforce binding. An equivalent method would need the get prefix.
  • vary the get and set access levels. E.g. private setter.
  • do dirty tracking via the setter to tell if the value has been changed.

The big weakness of Properties in Apex for me is around inheritance. You can't override a Properties implementation in a sub-class using the override keyword.

Again, personally, I wouldn't worry about the performance differences between properties and member variables unless you are doing something extreme. If the properties just a default get/set implementation then it's mostly the compilers problem to worry about performance.

You must log in to answer this question.

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