Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Representation exposure on an int method

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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;
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading