mixm
mixm

Reputation: 531

OOP getter function should return a value or reference of a private property

should a getter of a private property return a reference to the property or the value of the property (or a clone if the property is an object).

Upvotes: 0

Views: 734

Answers (3)

T.J. Crowder
T.J. Crowder

Reputation: 1074475

If the property has a primitive type (string, number, boolean) or a reference to an immutable object (String, Number), the answer is that you return the value/reference directly, since there's nothing the caller can do with it that will change your object's state.

If the property has a reference type to a mutable object (an array, an Object, a Date, lots and lots of other things), it depends on whether you want the caller to be able to change the properties on your object's copy of it. If you do, return the reference. If not, clone it.

For example, let's assume an object with a private array and some operations you can perform on that array:

var obj = (function() {
    var o = {};
    var theArray = [];

    o.addItem = function(item) {
        theArray.push(item);
    };

    o.getItemAt = function(index) {
        return theArray[index];
    };

    return o;
})();

(Note that this is not meant to be a glorious example of object design. :-) )

Now you want to add a getArray operation. You can do that in two ways:

You can return a reference to the array:

o.getArray = function() {
    return theArray;
};

...which means the caller can do all sorts of things to it (like remove items from it, an operation the object didn't previously support) that will change your object's state (because you've given them direct access to the formerly-private array).

Or you can return a clone of the array:

o.getArray = function() {
    return theArray.slice(0);
};

...which means the caller can't do anything to the private array, they can only operate on the copy of it you gave them.

Upvotes: 3

dhinesh
dhinesh

Reputation: 4764

Generally wherever the property is to be used should not be able to change the property value. This satisfies the important principle of the OOP i.e Encapsulation.

Upvotes: 0

Aram Kocharyan
Aram Kocharyan

Reputation: 20431

This depends whether the private property is immutable or not. If it's immutable, it can't be changed and there is no harm in returning it. If however it is mutable, then returning it will break the OOP principle of information hiding.

If it's a primitive immutable int/number, then returning the private property is fine since it achieves two things:

  • You hide the private property and provide an interface, and any changes to the implementation of the private property will not break the interface as easily
  • The returned object (in this case a value) can't be changed by the caller because it's a primitive type and is passed by value

If however, you return a mutable Array from a method and your language passes objects by reference, then you must accept the fact that the caller may decide to change this internal object - which could very easily lead to your internal code breaking.

For these reasons, if you plan to return a copy to a mutable object, return a copy and not the underlying object itself.

Upvotes: 0

Related Questions