Closed glemercier closed 4 years ago
Returning a copy of these arrays would adversely impact performance without improving the maintainability of the code in any appreciable way. Encapsulation is important in object-oriented programming, but this is a case where any modification of the returned arrays would be such an obvious mistake that it would simply not be done by a competent programmer.
In the Node class, private variables
identifier
andipAddress
are returned by reference in their getter functions:https://github.com/n-y-z-o/nyzoVerifier/blob/master/src/main/java/co/nyzo/verifier/Node.java#L31
As a result these private variables can be altered by calling functions after getting access to the private variable reference. While this is a minor issue that does not translate in any undefined behavior from wherever it's called in the rest of the code, it might be good to fix it for the sake of best practices and to prevent any potential future misbehavior.
This article describes the best practice in this case, which recommends copying the data to return into a new array as follows: