Representation exposure on an int method

Advertisements

This function exposes the value of worldSize, so it is a form of representation exposure:

/**
 * Returns the size of the simulation world.
 * Used for documentation ONLY
 * Should not have representation exposure
 *
 * @return The size of the simulation world.
 */
public int getWorldSize() 
{
    int clone = this.worldSize;
    return clone;
}

I have multiple of these functions in a project, and am only using them for documentation. I am wondering if its possible to obtain the same result without exposing the values of the integers directly.

I have used .clone() or Arrays.copyOf() in the past, but both of those are not for int type functions.

>Solution :

Primitive values are copied

There is no representation exposure in this case because the value is a primitive int type, not an object. The value is automatically copied. Unlike an object reference or an array, there is no reference shared that could be used to modify the original value. This is just a simple getter function. You could just simplify it as:

public int getWorldSize() {
    return this.worldSize;
}

Leave a ReplyCancel reply